mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Add extra spaces to output to match formating of origial od
This commit is contained in:
parent
e0c55b65a8
commit
1fbda9663d
1 changed files with 20 additions and 9 deletions
21
src/od/od.rs
21
src/od/od.rs
|
@ -61,6 +61,9 @@ pub fn uumain(args: Vec<String>) -> 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,16 +71,17 @@ 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::<u16>() {
|
||||
let bs = &bytes[(2 * b) .. (2 * b + 2)];
|
||||
let p: u16 = (bs[1] as u16) << 8 | bs[0] as u16;
|
||||
|
@ -86,11 +90,18 @@ fn odfunc(input_offset_base: &Radix, fname: &str) {
|
|||
if n % mem::size_of::<u16>() == 1 {
|
||||
print!(" {:06o}", bytes[n - 1]);
|
||||
}
|
||||
|
||||
// Add extra spaces to pad out the short, presumably last, line.
|
||||
if n<LINEBYTES {
|
||||
// calc # of items we did not print, must be short at least WORDBYTES to be missing any.
|
||||
let words_short = (LINEBYTES-n)/WORDBYTES;
|
||||
print!("{:>width$}", "", width=(words_short)*(6+2));
|
||||
}
|
||||
|
||||
print!("\n");
|
||||
addr += n;
|
||||
},
|
||||
Err(_) => {
|
||||
print_with_radix(input_offset_base, addr);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue