diff --git a/src/uu/dd/src/dd.rs b/src/uu/dd/src/dd.rs index 94da4b7d3dd..4b3ad44349d 100644 --- a/src/uu/dd/src/dd.rs +++ b/src/uu/dd/src/dd.rs @@ -146,7 +146,7 @@ impl Input { } opts.open(fname) - .map_err_context(|| "failed to open input file".to_string())? + .map_err_context(|| format!("failed to open {}", fname.quote()))? }; // The --skip and --iseek flags are additive. On a file, they seek. diff --git a/src/uu/dd/src/parseargs.rs b/src/uu/dd/src/parseargs.rs index 96ac55d75d2..701134ee14c 100644 --- a/src/uu/dd/src/parseargs.rs +++ b/src/uu/dd/src/parseargs.rs @@ -35,6 +35,7 @@ pub enum ParseError { IbsOutOfRange, ObsOutOfRange, CbsOutOfRange, + InvalidNumber(String), } impl ParseError { @@ -56,6 +57,7 @@ impl ParseError { Self::IbsOutOfRange => Self::IbsOutOfRange, Self::ObsOutOfRange => Self::ObsOutOfRange, Self::CbsOutOfRange => Self::CbsOutOfRange, + Self::InvalidNumber(_) => Self::InvalidNumber(s), } } } @@ -79,7 +81,12 @@ impl std::fmt::Display for ParseError { write!(f, "Only one ov conv=excl or conv=nocreat may be specified") } Self::FlagNoMatch(arg) => { - write!(f, "Unrecognized iflag=FLAG or oflag=FLAG -> {}", arg) + // Additional message about 'dd --help' is displayed only in this situation. + write!( + f, + "invalid input flag: ‘{}’\nTry 'dd --help' for more information.", + arg + ) } Self::ConvFlagNoMatch(arg) => { write!(f, "Unrecognized conv=CONV -> {}", arg) @@ -115,6 +122,9 @@ impl std::fmt::Display for ParseError { Self::Unimplemented(arg) => { write!(f, "feature not implemented on this system -> {}", arg) } + Self::InvalidNumber(arg) => { + write!(f, "invalid number: ‘{}’", arg) + } } } } @@ -389,7 +399,7 @@ fn parse_bytes_no_x(s: &str) -> Result { (None, None, None) => match uucore::parse_size::parse_size(s) { Ok(n) => (n, 1), Err(ParseSizeError::InvalidSuffix(s)) | Err(ParseSizeError::ParseFailure(s)) => { - return Err(ParseError::MultiplierStringParseFailure(s)) + return Err(ParseError::InvalidNumber(s)) } Err(ParseSizeError::SizeTooBig(s)) => { return Err(ParseError::MultiplierStringOverflow(s)) diff --git a/tests/by-util/test_dd.rs b/tests/by-util/test_dd.rs index 8393248ef88..a3a422ad819 100644 --- a/tests/by-util/test_dd.rs +++ b/tests/by-util/test_dd.rs @@ -1187,3 +1187,62 @@ fn test_final_stats_si_iec() { let s = result.stderr_str(); assert!(s.starts_with("2+0 records in\n2+0 records out\n1024 bytes (1 KB, 1024 B) copied,")); } + +#[test] +fn test_invalid_number_arg_gnu_compatibility() { + let commands = vec!["bs", "cbs", "count", "ibs", "obs", "seek", "skip"]; + + for command in commands { + new_ucmd!() + .args(&[format!("{}=", command)]) + .fails() + .stderr_is("dd: invalid number: ‘’"); + + new_ucmd!() + .args(&[format!("{}=29d", command)]) + .fails() + .stderr_is("dd: invalid number: ‘29d’"); + } +} + +#[test] +fn test_invalid_flag_arg_gnu_compatibility() { + let commands = vec!["iflag", "oflag"]; + + for command in commands { + new_ucmd!() + .args(&[format!("{}=", command)]) + .fails() + .stderr_is("dd: invalid input flag: ‘’\nTry 'dd --help' for more information."); + + new_ucmd!() + .args(&[format!("{}=29d", command)]) + .fails() + .stderr_is("dd: invalid input flag: ‘29d’\nTry 'dd --help' for more information."); + } +} + +#[test] +fn test_invalid_file_arg_gnu_compatibility() { + new_ucmd!() + .args(&["if="]) + .fails() + .stderr_is("dd: failed to open '': No such file or directory"); + + new_ucmd!() + .args(&["if=81as9bn8as9g302az8ns9.pdf.zip.pl.com"]) + .fails() + .stderr_is( + "dd: failed to open '81as9bn8as9g302az8ns9.pdf.zip.pl.com': No such file or directory", + ); + + new_ucmd!() + .args(&["of="]) + .fails() + .stderr_is("dd: failed to open '': No such file or directory"); + + new_ucmd!() + .args(&["of=81as9bn8as9g302az8ns9.pdf.zip.pl.com"]) + .pipe_in("") + .succeeds(); +}