diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 312e4355f..1493ce452 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -147,6 +147,7 @@ struct Options { untagged: bool, length: Option, output_format: OutputFormat, + binary: bool, } /// Calculate checksum @@ -170,6 +171,8 @@ where let stdin_buf; let file_buf; let not_file = filename == OsStr::new("-"); + + // Handle the file input let mut file = BufReader::new(if not_file { stdin_buf = stdin(); Box::new(stdin_buf) as Box @@ -185,6 +188,7 @@ where }; Box::new(file_buf) as Box }); + let (sum_hex, sz) = digest_read(&mut options.digest, &mut file, options.output_bits) .map_err_context(|| "failed to read input".to_string())?; if filename.is_dir() { @@ -250,7 +254,8 @@ where } _ => { if options.untagged { - println!("{sum} {}", filename.display()); + let prefix = if options.binary { "*" } else { " " }; + println!("{sum} {prefix}{}", filename.display()); } else { println!( "{} ({}) = {sum}", @@ -306,6 +311,10 @@ mod options { pub const LENGTH: &str = "length"; pub const RAW: &str = "raw"; pub const BASE64: &str = "base64"; + pub const CHECK: &str = "check"; + // for legacy compat reasons + pub const TEXT: &str = "text"; + pub const BINARY: &str = "binary"; } #[uucore::main] @@ -375,6 +384,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { length, untagged: matches.get_flag(options::UNTAGGED), output_format, + binary: matches.get_flag(options::BINARY), }; match matches.get_many::(options::FILE) { @@ -448,15 +458,42 @@ pub fn uu_app() -> Command { .help("emit a raw binary digest, not hexadecimal") .action(ArgAction::SetTrue), ) + /*.arg( + Arg::new(options::STRICT) + .long(options::STRICT) + .help("exit non-zero for improperly formatted checksum lines") + .action(ArgAction::SetTrue), + )*/ + .arg( + Arg::new(options::CHECK) + .short('c') + .long(options::CHECK) + .help("read hashsums from the FILEs and check them") + .action(ArgAction::SetTrue) + .conflicts_with("tag"), + ) .arg( Arg::new(options::BASE64) .long(options::BASE64) - .short('b') .help("emit a base64 digest, not hexadecimal") .action(ArgAction::SetTrue) // Even though this could easily just override an earlier '--raw', // GNU cksum does not permit these flags to be combined: .conflicts_with(options::RAW), ) + .arg( + Arg::new(options::TEXT) + .long(options::TEXT) + .short('t') + .hide(true) // for legacy compatibility, no action + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new(options::BINARY) + .long(options::BINARY) + .short('b') + .hide(true) // for legacy compatibility, no action + .action(ArgAction::SetTrue), + ) .after_help(AFTER_HELP) } diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index 71b32bdfa..112274baf 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -231,6 +231,16 @@ fn test_untagged_algorithm_single_file() { } } +#[test] +fn test_tag_short() { + new_ucmd!() + .arg("-t") + .arg("--algorithm=md5") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_is("MD5 (lorem_ipsum.txt) = cd724690f7dc61775dfac400a71f2caa\n"); +} + #[test] fn test_untagged_algorithm_after_tag() { new_ucmd!() @@ -379,15 +389,13 @@ fn test_base64_raw_conflicts() { #[test] fn test_base64_single_file() { for algo in ALGOS { - for base64_option in ["--base64", "-b"] { - new_ucmd!() - .arg(base64_option) - .arg("lorem_ipsum.txt") - .arg(format!("--algorithm={algo}")) - .succeeds() - .no_stderr() - .stdout_is_fixture_bytes(format!("base64/{algo}_single_file.expected")); - } + new_ucmd!() + .arg("--base64") + .arg("lorem_ipsum.txt") + .arg(format!("--algorithm={algo}")) + .succeeds() + .no_stderr() + .stdout_is_fixture_bytes(format!("base64/{algo}_single_file.expected")); } } #[test] @@ -435,6 +443,23 @@ fn test_all_algorithms_fail_on_folder() { } } +#[test] +fn test_binary_file() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("f"); + + scene + .ucmd() + .arg("--untagged") + .arg("-b") + .arg("--algorithm=md5") + .arg(at.subdir.join("f")) + .succeeds() + .stdout_contains("d41d8cd98f00b204e9800998ecf8427e *"); +} + #[test] fn test_folder_and_file() { let scene = TestScenario::new(util_name!());