From 3c462f681adf629f59c9734708f0f8e5c79bd368 Mon Sep 17 00:00:00 2001 From: Logic769 Date: Fri, 30 Oct 2026 04:53:54 +0800 Subject: [PATCH 1/2] cksum: print file name for explicit - operand --- src/uu/checksum_common/src/lib.rs | 5 +++++ src/uucore/src/lib/features/checksum/compute.rs | 9 +++++++-- tests/by-util/test_cksum.rs | 10 ++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/uu/checksum_common/src/lib.rs b/src/uu/checksum_common/src/lib.rs index a6452fb8181..a8f856a793b 100644 --- a/src/uu/checksum_common/src/lib.rs +++ b/src/uu/checksum_common/src/lib.rs @@ -10,6 +10,7 @@ use std::ffi::OsString; use std::io; use clap::builder::ValueParser; +use clap::parser::ValueSource; use clap::{Arg, ArgAction, ArgMatches, Command, ValueHint}; use uucore::checksum::compute::{ @@ -212,10 +213,14 @@ pub fn checksum_main( let algo = SizedAlgoKind::from_unsized(algo_kind, length)?; let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO)); + let explicit_stdin_filename = + matches.value_source(options::FILE) == Some(ValueSource::CommandLine); + let opts = ChecksumComputeOptions { algo_kind: algo, output_format, line_ending, + explicit_stdin_filename, }; perform_checksum_computation(io::stdout(), opts, files)?; diff --git a/src/uucore/src/lib/features/checksum/compute.rs b/src/uucore/src/lib/features/checksum/compute.rs index 90072308460..29c0ec0d86d 100644 --- a/src/uucore/src/lib/features/checksum/compute.rs +++ b/src/uucore/src/lib/features/checksum/compute.rs @@ -37,6 +37,11 @@ pub struct ChecksumComputeOptions { /// Whether to finish lines with '\n' or '\0'. pub line_ending: LineEnding, + + /// Whether stdin ("-") was given explicitly as an operand. GNU prints the + /// file name for an explicit "-" in legacy output, but not for the implicit + /// stdin used when no operand is provided. + pub explicit_stdin_filename: bool, } /// Whether to write the digest as hexadecimal or encoded in base64. @@ -165,8 +170,8 @@ fn write_legacy_checksum( (algo, output) => unreachable!("Bug: Invalid legacy checksum ({algo:?}, {output:?})"), } - // Print the filename after a space if not stdin - if escaped_filename != "-" { + // Print the filename after a space if not stdin, unless "-" was explicit + if escaped_filename != "-" || options.explicit_stdin_filename { write!(w, " ")?; w.write_all(escaped_filename.as_bytes())?; } diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index 2636010224a..c73608a3da9 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -89,6 +89,16 @@ fn test_stdin() { .stdout_is_fixture("crc_stdin.expected"); } +#[test] +fn test_explicit_dash_prints_filename() { + new_ucmd!() + .arg("-") + .pipe_in(b"") + .succeeds() + .no_stderr() + .normalized_newlines_stdout_is("4294967295 0 -\n"); +} + #[test] fn test_stdin_with_dash_directory() { let (at, mut ucmd) = at_and_ucmd!(); From efd813ede723428ddaf0b4b489040529af4ece10 Mon Sep 17 00:00:00 2001 From: Logic769 Date: Tue, 3 Nov 2026 08:15:11 +0800 Subject: [PATCH 2/2] cksum: rename explicit_stdin_filename to has_file_operands --- src/uu/checksum_common/src/lib.rs | 5 ++--- src/uucore/src/lib/features/checksum/compute.rs | 9 +++++---- tests/by-util/test_cksum.rs | 11 +++++++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/uu/checksum_common/src/lib.rs b/src/uu/checksum_common/src/lib.rs index a8f856a793b..6e26d72240e 100644 --- a/src/uu/checksum_common/src/lib.rs +++ b/src/uu/checksum_common/src/lib.rs @@ -213,14 +213,13 @@ pub fn checksum_main( let algo = SizedAlgoKind::from_unsized(algo_kind, length)?; let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO)); - let explicit_stdin_filename = - matches.value_source(options::FILE) == Some(ValueSource::CommandLine); + let has_file_operands = matches.value_source(options::FILE) == Some(ValueSource::CommandLine); let opts = ChecksumComputeOptions { algo_kind: algo, output_format, line_ending, - explicit_stdin_filename, + has_file_operands, }; perform_checksum_computation(io::stdout(), opts, files)?; diff --git a/src/uucore/src/lib/features/checksum/compute.rs b/src/uucore/src/lib/features/checksum/compute.rs index 29c0ec0d86d..185667c755b 100644 --- a/src/uucore/src/lib/features/checksum/compute.rs +++ b/src/uucore/src/lib/features/checksum/compute.rs @@ -38,10 +38,10 @@ pub struct ChecksumComputeOptions { /// Whether to finish lines with '\n' or '\0'. pub line_ending: LineEnding, - /// Whether stdin ("-") was given explicitly as an operand. GNU prints the + /// Whether any file operand was given on the command line. GNU prints the /// file name for an explicit "-" in legacy output, but not for the implicit /// stdin used when no operand is provided. - pub explicit_stdin_filename: bool, + pub has_file_operands: bool, } /// Whether to write the digest as hexadecimal or encoded in base64. @@ -170,8 +170,9 @@ fn write_legacy_checksum( (algo, output) => unreachable!("Bug: Invalid legacy checksum ({algo:?}, {output:?})"), } - // Print the filename after a space if not stdin, unless "-" was explicit - if escaped_filename != "-" || options.explicit_stdin_filename { + // Print the filename after a space if not stdin, unless a file operand + // was given explicitly. + if escaped_filename != "-" || options.has_file_operands { write!(w, " ")?; w.write_all(escaped_filename.as_bytes())?; } diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index c73608a3da9..35cc92a900e 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -89,14 +89,17 @@ fn test_stdin() { .stdout_is_fixture("crc_stdin.expected"); } -#[test] -fn test_explicit_dash_prints_filename() { +#[rstest] +#[case::crc("crc", "4294967295 0 -\n")] +#[case::bsd("bsd", "00000 0 -\n")] +#[case::sysv("sysv", "0 0 -\n")] +fn test_explicit_dash_prints_filename(#[case] algo: &str, #[case] expected: &str) { new_ucmd!() - .arg("-") + .args(&["--algorithm", algo, "-"]) .pipe_in(b"") .succeeds() .no_stderr() - .normalized_newlines_stdout_is("4294967295 0 -\n"); + .stdout_is(expected); } #[test]