1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

cksum: implement --zero flag with newline/NUL replacement

This commit is contained in:
Dorian Péron 2024-10-24 00:19:21 +02:00
parent 6850be2539
commit 0b31e43fac

View file

@ -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<usize>,
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::<OsString>(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)
}