From 0b31e43fac2a394536a23fdadd430db9824eb787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Thu, 24 Oct 2024 00:19:21 +0200 Subject: [PATCH 1/2] cksum: implement --zero flag with newline/NUL replacement --- src/uu/cksum/src/cksum.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 3b1057d701..e7d73a3bbf 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -19,7 +19,9 @@ use uucore::checksum::{ use uucore::{ encoding, error::{FromIo, UResult, USimpleError}, - format_usage, help_about, help_section, help_usage, os_str_as_bytes, show, + format_usage, help_about, help_section, help_usage, + line_ending::LineEnding, + os_str_as_bytes, show, sum::{div_ceil, Digest}, }; @@ -42,6 +44,7 @@ struct Options { length: Option, output_format: OutputFormat, asterisk: bool, // if we display an asterisk or not (--binary/--text) + line_ending: LineEnding, } /// Calculate checksum @@ -173,7 +176,7 @@ where // Therefore, emit the bytes directly to stdout, without any attempt at encoding them. let _dropped_result = stdout().write_all(os_str_as_bytes(filename.as_os_str())?); } - println!("{after_filename}"); + print!("{after_filename}{}", options.line_ending); } Ok(()) @@ -195,6 +198,7 @@ mod options { pub const WARN: &str = "warn"; pub const IGNORE_MISSING: &str = "ignore-missing"; pub const QUIET: &str = "quiet"; + pub const ZERO: &str = "zero"; } /// Determines whether to prompt an asterisk (*) in the output. @@ -330,6 +334,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let (tag, asterisk) = handle_tag_text_binary_flags(&matches)?; let algo = detect_algo(algo_name, length)?; + let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO)); let output_format = if matches.get_flag(options::RAW) { OutputFormat::Raw @@ -347,6 +352,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { tag, output_format, asterisk, + line_ending, }; match matches.get_many::(options::FILE) { @@ -474,6 +480,15 @@ pub fn uu_app() -> Command { .help("don't fail or report status for missing files") .action(ArgAction::SetTrue), ) + .arg( + Arg::new(options::ZERO) + .long(options::ZERO) + .short('z') + .help( + "end each output line with NUL, not newline,\n and disable file name escaping", + ) + .action(ArgAction::SetTrue), + ) .after_help(AFTER_HELP) } From e9cf31451ec482f1f6c93f6424f8a49ce186ff6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Thu, 24 Oct 2024 00:29:45 +0200 Subject: [PATCH 2/2] test(cksum): add tests for -z/--zero --- tests/by-util/test_cksum.rs | 19 ++++++++++++++++++ .../cksum/zero_multiple_file.expected | Bin 0 -> 69 bytes .../fixtures/cksum/zero_single_file.expected | Bin 0 -> 39 bytes 3 files changed, 19 insertions(+) create mode 100644 tests/fixtures/cksum/zero_multiple_file.expected create mode 100644 tests/fixtures/cksum/zero_single_file.expected diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index c408ae34d0..07c6e7b17c 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -1383,3 +1383,22 @@ fn test_check_failed_to_read() { .stdout_is("dir: FAILED open or read\n") .stderr_contains("cksum: WARNING: 1 listed file could not be read"); } + +#[test] +fn test_zero_multiple_file() { + new_ucmd!() + .arg("-z") + .arg("alice_in_wonderland.txt") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_is_fixture("zero_multiple_file.expected"); +} + +#[test] +fn test_zero_single_file() { + new_ucmd!() + .arg("--zero") + .arg("alice_in_wonderland.txt") + .succeeds() + .stdout_is_fixture("zero_single_file.expected"); +} diff --git a/tests/fixtures/cksum/zero_multiple_file.expected b/tests/fixtures/cksum/zero_multiple_file.expected new file mode 100644 index 0000000000000000000000000000000000000000..46ad77aca49914b91eb0d47bee58fe5b41d84a07 GIT binary patch literal 69 zcmXrEFfg?=FgG_aS1>j(Qb^3nOiqo@%!@D2&r3-y%1O*i(JQGaVK6qgFtRi;HaAl+ VH#btq$uCOHjn6D7F3knY0sy`l6)pe( literal 0 HcmV?d00001 diff --git a/tests/fixtures/cksum/zero_single_file.expected b/tests/fixtures/cksum/zero_single_file.expected new file mode 100644 index 0000000000000000000000000000000000000000..23292d9b58c2abd499f2f6d012b5a07028293d73 GIT binary patch literal 39 ucmXrEFfg?=FgG_aS1>j(Qb^3nOiqo@%!@D2&r3-y%1O*i(JQGaVE_QuP7E^u literal 0 HcmV?d00001