Skip to content
Merged
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
31 changes: 19 additions & 12 deletions src/uu/od/src/od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,23 +171,30 @@ impl OdOptions {
None => Radix::Octal,
Some(s) => {
// Other implementations of od only check the first character of this argument's value.
// This means executing "od -Anone" is equivalent to "od -An".
// This means executing `od -Anone` is equivalent to executing `od -An`.
// Existing users of od rely on this behavior:
// https://github.com/landley/toybox/blob/d50372cad35d5dd12e6391c3c7c901a96122dc67/scripts/make.sh#L239
// https://github.com/google/jsonnet/blob/913281d203578bb394995bacc792f2576371e06c/Makefile#L212
let st = s.as_bytes();
let radix: char = *(st.first().expect("should be caught by clap")) as char;
match radix {
'd' => Radix::Decimal,
'x' => Radix::Hexadecimal,
'o' => Radix::Octal,
'n' => Radix::NoPrefix,
_ => {
return Err(USimpleError::new(
1,
"Radix must be one of [d, o, n, x]".to_string(),
))
if let Some(u) = st.first() {
match *u {
b'o' => Radix::Octal,
b'd' => Radix::Decimal,
b'x' => Radix::Hexadecimal,
b'n' => Radix::NoPrefix,
_ => {
return Err(USimpleError::new(
1,
"Radix must be one of [o, d, x, n]".to_string(),
));
}
}
} else {
// Return an error instead of panicking when `od -A ''` is executed.
return Err(USimpleError::new(
1,
"Radix cannot be empty, and must be one of [o, d, x, n]".to_string(),
));
}
}
};
Expand Down
16 changes: 12 additions & 4 deletions tests/by-util/test_od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,17 +579,25 @@ fn test_invalid_offset() {
new_ucmd!().arg("-Ab").fails();
}

#[test]
fn test_empty_offset() {
new_ucmd!()
.arg("-A")
.arg("")
.fails()
.stderr_only("od: Radix cannot be empty, and must be one of [o, d, x, n]\n");
}

#[test]
fn test_offset_compatibility() {
let input = [0u8; 4];
let expected_output = " 000000 000000\n";

new_ucmd!()
.arg("-Anone")
.run_piped_stdin(input)
.no_stderr()
.success()
.stdout_is(expected_output);
.pipe_in(input)
.succeeds()
.stdout_only(expected_output);
}

#[test]
Expand Down