From 5ca995b21f560cbfc4b67ae64bd94aaea699739a Mon Sep 17 00:00:00 2001 From: Andrew Liebenow Date: Sat, 31 Aug 2024 14:13:31 -0500 Subject: [PATCH] od: allow trailing characters in address radix --- src/uu/od/src/od.rs | 38 +++++++++++++++++--------------------- tests/by-util/test_od.rs | 15 ++++++++++++++- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/uu/od/src/od.rs b/src/uu/od/src/od.rs index 1c9548f50..854b4083e 100644 --- a/src/uu/od/src/od.rs +++ b/src/uu/od/src/od.rs @@ -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; @@ -169,29 +170,24 @@ impl OdOptions { let radix = match matches.get_one::(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, + '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(), - )); } } }; diff --git a/tests/by-util/test_od.rs b/tests/by-util/test_od.rs index 5703c4371..bce088c17 100644 --- a/tests/by-util/test_od.rs +++ b/tests/by-util/test_od.rs @@ -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; @@ -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";