mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +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:
commit
9c0ed7236c
1 changed files with 50 additions and 52 deletions
102
src/od/od.rs
102
src/od/od.rs
|
@ -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")) {
|
||||||
let mut rad = Radix::Octal;
|
Ok(r) => r,
|
||||||
if matches.opt_present("A") {
|
Err(f) => { panic!("Invalid -A/--address-radix\n{}", f) }
|
||||||
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") ; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
main(rad, fname.clone());
|
let fname = match args.last() {
|
||||||
|
Some(n) => n,
|
||||||
|
None => { panic!("Need fname for now") ; }
|
||||||
|
};
|
||||||
|
|
||||||
|
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,53 +72,55 @@ 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 {
|
for b in range(0, n / std::u16::BYTES) {
|
||||||
Radix::Decimal => {},
|
let bs = &bytes[(2 * b) .. (2 * b + 2)];
|
||||||
Radix::Octal => {
|
let p: u16 = (bs[1] as u16) << 8 | bs[0] as u16;
|
||||||
for b in range(0, n / std::u16::BYTES) {
|
print!(" {:06o}", p);
|
||||||
let bs = &bytes[(2 * b) .. (2 * b + 2)];
|
}
|
||||||
let p: u16 = (bs[1] as u16) << 8 | bs[0] as u16;
|
if n % std::u16::BYTES == 1 {
|
||||||
print!(" {:06o}", p);
|
print!(" {:06o}", bytes[n - 1]);
|
||||||
}
|
}
|
||||||
if n % std::u16::BYTES == 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")
|
||||||
}
|
|
||||||
|
|
||||||
let radix: char = *(st.get(0)
|
|
||||||
.expect("byte string of length 1 lacks a 0th elem")) as char;
|
|
||||||
if radix == 'd' {
|
|
||||||
Radix::Decimal
|
|
||||||
} else if radix == 'x' {
|
|
||||||
Radix::Hexadecimal
|
|
||||||
} else if radix == 'o' {
|
|
||||||
Radix::Octal
|
|
||||||
} else if radix == 'b' {
|
|
||||||
Radix::Binary
|
|
||||||
} else {
|
} else {
|
||||||
panic!("Radix must be one of [d, o, b, x]\n");
|
let radix: char = *(st.get(0)
|
||||||
|
.expect("byte string of length 1 lacks a 0th elem")) as char;
|
||||||
|
match radix {
|
||||||
|
'd' => Ok(Radix::Decimal),
|
||||||
|
'x' => Ok(Radix::Hexadecimal),
|
||||||
|
'o' => Ok(Radix::Octal),
|
||||||
|
'b' => Ok(Radix::Binary),
|
||||||
|
_ => Err("Radix must be one of [d, o, b, x]\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
None => Radix::Octal
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
rad
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue