8000 od: allow trailing characters in address radix by andrewliebenow · Pull Request #6674 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

od: allow trailing characters in address radix #6674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
38 changes: 17 additions & 21 deletions src/uu/od/src/od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

// spell-checker:ignore (clap) dont
// spell-checker:ignore (ToDO) formatteriteminfo inputdecoder inputoffset mockstream nrofbytes partialreader odfunc multifile exitcode
// spell-checker:ignore Anone

mod byteorder_io;
mod formatteriteminfo;
Expand Down Expand Up @@ -169,29 +170,24 @@
let radix = match matches.get_one::<String>(options::ADDRESS_RADIX) {
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".
// 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();
if st.len() == 1 {
let radix: char = *(st
.first()
.expect("byte string of length 1 lacks a 0th elem"))
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(),
))
}
let radix: char = *(st.first().expect("should be caught by clap")) as char;
match radix {
'd' => Radix::Decimal,
'x' => Radix::Hexadecimal,
'o' => Radix::Octal,

Check warning on line 183 in src/uu/od/src/od.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/od/src/od.rs#L183

Added line #L183 was not covered by tests
'n' => Radix::NoPrefix,
_ => {
return Err(USimpleError::new(
1,
"Radix must be one of [d, o, n, x]".to_string(),
))
}
} else {
return Err(USimpleError::new(
1,
"Radix must be one of [d, o, n, x]".to_string(),
));
}
}
};
Expand Down
15 changes: 14 additions & 1 deletion tests/by-util/test_od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore abcdefghijklmnopqrstuvwxyz
// spell-checker:ignore abcdefghijklmnopqrstuvwxyz Anone

use crate::common::util::TestScenario;
use unindent::unindent;
Expand Down Expand Up @@ -579,6 +579,19 @@ fn test_invalid_offset() {
new_ucmd!().arg("-Ab").fails();
}

#[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);
}

#[test]
fn test_skip_bytes() {
let input = "abcdefghijklmnopq";
Expand Down
Loading
0