From e7ef9dcd4676ca231bc4e5ce9c6afb684a78cf17 Mon Sep 17 00:00:00 2001 From: 0xMRTT <0xMRTT@proton.me> Date: Tue, 28 Mar 2023 23:26:11 +0200 Subject: [PATCH] `hashsum`: add `--zero` option (#4612) (#4632) * `hashsum`: add `--zero` option (#4612) --- src/uu/hashsum/src/hashsum.rs | 12 ++++++++++++ tests/by-util/test_hashsum.rs | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/src/uu/hashsum/src/hashsum.rs b/src/uu/hashsum/src/hashsum.rs index d7e55e567..460ab6f19 100644 --- a/src/uu/hashsum/src/hashsum.rs +++ b/src/uu/hashsum/src/hashsum.rs @@ -47,6 +47,7 @@ struct Options { strict: bool, warn: bool, output_bits: usize, + zero: bool, } #[allow(clippy::cognitive_complexity)] @@ -269,6 +270,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> { let quiet = matches.get_flag("quiet") || status; let strict = matches.get_flag("strict"); let warn = matches.get_flag("warn") && !status; + let zero = matches.get_flag("zero"); let opts = Options { algoname: name, @@ -282,6 +284,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> { quiet, strict, warn, + zero, }; match matches.get_many::("FILE") { @@ -359,6 +362,13 @@ pub fn uu_app_common() -> Command { .help("warn about improperly formatted checksum lines") .action(ArgAction::SetTrue), ) + .arg( + Arg::new("zero") + .short('z') + .long("zero") + .help("end each output line with NUL, not newline") + .action(ArgAction::SetTrue), + ) .arg( Arg::new("FILE") .index(1) @@ -613,6 +623,8 @@ where println!("{} ({}) = {}", options.algoname, filename.display(), sum); } else if options.nonames { println!("{sum}"); + } else if options.zero { + print!("{} {}{}\0", sum, binary_marker, filename.display()); } else { println!("{} {}{}", sum, binary_marker, filename.display()); } diff --git a/tests/by-util/test_hashsum.rs b/tests/by-util/test_hashsum.rs index 3d3d72c4c..a227b9fc3 100644 --- a/tests/by-util/test_hashsum.rs +++ b/tests/by-util/test_hashsum.rs @@ -53,6 +53,14 @@ macro_rules! test_digest { .stdout_is("input.txt: OK\n"); } + #[test] + fn test_zero() { + let ts = TestScenario::new("hashsum"); + assert_eq!(ts.fixtures.read(EXPECTED_FILE), + get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg("--zero").arg("input.txt").succeeds().no_stderr().stdout_str())); + } + + #[cfg(windows)] #[test] fn test_text_mode() {