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] 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 3b1057d70..e7d73a3bb 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) }