diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 5a8cec5ea60..a46f69302a2 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -6,7 +6,7 @@ // file that was distributed with this source code. // spell-checker:ignore (ToDO) fname, algo -use clap::{crate_version, Arg, Command}; +use clap::{crate_version, Arg, ArgAction, Command}; use hex::encode; use std::ffi::OsStr; use std::fs::File; @@ -103,6 +103,7 @@ struct Options { algo_name: &'static str, digest: Box, output_bits: usize, + untagged: bool, } /// Calculate checksum @@ -161,12 +162,20 @@ where ), (ALGORITHM_OPTIONS_CRC, true) => println!("{sum} {sz}"), (ALGORITHM_OPTIONS_CRC, false) => println!("{sum} {sz} {}", filename.display()), - (ALGORITHM_OPTIONS_BLAKE2B, _) => println!("BLAKE2b ({}) = {sum}", filename.display()), - _ => println!( - "{} ({}) = {sum}", - options.algo_name.to_ascii_uppercase(), - filename.display() - ), + (ALGORITHM_OPTIONS_BLAKE2B, _) if !options.untagged => { + println!("BLAKE2b ({}) = {sum}", filename.display()); + } + _ => { + if options.untagged { + println!("{sum} {}", filename.display()); + } else { + println!( + "{} ({}) = {sum}", + options.algo_name.to_ascii_uppercase(), + filename.display() + ); + } + } } } @@ -208,8 +217,9 @@ fn digest_read( } mod options { - pub static FILE: &str = "file"; - pub static ALGORITHM: &str = "algorithm"; + pub const ALGORITHM: &str = "algorithm"; + pub const FILE: &str = "file"; + pub const UNTAGGED: &str = "untagged"; } #[uucore::main] @@ -228,6 +238,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { algo_name: name, digest: algo, output_bits: bits, + untagged: matches.get_flag(options::UNTAGGED), }; match matches.get_many::(options::FILE) { @@ -270,5 +281,11 @@ pub fn uu_app() -> Command { ALGORITHM_OPTIONS_SM3, ]), ) + .arg( + Arg::new(options::UNTAGGED) + .long(options::UNTAGGED) + .help("create a reversed style checksum, without digest type") + .action(ArgAction::SetTrue), + ) .after_help(AFTER_HELP) } diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index 28c978ba6d9..41ddc2ee0b1 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -159,3 +159,68 @@ fn test_algorithm_stdin() { } } } + +#[test] +fn test_untagged_single_file() { + new_ucmd!() + .arg("--untagged") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_is_fixture("untagged/crc_single_file.expected"); +} + +#[test] +fn test_untagged_multiple_files() { + new_ucmd!() + .arg("--untagged") + .arg("lorem_ipsum.txt") + .arg("alice_in_wonderland.txt") + .succeeds() + .stdout_is_fixture("untagged/crc_multiple_files.expected"); +} + +#[test] +fn test_untagged_stdin() { + new_ucmd!() + .arg("--untagged") + .pipe_in_fixture("lorem_ipsum.txt") + .succeeds() + .stdout_is_fixture("untagged/crc_stdin.expected"); +} + +#[test] +fn test_untagged_algorithm_single_file() { + for algo in ALGOS { + new_ucmd!() + .arg("--untagged") + .arg(format!("--algorithm={algo}")) + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_is_fixture(format!("untagged/{algo}_single_file.expected")); + } +} + +#[test] +fn test_untagged_algorithm_multiple_files() { + for algo in ALGOS { + new_ucmd!() + .arg("--untagged") + .arg(format!("--algorithm={algo}")) + .arg("lorem_ipsum.txt") + .arg("alice_in_wonderland.txt") + .succeeds() + .stdout_is_fixture(format!("untagged/{algo}_multiple_files.expected")); + } +} + +#[test] +fn test_untagged_algorithm_stdin() { + for algo in ALGOS { + new_ucmd!() + .arg("--untagged") + .arg(format!("--algorithm={algo}")) + .pipe_in_fixture("lorem_ipsum.txt") + .succeeds() + .stdout_is_fixture(format!("untagged/{algo}_stdin.expected")); + } +} diff --git a/tests/fixtures/cksum/untagged/blake2b_multiple_files.expected b/tests/fixtures/cksum/untagged/blake2b_multiple_files.expected new file mode 100644 index 00000000000..7c68c857332 --- /dev/null +++ b/tests/fixtures/cksum/untagged/blake2b_multiple_files.expected @@ -0,0 +1,2 @@ +0e97a09189e560c3789c0bff1f020166861ef857d1fbfe4574de1842e3c06cabb9575e4af6309a166158c2b408d3c038c1b49d828b35158142cdc0396d1195c3 lorem_ipsum.txt +91b8b0f0868e905ad18b8ac35e4a1dacd289857b19258ab5d1e071761af758b0134ec152d4f011fe1825ca889c80c2e072ca70eb50548c25fc49a98937515af4 alice_in_wonderland.txt diff --git a/tests/fixtures/cksum/untagged/blake2b_single_file.expected b/tests/fixtures/cksum/untagged/blake2b_single_file.expected new file mode 100644 index 00000000000..1f5444c87cd --- /dev/null +++ b/tests/fixtures/cksum/untagged/blake2b_single_file.expected @@ -0,0 +1 @@ +0e97a09189e560c3789c0bff1f020166861ef857d1fbfe4574de1842e3c06cabb9575e4af6309a166158c2b408d3c038c1b49d828b35158142cdc0396d1195c3 lorem_ipsum.txt diff --git a/tests/fixtures/cksum/untagged/blake2b_stdin.expected b/tests/fixtures/cksum/untagged/blake2b_stdin.expected new file mode 100644 index 00000000000..0892bff36dc --- /dev/null +++ b/tests/fixtures/cksum/untagged/blake2b_stdin.expected @@ -0,0 +1 @@ +0e97a09189e560c3789c0bff1f020166861ef857d1fbfe4574de1842e3c06cabb9575e4af6309a166158c2b408d3c038c1b49d828b35158142cdc0396d1195c3 - diff --git a/tests/fixtures/cksum/untagged/bsd_multiple_files.expected b/tests/fixtures/cksum/untagged/bsd_multiple_files.expected new file mode 100644 index 00000000000..941a2a512f8 --- /dev/null +++ b/tests/fixtures/cksum/untagged/bsd_multiple_files.expected @@ -0,0 +1,2 @@ +08109 1 lorem_ipsum.txt +01814 1 alice_in_wonderland.txt diff --git a/tests/fixtures/cksum/untagged/bsd_single_file.expected b/tests/fixtures/cksum/untagged/bsd_single_file.expected new file mode 100644 index 00000000000..293ada3bd61 --- /dev/null +++ b/tests/fixtures/cksum/untagged/bsd_single_file.expected @@ -0,0 +1 @@ +08109 1 lorem_ipsum.txt diff --git a/tests/fixtures/cksum/untagged/bsd_stdin.expected b/tests/fixtures/cksum/untagged/bsd_stdin.expected new file mode 100644 index 00000000000..4843ba08246 --- /dev/null +++ b/tests/fixtures/cksum/untagged/bsd_stdin.expected @@ -0,0 +1 @@ +08109 1 diff --git a/tests/fixtures/cksum/untagged/crc_multiple_files.expected b/tests/fixtures/cksum/untagged/crc_multiple_files.expected new file mode 100644 index 00000000000..d7a4f5b4f3b --- /dev/null +++ b/tests/fixtures/cksum/untagged/crc_multiple_files.expected @@ -0,0 +1,2 @@ +378294376 772 lorem_ipsum.txt +3805907707 302 alice_in_wonderland.txt diff --git a/tests/fixtures/cksum/untagged/crc_single_file.expected b/tests/fixtures/cksum/untagged/crc_single_file.expected new file mode 100644 index 00000000000..e9fc1ca7cf4 --- /dev/null +++ b/tests/fixtures/cksum/untagged/crc_single_file.expected @@ -0,0 +1 @@ +378294376 772 lorem_ipsum.txt diff --git a/tests/fixtures/cksum/untagged/crc_stdin.expected b/tests/fixtures/cksum/untagged/crc_stdin.expected new file mode 100644 index 00000000000..28b37d0bed1 --- /dev/null +++ b/tests/fixtures/cksum/untagged/crc_stdin.expected @@ -0,0 +1 @@ +378294376 772 diff --git a/tests/fixtures/cksum/untagged/md5_multiple_files.expected b/tests/fixtures/cksum/untagged/md5_multiple_files.expected new file mode 100644 index 00000000000..4b63cbff7fd --- /dev/null +++ b/tests/fixtures/cksum/untagged/md5_multiple_files.expected @@ -0,0 +1,2 @@ +cd724690f7dc61775dfac400a71f2caa lorem_ipsum.txt +f6fa7033e16166a9589aa1c0388ffd58 alice_in_wonderland.txt diff --git a/tests/fixtures/cksum/untagged/md5_single_file.expected b/tests/fixtures/cksum/untagged/md5_single_file.expected new file mode 100644 index 00000000000..ca9eb678596 --- /dev/null +++ b/tests/fixtures/cksum/untagged/md5_single_file.expected @@ -0,0 +1 @@ +cd724690f7dc61775dfac400a71f2caa lorem_ipsum.txt diff --git a/tests/fixtures/cksum/untagged/md5_stdin.expected b/tests/fixtures/cksum/untagged/md5_stdin.expected new file mode 100644 index 00000000000..f4094dcc7e0 --- /dev/null +++ b/tests/fixtures/cksum/untagged/md5_stdin.expected @@ -0,0 +1 @@ +cd724690f7dc61775dfac400a71f2caa - diff --git a/tests/fixtures/cksum/untagged/sha1_multiple_files.expected b/tests/fixtures/cksum/untagged/sha1_multiple_files.expected new file mode 100644 index 00000000000..1712c740911 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha1_multiple_files.expected @@ -0,0 +1,2 @@ +ab1dd0bae1d8883a3d18a66de6afbd28252cfbef lorem_ipsum.txt +22b54b2520e8b4fa59eb10719028a4e587c12d1e alice_in_wonderland.txt diff --git a/tests/fixtures/cksum/untagged/sha1_single_file.expected b/tests/fixtures/cksum/untagged/sha1_single_file.expected new file mode 100644 index 00000000000..c8e8ddca6f9 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha1_single_file.expected @@ -0,0 +1 @@ +ab1dd0bae1d8883a3d18a66de6afbd28252cfbef lorem_ipsum.txt diff --git a/tests/fixtures/cksum/untagged/sha1_stdin.expected b/tests/fixtures/cksum/untagged/sha1_stdin.expected new file mode 100644 index 00000000000..2396ed481ec --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha1_stdin.expected @@ -0,0 +1 @@ +ab1dd0bae1d8883a3d18a66de6afbd28252cfbef - diff --git a/tests/fixtures/cksum/untagged/sha224_multiple_files.expected b/tests/fixtures/cksum/untagged/sha224_multiple_files.expected new file mode 100644 index 00000000000..3eb2b500a00 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha224_multiple_files.expected @@ -0,0 +1,2 @@ +3de66fbcad106e1b40ab391be56c51d2007eb1f9c655d0f4e29bfc01 lorem_ipsum.txt +54c9c7d78458886418ce0845111fc49fe1c628ffd4bf3da14226ffd9 alice_in_wonderland.txt diff --git a/tests/fixtures/cksum/untagged/sha224_single_file.expected b/tests/fixtures/cksum/untagged/sha224_single_file.expected new file mode 100644 index 00000000000..c5ab1f7d143 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha224_single_file.expected @@ -0,0 +1 @@ +3de66fbcad106e1b40ab391be56c51d2007eb1f9c655d0f4e29bfc01 lorem_ipsum.txt diff --git a/tests/fixtures/cksum/untagged/sha224_stdin.expected b/tests/fixtures/cksum/untagged/sha224_stdin.expected new file mode 100644 index 00000000000..691e451b373 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha224_stdin.expected @@ -0,0 +1 @@ +3de66fbcad106e1b40ab391be56c51d2007eb1f9c655d0f4e29bfc01 - diff --git a/tests/fixtures/cksum/untagged/sha256_multiple_files.expected b/tests/fixtures/cksum/untagged/sha256_multiple_files.expected new file mode 100644 index 00000000000..a57fa2eaf1b --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha256_multiple_files.expected @@ -0,0 +1,2 @@ +f7c420501c50e00b309250100d67ea5e910981536b4582fe9c435bd92b3f1f02 lorem_ipsum.txt +14ab7e5a0aa3a670222744714bc96961d51012cb216225d965db71824a46e5fe alice_in_wonderland.txt diff --git a/tests/fixtures/cksum/untagged/sha256_single_file.expected b/tests/fixtures/cksum/untagged/sha256_single_file.expected new file mode 100644 index 00000000000..1b1be9516be --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha256_single_file.expected @@ -0,0 +1 @@ +f7c420501c50e00b309250100d67ea5e910981536b4582fe9c435bd92b3f1f02 lorem_ipsum.txt diff --git a/tests/fixtures/cksum/untagged/sha256_stdin.expected b/tests/fixtures/cksum/untagged/sha256_stdin.expected new file mode 100644 index 00000000000..998db6c664b --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha256_stdin.expected @@ -0,0 +1 @@ +f7c420501c50e00b309250100d67ea5e910981536b4582fe9c435bd92b3f1f02 - diff --git a/tests/fixtures/cksum/untagged/sha384_multiple_files.expected b/tests/fixtures/cksum/untagged/sha384_multiple_files.expected new file mode 100644 index 00000000000..d309034b4af --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha384_multiple_files.expected @@ -0,0 +1,2 @@ +4be4b90a0d0d32966992921019f24abc824dcfb8b1c408102f1f6788fb80ba9a9a4c5a7b575a3353a90a8ee719481dcb lorem_ipsum.txt +b7966c97ef84ab5858db2e0cdd33fbaf4fa8346d84de65aba001e738c242598a43272854d0073ad1099404eaa1d93766 alice_in_wonderland.txt diff --git a/tests/fixtures/cksum/untagged/sha384_single_file.expected b/tests/fixtures/cksum/untagged/sha384_single_file.expected new file mode 100644 index 00000000000..88d4da57726 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha384_single_file.expected @@ -0,0 +1 @@ +4be4b90a0d0d32966992921019f24abc824dcfb8b1c408102f1f6788fb80ba9a9a4c5a7b575a3353a90a8ee719481dcb lorem_ipsum.txt diff --git a/tests/fixtures/cksum/untagged/sha384_stdin.expected b/tests/fixtures/cksum/untagged/sha384_stdin.expected new file mode 100644 index 00000000000..cde20b78b93 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha384_stdin.expected @@ -0,0 +1 @@ +4be4b90a0d0d32966992921019f24abc824dcfb8b1c408102f1f6788fb80ba9a9a4c5a7b575a3353a90a8ee719481dcb - diff --git a/tests/fixtures/cksum/untagged/sha512_multiple_files.expected b/tests/fixtures/cksum/untagged/sha512_multiple_files.expected new file mode 100644 index 00000000000..a5dafa7c329 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha512_multiple_files.expected @@ -0,0 +1,2 @@ +965464ab2556aad58ebc73d89ad221e559797529ecafc0f466c11795cff6d6e2c60f96a07c542cfd1f426e5e4fe0a48aa15667ba44096b213d0813cd038dfa05 lorem_ipsum.txt +251646d5a7eb481e0f3aced7839d78dd5e97153f822dc55938e17059c485990d85d602e2881b528b565ab6262584a69c97b068b26bda81acc9356c53c7c1c96d alice_in_wonderland.txt diff --git a/tests/fixtures/cksum/untagged/sha512_single_file.expected b/tests/fixtures/cksum/untagged/sha512_single_file.expected new file mode 100644 index 00000000000..adea498d609 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha512_single_file.expected @@ -0,0 +1 @@ +965464ab2556aad58ebc73d89ad221e559797529ecafc0f466c11795cff6d6e2c60f96a07c542cfd1f426e5e4fe0a48aa15667ba44096b213d0813cd038dfa05 lorem_ipsum.txt diff --git a/tests/fixtures/cksum/untagged/sha512_stdin.expected b/tests/fixtures/cksum/untagged/sha512_stdin.expected new file mode 100644 index 00000000000..dd9c968438a --- /dev/null +++ b/tests/fixtures/cksum/untagged/sha512_stdin.expected @@ -0,0 +1 @@ +965464ab2556aad58ebc73d89ad221e559797529ecafc0f466c11795cff6d6e2c60f96a07c542cfd1f426e5e4fe0a48aa15667ba44096b213d0813cd038dfa05 - diff --git a/tests/fixtures/cksum/untagged/sm3_multiple_files.expected b/tests/fixtures/cksum/untagged/sm3_multiple_files.expected new file mode 100644 index 00000000000..de12ab0b9b6 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sm3_multiple_files.expected @@ -0,0 +1,2 @@ +6d296b805d060bfed22808df308dbb9b4317794dd4ed6740a10770a782699bc2 lorem_ipsum.txt +d66617ae3c4e87828298dcd836f79efbab488c53b84e09c3e8e83a16c902418d alice_in_wonderland.txt diff --git a/tests/fixtures/cksum/untagged/sm3_single_file.expected b/tests/fixtures/cksum/untagged/sm3_single_file.expected new file mode 100644 index 00000000000..54d5f40d233 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sm3_single_file.expected @@ -0,0 +1 @@ +6d296b805d060bfed22808df308dbb9b4317794dd4ed6740a10770a782699bc2 lorem_ipsum.txt diff --git a/tests/fixtures/cksum/untagged/sm3_stdin.expected b/tests/fixtures/cksum/untagged/sm3_stdin.expected new file mode 100644 index 00000000000..6ba002b45b6 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sm3_stdin.expected @@ -0,0 +1 @@ +6d296b805d060bfed22808df308dbb9b4317794dd4ed6740a10770a782699bc2 - diff --git a/tests/fixtures/cksum/untagged/sysv_multiple_files.expected b/tests/fixtures/cksum/untagged/sysv_multiple_files.expected new file mode 100644 index 00000000000..83a6d6d839f --- /dev/null +++ b/tests/fixtures/cksum/untagged/sysv_multiple_files.expected @@ -0,0 +1,2 @@ +6985 2 lorem_ipsum.txt +27441 1 alice_in_wonderland.txt diff --git a/tests/fixtures/cksum/untagged/sysv_single_file.expected b/tests/fixtures/cksum/untagged/sysv_single_file.expected new file mode 100644 index 00000000000..e0f7252cbe8 --- /dev/null +++ b/tests/fixtures/cksum/untagged/sysv_single_file.expected @@ -0,0 +1 @@ +6985 2 lorem_ipsum.txt diff --git a/tests/fixtures/cksum/untagged/sysv_stdin.expected b/tests/fixtures/cksum/untagged/sysv_stdin.expected new file mode 100644 index 00000000000..f0fba8c815c --- /dev/null +++ b/tests/fixtures/cksum/untagged/sysv_stdin.expected @@ -0,0 +1 @@ +6985 2