fcoreutils 0.22.0

High-performance GNU coreutils replacement with SIMD and parallelism
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
use std::io::{self, BufRead, BufReader, BufWriter, Write};
#[cfg(unix)]
use std::mem::ManuallyDrop;
#[cfg(unix)]
use std::os::unix::io::FromRawFd;
use std::path::Path;
use std::process;

use coreutils_rs::common::io_error_msg;
use coreutils_rs::hash::{self, HashAlgorithm};

const TOOL_NAME: &str = "sha224sum";
/// SHA224 hex digest is always 56 characters.
const SHA224_HEX_LEN: usize = 56;

struct Cli {
    binary: bool,
    check: bool,
    tag: bool,
    text: bool,
    ignore_missing: bool,
    quiet: bool,
    status: bool,
    strict: bool,
    warn: bool,
    zero: bool,
    files: Vec<String>,
}

/// Hand-rolled argument parser — eliminates clap's ~100-200µs initialization.
fn parse_args() -> Cli {
    let mut cli = Cli {
        binary: false,
        check: false,
        tag: false,
        text: false,
        ignore_missing: false,
        quiet: false,
        status: false,
        strict: false,
        warn: false,
        zero: false,
        files: Vec::new(),
    };

    let args = std::env::args_os().skip(1);
    let mut saw_dashdash = false;
    for arg in args {
        let bytes = arg.as_encoded_bytes();
        if saw_dashdash {
            cli.files.push(arg.to_string_lossy().into_owned());
            continue;
        }
        if bytes == b"--" {
            saw_dashdash = true;
            continue;
        }
        if bytes.starts_with(b"--") {
            match bytes {
                b"--binary" => cli.binary = true,
                b"--check" => cli.check = true,
                b"--tag" => cli.tag = true,
                b"--text" => cli.text = true,
                b"--ignore-missing" => cli.ignore_missing = true,
                b"--quiet" => cli.quiet = true,
                b"--status" => cli.status = true,
                b"--strict" => cli.strict = true,
                b"--warn" => cli.warn = true,
                b"--zero" => cli.zero = true,
                b"--help" => {
                    print!(
                        "Usage: {} [OPTION]... [FILE]...\n\
                        Print or check SHA224 (224-bit) checksums.\n\n\
                        With no FILE, or when FILE is -, read standard input.\n\n\
                        \x20 -b, --binary         read in binary mode\n\
                        \x20 -c, --check          read checksums from the FILEs and check them\n\
                        \x20     --tag             create a BSD-style checksum\n\
                        \x20 -t, --text           read in text mode (default)\n\
                        \x20 -z, --zero           end each output line with NUL, not newline\n\n\
                        The following five options are useful only when verifying checksums:\n\
                        \x20     --ignore-missing  don't fail or report status for missing files\n\
                        \x20     --quiet           don't print OK for each successfully verified file\n\
                        \x20     --status          don't output anything, status code shows success\n\
                        \x20     --strict          exit non-zero for improperly formatted checksum lines\n\
                        \x20 -w, --warn           warn about improperly formatted checksum lines\n\n\
                        \x20     --help            display this help and exit\n\
                        \x20     --version         output version information and exit\n",
                        TOOL_NAME
                    );
                    process::exit(0);
                }
                b"--version" => {
                    println!("{} (fcoreutils) {}", TOOL_NAME, env!("CARGO_PKG_VERSION"));
                    process::exit(0);
                }
                _ => {
                    eprintln!(
                        "{}: unrecognized option '{}'",
                        TOOL_NAME,
                        arg.to_string_lossy()
                    );
                    eprintln!("Try '{} --help' for more information.", TOOL_NAME);
                    process::exit(1);
                }
            }
        } else if bytes.len() > 1 && bytes[0] == b'-' {
            for &b in &bytes[1..] {
                match b {
                    b'b' => cli.binary = true,
                    b'c' => cli.check = true,
                    b't' => cli.text = true,
                    b'w' => cli.warn = true,
                    b'z' => cli.zero = true,
                    _ => {
                        eprintln!("{}: invalid option -- '{}'", TOOL_NAME, b as char);
                        eprintln!("Try '{} --help' for more information.", TOOL_NAME);
                        process::exit(1);
                    }
                }
            }
        } else {
            cli.files.push(arg.to_string_lossy().into_owned());
        }
    }

    cli
}

// ── Filename escaping (GNU compat) ─────────────────────────────────

/// Check if a filename needs escaping (contains backslash or newline).
fn needs_escape(name: &str) -> bool {
    name.bytes().any(|b| b == b'\\' || b == b'\n')
}

/// Escape a filename: replace `\` with `\\` and newline with `\n` (literal).
fn escape_filename(name: &str) -> String {
    let mut out = String::with_capacity(name.len() + 8);
    for b in name.bytes() {
        match b {
            b'\\' => out.push_str("\\\\"),
            b'\n' => out.push_str("\\n"),
            _ => out.push(b as char),
        }
    }
    out
}

/// Unescape a checksum-line filename: `\\` -> `\`, `\n` -> newline.
fn unescape_filename(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut chars = s.chars();
    while let Some(c) = chars.next() {
        if c == '\\' {
            match chars.next() {
                Some('\\') => out.push('\\'),
                Some('n') => out.push('\n'),
                Some(other) => {
                    out.push('\\');
                    out.push(other);
                }
                None => out.push('\\'),
            }
        } else {
            out.push(c);
        }
    }
    out
}

/// Enlarge pipe buffers on Linux for higher throughput.
#[cfg(target_os = "linux")]
fn enlarge_pipes() {
    const PIPE_SIZE: i32 = 8 * 1024 * 1024;
    unsafe {
        libc::fcntl(0, libc::F_SETPIPE_SZ, PIPE_SIZE);
        libc::fcntl(1, libc::F_SETPIPE_SZ, PIPE_SIZE);
    }
}

/// Ultra-fast path for `fsha224sum <single_file>` — raw syscalls, zero allocation.
#[cfg(target_os = "linux")]
fn single_file_fast(path: &Path) -> ! {
    let mut out_buf = [0u8; 4096];
    match hash::hash_file_raw_to_buf(HashAlgorithm::Sha224, path, &mut out_buf) {
        Ok(hex_len) => {
            let name_bytes = {
                use std::os::unix::ffi::OsStrExt;
                path.as_os_str().as_bytes()
            };
            let mut pos = hex_len;
            out_buf[pos] = b' ';
            pos += 1;
            out_buf[pos] = b' ';
            pos += 1;
            if pos + name_bytes.len() < out_buf.len() {
                out_buf[pos..pos + name_bytes.len()].copy_from_slice(name_bytes);
                pos += name_bytes.len();
                out_buf[pos] = b'\n';
                pos += 1;
                unsafe {
                    libc::write(1, out_buf.as_ptr() as *const libc::c_void, pos as _);
                }
            } else {
                let mut v = Vec::with_capacity(pos + name_bytes.len() + 1);
                v.extend_from_slice(&out_buf[..pos]);
                v.extend_from_slice(name_bytes);
                v.push(b'\n');
                unsafe {
                    libc::write(1, v.as_ptr() as *const libc::c_void, v.len() as _);
                }
            }
            process::exit(0);
        }
        Err(e) => {
            eprintln!(
                "{}: {}: {}",
                TOOL_NAME,
                path.to_string_lossy(),
                io_error_msg(&e)
            );
            process::exit(1);
        }
    }
}

fn main() {
    coreutils_rs::common::reset_sigpipe();

    #[cfg(target_os = "linux")]
    {
        let mut args = std::env::args_os();
        let _ = args.next();
        if let Some(arg) = args.next()
            && args.next().is_none()
        {
            let bytes = arg.as_encoded_bytes();
            if !bytes.is_empty() && bytes[0] != b'-' {
                single_file_fast(Path::new(&arg));
            }
        }
    }

    let cli = parse_args();
    let algo = HashAlgorithm::Sha224;

    // Validate flag combinations
    if cli.tag && cli.check {
        eprintln!(
            "{}: the --tag option is meaningless when verifying checksums",
            TOOL_NAME
        );
        eprintln!("Try '{} --help' for more information.", TOOL_NAME);
        process::exit(1);
    }

    let files = if cli.files.is_empty() {
        vec!["-".to_string()]
    } else {
        cli.files.clone()
    };

    #[cfg(target_os = "linux")]
    if files.iter().any(|f| f == "-") {
        enlarge_pipes();
    }

    // Raw fd stdout on Unix for zero-overhead writes
    #[cfg(unix)]
    let mut raw = unsafe { ManuallyDrop::new(std::fs::File::from_raw_fd(1)) };
    #[cfg(unix)]
    let mut out = BufWriter::with_capacity(256 * 1024, &mut *raw);
    #[cfg(not(unix))]
    let stdout = io::stdout();
    #[cfg(not(unix))]
    let mut out = BufWriter::with_capacity(256 * 1024, stdout.lock());
    let mut had_error = false;

    if cli.check {
        run_check_mode(&cli, algo, &files, &mut out, &mut had_error);
    } else {
        run_hash_mode(&cli, algo, &files, &mut out, &mut had_error);
    }

    let _ = out.flush();
    if had_error {
        process::exit(1);
    }
}

fn run_hash_mode(
    cli: &Cli,
    algo: HashAlgorithm,
    files: &[String],
    out: &mut impl Write,
    had_error: &mut bool,
) {
    let has_stdin = files.iter().any(|f| f == "-");

    if has_stdin || files.len() <= 1 {
        for filename in files {
            let hash_result = if filename == "-" {
                hash::hash_stdin(algo)
            } else {
                hash::hash_file(algo, Path::new(filename))
            };

            match hash_result {
                Ok(h) => {
                    let name = if filename == "-" {
                        "-"
                    } else {
                        filename.as_str()
                    };
                    write_output(out, cli, algo, &h, name);
                }
                Err(e) => {
                    let _ = out.flush();
                    eprintln!("{}: {}: {}", TOOL_NAME, filename, io_error_msg(&e));
                    *had_error = true;
                }
            }
        }
    } else {
        let paths: Vec<_> = files.iter().map(|f| Path::new(f.as_str())).collect();
        let results = hash::hash_files_auto(&paths, algo);

        for (filename, result) in files.iter().zip(results) {
            match result {
                Ok(h) => {
                    write_output(out, cli, algo, &h, filename);
                }
                Err(e) => {
                    let _ = out.flush();
                    eprintln!("{}: {}: {}", TOOL_NAME, filename, io_error_msg(&e));
                    *had_error = true;
                }
            }
        }
    }
}

/// Write hash output using single-write batched buffer for minimum overhead.
#[inline]
fn write_output(out: &mut impl Write, cli: &Cli, algo: HashAlgorithm, hash: &str, filename: &str) {
    let binary = cli.binary || (!cli.text && cfg!(windows));
    if cli.tag {
        let _ = hash::write_hash_tag_line(out, algo.name(), hash, filename, cli.zero);
    } else if !cli.zero && needs_escape(filename) {
        let escaped = escape_filename(filename);
        let _ = hash::write_hash_line(out, hash, &escaped, binary, cli.zero, true);
    } else {
        let _ = hash::write_hash_line(out, hash, filename, binary, cli.zero, false);
    }
}

fn run_check_mode(
    cli: &Cli,
    algo: HashAlgorithm,
    files: &[String],
    out: &mut impl Write,
    had_error: &mut bool,
) {
    let mut _total_ok: usize = 0;
    let mut total_mismatches: usize = 0;
    let mut total_fmt_errors: usize = 0;
    let mut total_read_errors: usize = 0;

    for filename in files {
        let reader: Box<dyn io::BufRead> = if filename == "-" {
            Box::new(BufReader::new(io::stdin().lock()))
        } else {
            match std::fs::File::open(filename) {
                Ok(f) => Box::new(BufReader::new(f)),
                Err(e) => {
                    eprintln!("{}: {}: {}", TOOL_NAME, filename, io_error_msg(&e));
                    *had_error = true;
                    continue;
                }
            }
        };

        let display_name = if filename == "-" {
            "standard input".to_string()
        } else {
            filename.clone()
        };

        let (file_ok, file_fail, file_fmt, file_read, file_ignored) =
            check_one(cli, algo, reader, &display_name, out);

        _total_ok += file_ok;
        total_mismatches += file_fail;
        total_fmt_errors += file_fmt;
        total_read_errors += file_read;

        if file_fail > 0 || file_read > 0 {
            *had_error = true;
        }
        if cli.strict && file_fmt > 0 {
            *had_error = true;
        }

        if file_ok == 0 && file_fail == 0 && file_read == 0 && file_ignored == 0 && file_fmt > 0 {
            if !cli.status {
                let _ = out.flush();
                eprintln!(
                    "{}: {}: no properly formatted SHA224 checksum lines found",
                    TOOL_NAME, display_name
                );
            }
            total_fmt_errors -= file_fmt;
            *had_error = true;
        }

        if cli.ignore_missing && file_ok == 0 && file_fail == 0 && file_ignored > 0 {
            if !cli.status {
                let _ = out.flush();
                eprintln!("{}: {}: no file was verified", TOOL_NAME, display_name);
            }
            *had_error = true;
        }
    }

    let _ = out.flush();

    if !cli.status {
        if total_mismatches > 0 {
            let checksum_word = if total_mismatches == 1 {
                "computed checksum did NOT match"
            } else {
                "computed checksums did NOT match"
            };
            eprintln!(
                "{}: WARNING: {} {}",
                TOOL_NAME, total_mismatches, checksum_word
            );
        }

        if total_read_errors > 0 {
            let word = if total_read_errors == 1 {
                "listed file could not be read"
            } else {
                "listed files could not be read"
            };
            eprintln!("{}: WARNING: {} {}", TOOL_NAME, total_read_errors, word);
        }

        if total_fmt_errors > 0 {
            let line_word = if total_fmt_errors == 1 {
                "line is"
            } else {
                "lines are"
            };
            eprintln!(
                "{}: WARNING: {} {} improperly formatted",
                TOOL_NAME, total_fmt_errors, line_word
            );
        }
    }
}

/// Check checksums from one input source. Returns (ok, fail, fmt_errors, read_errors, ignored_missing).
fn check_one(
    cli: &Cli,
    algo: HashAlgorithm,
    reader: Box<dyn BufRead>,
    display_name: &str,
    out: &mut impl Write,
) -> (usize, usize, usize, usize, usize) {
    let mut ok_count: usize = 0;
    let mut mismatch_count: usize = 0;
    let mut format_errors: usize = 0;
    let mut read_errors: usize = 0;
    let mut ignored_missing: usize = 0;
    let mut line_num: usize = 0;

    for line_result in reader.lines() {
        line_num += 1;
        let line = match line_result {
            Ok(l) => l,
            Err(e) => {
                eprintln!("{}: {}: {}", TOOL_NAME, display_name, io_error_msg(&e));
                break;
            }
        };
        let line = line.trim_end();

        if line.is_empty() {
            continue;
        }

        let line_content = line.strip_prefix('\\').unwrap_or(line);

        let (expected_hash, parsed_filename) = match hash::parse_check_line(line_content) {
            Some(v) => v,
            None => {
                format_errors += 1;
                if cli.warn {
                    let _ = out.flush();
                    eprintln!(
                        "{}: {}: {}: improperly formatted SHA224 checksum line",
                        TOOL_NAME, display_name, line_num
                    );
                }
                continue;
            }
        };

        if expected_hash.len() != SHA224_HEX_LEN
            || !expected_hash.bytes().all(|b| b.is_ascii_hexdigit())
        {
            format_errors += 1;
            if cli.warn {
                let _ = out.flush();
                eprintln!(
                    "{}: {}: {}: improperly formatted SHA224 checksum line",
                    TOOL_NAME, display_name, line_num
                );
            }
            continue;
        }

        let check_filename = if line.starts_with('\\') {
            unescape_filename(parsed_filename)
        } else {
            parsed_filename.to_string()
        };

        let actual = match hash::hash_file(algo, Path::new(&check_filename)) {
            Ok(h) => h,
            Err(e) => {
                if cli.ignore_missing && e.kind() == io::ErrorKind::NotFound {
                    ignored_missing += 1;
                    continue;
                }
                read_errors += 1;
                if !cli.status {
                    let _ = out.flush();
                    eprintln!("{}: {}: {}", TOOL_NAME, check_filename, io_error_msg(&e));
                    let _ = writeln!(out, "{}: FAILED open or read", check_filename);
                }
                continue;
            }
        };

        if actual.eq_ignore_ascii_case(expected_hash) {
            ok_count += 1;
            if !cli.quiet && !cli.status {
                let _ = writeln!(out, "{}: OK", check_filename);
            }
        } else {
            mismatch_count += 1;
            if !cli.status {
                let _ = writeln!(out, "{}: FAILED", check_filename);
            }
        }
    }

    (
        ok_count,
        mismatch_count,
        format_errors,
        read_errors,
        ignored_missing,
    )
}

#[cfg(test)]
mod tests {
    use std::process::Command;

    fn cmd() -> Command {
        let mut path = std::env::current_exe().unwrap();
        path.pop();
        path.pop();
        path.push("fsha224sum");
        Command::new(path)
    }
    #[cfg(unix)]
    #[test]
    fn test_hash_stdin() {
        use std::io::Write;
        use std::process::Stdio;
        let mut child = cmd()
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()
            .unwrap();
        child.stdin.take().unwrap().write_all(b"hello\n").unwrap();
        let output = child.wait_with_output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stdout = stdout.trim();
        assert!(stdout.contains("  -"), "Should contain filename marker");
    }

    #[test]
    fn test_hash_file() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.txt");
        std::fs::write(&file, "hello\n").unwrap();
        let output = cmd().arg(file.to_str().unwrap()).output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("test.txt"));
    }

    #[test]
    fn test_check_mode() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.txt");
        std::fs::write(&file, "hello\n").unwrap();
        let output = cmd().arg(file.to_str().unwrap()).output().unwrap();
        let checksum_line = String::from_utf8_lossy(&output.stdout);
        let checksums = dir.path().join("checksums.txt");
        std::fs::write(&checksums, checksum_line.as_ref()).unwrap();
        let output = cmd()
            .args(["--check", checksums.to_str().unwrap()])
            .output()
            .unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("OK"));
    }

    #[test]
    fn test_known_empty_hash() {
        use std::process::Stdio;
        let mut child = cmd()
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()
            .unwrap();
        drop(child.stdin.take().unwrap());
        let output = child.wait_with_output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        // SHA-224 of empty input
        assert!(stdout.contains("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"));
    }

    #[test]
    fn test_nonexistent_file() {
        let output = cmd().arg("/nonexistent_xyz_sha224").output().unwrap();
        assert!(!output.status.success());
    }

    #[test]
    fn test_check_tampered() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.txt");
        std::fs::write(&file, "original\n").unwrap();
        let output = cmd().arg(file.to_str().unwrap()).output().unwrap();
        let checksums = dir.path().join("sums.txt");
        std::fs::write(&checksums, String::from_utf8_lossy(&output.stdout).as_ref()).unwrap();
        std::fs::write(&file, "tampered\n").unwrap();
        let output = cmd()
            .args(["--check", checksums.to_str().unwrap()])
            .output()
            .unwrap();
        assert!(!output.status.success());
    }

    #[test]
    fn test_tag_format() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.txt");
        std::fs::write(&file, "x").unwrap();
        let output = cmd()
            .args(["--tag", file.to_str().unwrap()])
            .output()
            .unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("SHA224"));
    }

    #[test]
    fn test_multiple_files() {
        let dir = tempfile::tempdir().unwrap();
        let f1 = dir.path().join("a.txt");
        let f2 = dir.path().join("b.txt");
        std::fs::write(&f1, "aaa\n").unwrap();
        std::fs::write(&f2, "bbb\n").unwrap();
        let output = cmd()
            .args([f1.to_str().unwrap(), f2.to_str().unwrap()])
            .output()
            .unwrap();
        assert!(output.status.success());
        assert_eq!(String::from_utf8_lossy(&output.stdout).lines().count(), 2);
    }

    #[test]
    fn test_invalid_option() {
        let output = cmd().arg("--invalid-xyz").output().unwrap();
        assert!(!output.status.success());
    }

    #[cfg(unix)]
    #[test]
    fn test_hash_length() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.txt");
        std::fs::write(&file, "test").unwrap();
        let output = cmd().arg(file.to_str().unwrap()).output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stdout = stdout.trim();
        let hash_part: &str = stdout.split_whitespace().next().unwrap();
        assert_eq!(hash_part.len(), 56); // SHA224 = 56 hex chars
    }
}