diff --git a/src/od/od.rs b/src/od/od.rs index 969ee55ea..3b65a17c0 100644 --- a/src/od/od.rs +++ b/src/od/od.rs @@ -61,6 +61,9 @@ pub fn uumain(args: Vec) -> i32 { 0 } +const LINEBYTES:usize = 16; +const WORDBYTES:usize = 2; + fn odfunc(input_offset_base: &Radix, fname: &str) { let mut f = match File::open(Path::new(fname)) { Ok(f) => f, @@ -68,29 +71,37 @@ fn odfunc(input_offset_base: &Radix, fname: &str) { }; let mut addr = 0; - let bytes = &mut [b'\x00'; 16]; - loop { + let bytes = &mut [b'\x00'; LINEBYTES]; + loop { // print each line + print_with_radix(input_offset_base, addr); // print offset match f.read(bytes) { Ok(0) => { - print_with_radix(input_offset_base, addr); print!("\n"); break; } Ok(n) => { - print_with_radix(input_offset_base, addr); + print!(" "); // 4 spaces after offset - we print 2 more before each word + for b in 0 .. n / mem::size_of::() { let bs = &bytes[(2 * b) .. (2 * b + 2)]; let p: u16 = (bs[1] as u16) << 8 | bs[0] as u16; - print!(" {:06o}", p); + print!(" {:06o}", p); } if n % mem::size_of::() == 1 { - print!(" {:06o}", bytes[n - 1]); + print!(" {:06o}", bytes[n - 1]); } + + // Add extra spaces to pad out the short, presumably last, line. + if nwidth$}", "", width=(words_short)*(6+2)); + } + print!("\n"); addr += n; }, Err(_) => { - print_with_radix(input_offset_base, addr); break; } }; @@ -118,7 +129,7 @@ fn parse_radix(radix_str: Option) -> Result { } } } - + 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. @@ -128,4 +139,4 @@ fn print_with_radix(r: &Radix, x: usize) { Radix::Octal => print!("{:07o}", x), Radix::Binary => print!("{:07b}", x) } -} +} \ No newline at end of file