Skip to content

Commit

Permalink
Merge pull request uutils#6815 from RenjiSann/cksum-zero
Browse files Browse the repository at this point in the history
cksum: Implement -z/--zero
  • Loading branch information
sylvestre authored Oct 24, 2024
2 parents 6850be2 + e9cf314 commit c9f8ef5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/uu/cksum/src/cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand All @@ -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
Expand Down Expand Up @@ -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(())
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down
19 changes: 19 additions & 0 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Binary file added tests/fixtures/cksum/zero_multiple_file.expected
Binary file not shown.
Binary file added tests/fixtures/cksum/zero_single_file.expected
Binary file not shown.

0 comments on commit c9f8ef5

Please sign in to comment.