Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/uu/checksum_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -212,10 +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 has_file_operands = matches.value_source(options::FILE) == Some(ValueSource::CommandLine);

let opts = ChecksumComputeOptions {
algo_kind: algo,
output_format,
line_ending,
has_file_operands,
};

perform_checksum_computation(io::stdout(), opts, files)?;
Expand Down
10 changes: 8 additions & 2 deletions src/uucore/src/lib/features/checksum/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ pub struct ChecksumComputeOptions {

/// Whether to finish lines with '\n' or '\0'.
pub line_ending: LineEnding,

/// 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 has_file_operands: bool,
}

/// Whether to write the digest as hexadecimal or encoded in base64.
Expand Down Expand Up @@ -165,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
if escaped_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())?;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ fn test_stdin() {
.stdout_is_fixture("crc_stdin.expected");
}

#[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!()
.args(&["--algorithm", algo, "-"])
.pipe_in(b"")
.succeeds()
.no_stderr()
.stdout_is(expected);
}

#[test]
fn test_stdin_with_dash_directory() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down
Loading