1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 13:07:46 +00:00

Merge pull request #517 from keunwoo/keunwoo-fix-address-radix-20150127

od: fix --address-radix interpretation and minor cleanups
This commit is contained in:
Alex Lyon 2015-02-01 12:13:37 -08:00
commit 9c0ed7236c

View file

@ -38,7 +38,7 @@ pub fn uumain(args: Vec<String>) -> isize {
specified."), specified."),
"BYTES"), "BYTES"),
getopts::optflag("h", "help", "display this help and exit."), getopts::optflag("h", "help", "display this help and exit."),
getopts::optflag("v", "version", "output version information and exit."), getopts::optflag("", "version", "output version information and exit."),
]; ];
let matches = match getopts::getopts(args.tail(), &opts) { let matches = match getopts::getopts(args.tail(), &opts) {
@ -46,26 +46,22 @@ pub fn uumain(args: Vec<String>) -> isize {
Err(f) => panic!("Invalid options\n{}", f) Err(f) => panic!("Invalid options\n{}", f)
}; };
let input_offset_base = match parse_radix(matches.opt_str("A")) {
Ok(r) => r,
Err(f) => { panic!("Invalid -A/--address-radix\n{}", f) }
};
let mut rad = Radix::Octal; let fname = match args.last() {
if matches.opt_present("A") { Some(n) => n,
rad = parse_radix(matches.opt_str("A"));
} else {
println!("{}", getopts::usage("od", &opts));
}
let mut fname;
match args.last() {
Some(n) => fname = n,
None => { panic!("Need fname for now") ; } None => { panic!("Need fname for now") ; }
}; };
main(rad, fname.clone()); main(&input_offset_base, fname.as_slice());
0 0
} }
fn main(radix: Radix, fname: String) { fn main(input_offset_base: &Radix, fname: &str) {
let mut f = match File::open(&Path::new(fname)) { let mut f = match File::open(&Path::new(fname)) {
Ok(f) => f, Ok(f) => f,
Err(e) => panic!("file error: {}", e) Err(e) => panic!("file error: {}", e)
@ -76,10 +72,7 @@ fn main(radix: Radix, fname: String) {
loop { loop {
match f.read(bytes) { match f.read(bytes) {
Ok(n) => { Ok(n) => {
print!("{:07o}", addr); print_with_radix(input_offset_base, addr);
match radix {
Radix::Decimal => {},
Radix::Octal => {
for b in range(0, n / std::u16::BYTES) { for b in range(0, n / std::u16::BYTES) {
let bs = &bytes[(2 * b) .. (2 * b + 2)]; let bs = &bytes[(2 * b) .. (2 * b + 2)];
let p: u16 = (bs[1] as u16) << 8 | bs[0] as u16; let p: u16 = (bs[1] as u16) << 8 | bs[0] as u16;
@ -88,41 +81,46 @@ fn main(radix: Radix, fname: String) {
if n % std::u16::BYTES == 1 { if n % std::u16::BYTES == 1 {
print!(" {:06o}", bytes[n - 1]); print!(" {:06o}", bytes[n - 1]);
} }
}
_ => { }
};
print!("\n"); print!("\n");
addr += n; addr += n;
}, },
Err(_) => { println!("{:07o}", addr); break; } Err(_) => {
print_with_radix(input_offset_base, addr);
break;
}
}; };
}; };
} }
fn parse_radix(radix_str: Option<String>) -> Radix { fn parse_radix(radix_str: Option<String>) -> Result<Radix, &'static str> {
let rad = match radix_str { match radix_str {
None => Ok(Radix::Octal),
Some(s) => { Some(s) => {
let st = s.into_bytes(); let st = s.into_bytes();
if st.len() != 1 { if st.len() != 1 {
panic!("Radix must be one of [d, o, b, x]\n"); Err("Radix must be one of [d, o, b, x]\n")
} } else {
let radix: char = *(st.get(0) let radix: char = *(st.get(0)
.expect("byte string of length 1 lacks a 0th elem")) as char; .expect("byte string of length 1 lacks a 0th elem")) as char;
if radix == 'd' { match radix {
Radix::Decimal 'd' => Ok(Radix::Decimal),
} else if radix == 'x' { 'x' => Ok(Radix::Hexadecimal),
Radix::Hexadecimal 'o' => Ok(Radix::Octal),
} else if radix == 'o' { 'b' => Ok(Radix::Binary),
Radix::Octal _ => Err("Radix must be one of [d, o, b, x]\n")
} else if radix == 'b' { }
Radix::Binary }
} else { }
panic!("Radix must be one of [d, o, b, x]\n"); }
}
fn print_with_radix(r: &Radix, x: usize) {
// TODO(keunwoo): field widths should be based on sizeof(x), or chosen dynamically based on the
// expected range of address values. Binary in particular is not great here.
match *r {
Radix::Decimal => print!("{:07}", x),
Radix::Hexadecimal => print!("{:07X}", x),
Radix::Octal => print!("{:07o}", x),
Radix::Binary => print!("{:07b}", x)
} }
},
None => Radix::Octal
};
rad
} }