2015-12-08 02:42:08 +00:00
|
|
|
#![crate_name = "uu_od"]
|
2014-07-10 20:49:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Ben Hirsch <benhirsch24@gmail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern crate getopts;
|
|
|
|
|
2015-05-11 04:48:36 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::mem;
|
2016-04-26 02:55:34 +00:00
|
|
|
use std::io::BufReader;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::io;
|
|
|
|
|
2015-02-03 21:34:45 +00:00
|
|
|
#[derive(Debug)]
|
2014-07-10 20:49:20 +00:00
|
|
|
enum Radix { Decimal, Hexadecimal, Octal, Binary }
|
2016-04-26 02:05:16 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum InputSource<'a> {
|
|
|
|
FileName(&'a str ),
|
|
|
|
Stdin
|
|
|
|
}
|
|
|
|
|
2015-02-06 13:48:07 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> i32 {
|
2015-05-21 02:45:43 +00:00
|
|
|
let mut opts = getopts::Options::new();
|
2015-01-25 08:03:14 +00:00
|
|
|
|
2015-05-21 02:45:43 +00:00
|
|
|
opts.optopt("A", "address-radix",
|
|
|
|
"Select the base in which file offsets are printed.", "RADIX");
|
|
|
|
opts.optopt("j", "skip-bytes",
|
|
|
|
"Skip bytes input bytes before formatting and writing.", "BYTES");
|
|
|
|
opts.optopt("N", "read-bytes",
|
|
|
|
"limit dump to BYTES input bytes", "BYTES");
|
|
|
|
opts.optopt("S", "strings",
|
|
|
|
("output strings of at least BYTES graphic chars. 3 is assumed when \
|
|
|
|
BYTES is not specified."),
|
|
|
|
"BYTES");
|
|
|
|
opts.optopt("t", "format", "select output format or formats", "TYPE");
|
|
|
|
opts.optflag("v", "output-duplicates", "do not use * to mark line suppression");
|
|
|
|
opts.optopt("w", "width",
|
|
|
|
("output BYTES bytes per output line. 32 is implied when BYTES is not \
|
|
|
|
specified."),
|
|
|
|
"BYTES");
|
|
|
|
opts.optflag("h", "help", "display this help and exit.");
|
|
|
|
opts.optflag("", "version", "output version information and exit.");
|
|
|
|
|
|
|
|
let matches = match opts.parse(&args[1..]) {
|
2015-01-25 08:03:14 +00:00
|
|
|
Ok(m) => m,
|
|
|
|
Err(f) => panic!("Invalid options\n{}", f)
|
|
|
|
};
|
|
|
|
|
2015-01-26 06:02:48 +00:00
|
|
|
let input_offset_base = match parse_radix(matches.opt_str("A")) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(f) => { panic!("Invalid -A/--address-radix\n{}", f) }
|
|
|
|
};
|
2016-04-26 02:55:34 +00:00
|
|
|
|
2016-04-26 02:05:16 +00:00
|
|
|
// Gather up file names - args which don't start with '-'
|
2016-04-26 02:55:34 +00:00
|
|
|
let fnames = args[1..]
|
|
|
|
.iter()
|
2016-04-26 02:05:16 +00:00
|
|
|
.filter(|w| !w.starts_with('-') || w == &"--" ) // "--" starts with '-', but it denotes stdin, not a flag
|
|
|
|
.map(|x| match x.as_str() { "--" => InputSource::Stdin, x => InputSource::FileName(x)})
|
2016-04-26 02:55:34 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2016-04-26 02:05:16 +00:00
|
|
|
// With no filenames, od uses stdin as input.
|
2016-04-26 02:55:34 +00:00
|
|
|
if fnames.len() == 0 {
|
2016-04-26 02:05:16 +00:00
|
|
|
odfunc(&input_offset_base, &[InputSource::Stdin])
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
odfunc(&input_offset_base, &fnames)
|
|
|
|
}
|
2014-07-10 20:49:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-26 02:47:24 +00:00
|
|
|
const LINEBYTES:usize = 16;
|
|
|
|
const WORDBYTES:usize = 2;
|
2016-04-26 02:55:34 +00:00
|
|
|
|
2016-04-26 02:05:16 +00:00
|
|
|
fn odfunc(input_offset_base: &Radix, fnames: &[InputSource]) -> i32 {
|
|
|
|
|
2016-04-26 02:55:34 +00:00
|
|
|
let mut status = 0;
|
|
|
|
let mut ni = fnames.iter();
|
|
|
|
{
|
|
|
|
// Open and return the next file to process as a BufReader
|
|
|
|
// Returns None when no more files.
|
2016-04-26 02:05:16 +00:00
|
|
|
let mut next_file = || -> Option<Box<io::Read>> {
|
2016-04-26 02:55:34 +00:00
|
|
|
// loop retries with subsequent files if err - normally 'loops' once
|
|
|
|
loop {
|
2016-04-26 02:05:16 +00:00
|
|
|
match ni.next() {
|
2016-04-26 02:55:34 +00:00
|
|
|
None => return None,
|
2016-04-26 02:05:16 +00:00
|
|
|
Some(input) => match *input {
|
|
|
|
InputSource::Stdin => return Some(Box::new(BufReader::new(std::io::stdin()))),
|
|
|
|
InputSource::FileName(fname) => match File::open(fname) {
|
|
|
|
Ok(f) => return Some(Box::new(BufReader::new(f))),
|
|
|
|
Err(e) => {
|
|
|
|
// If any file can't be opened,
|
|
|
|
// print an error at the time that the file is needed,
|
|
|
|
// then move on the the next file.
|
|
|
|
// This matches the behavior of the original `od`
|
|
|
|
let _ = writeln!(&mut std::io::stderr(), "od: '{}': {}", fname, e);
|
|
|
|
if status == 0 {status = 1}
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 02:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-04-26 02:05:16 +00:00
|
|
|
|
|
|
|
let mut curr_file: Box<io::Read> = match next_file() {
|
2016-04-26 02:55:34 +00:00
|
|
|
Some(f) => f,
|
|
|
|
None => {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
2016-04-26 02:05:16 +00:00
|
|
|
|
2016-04-26 02:55:34 +00:00
|
|
|
let mut exhausted = false; // There is no more input, gone to the end of the last file.
|
2016-04-26 02:47:24 +00:00
|
|
|
|
2016-04-26 02:55:34 +00:00
|
|
|
// Fill buf with bytes read from the list of files
|
|
|
|
// Returns Ok(<number of bytes read>)
|
|
|
|
// Handles io errors itself, thus always returns OK
|
|
|
|
// Fills the provided buffer completely, unless it has run out of input.
|
|
|
|
// If any call returns short (< buf.len()), all subsequent calls will return Ok<0>
|
|
|
|
let mut f_read = |buf: &mut [u8]| -> io::Result<usize> {
|
|
|
|
if exhausted {
|
|
|
|
Ok(0)
|
|
|
|
} else {
|
|
|
|
let mut xfrd = 0;
|
2016-04-26 02:05:16 +00:00
|
|
|
// while buffer we are filling is not full.. May go thru several files.
|
|
|
|
'fillloop: while xfrd < buf.len() {
|
|
|
|
loop { // stdin may return on 'return' (enter), even though the buffer isn't full.
|
|
|
|
xfrd += match curr_file.read(&mut buf[xfrd..]) {
|
|
|
|
Ok(0) => break,
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(e) => panic!("file error: {}", e),
|
|
|
|
};
|
|
|
|
if xfrd == buf.len() {
|
|
|
|
// transferred all that was asked for.
|
|
|
|
break 'fillloop;
|
|
|
|
}
|
2016-04-26 02:55:34 +00:00
|
|
|
}
|
|
|
|
curr_file = match next_file() {
|
|
|
|
Some(f) => f,
|
|
|
|
None => {
|
|
|
|
exhausted = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2015-01-26 06:39:49 +00:00
|
|
|
}
|
2016-04-26 02:55:34 +00:00
|
|
|
Ok(xfrd)
|
2015-01-26 06:39:49 +00:00
|
|
|
}
|
2015-01-25 08:03:14 +00:00
|
|
|
};
|
2016-04-26 02:55:34 +00:00
|
|
|
|
|
|
|
let mut addr = 0;
|
|
|
|
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!("\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(n) => {
|
|
|
|
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;
|
|
|
|
print!(" {:06o}", p);
|
|
|
|
}
|
|
|
|
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(_) => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2015-01-25 08:03:14 +00:00
|
|
|
};
|
2016-04-26 02:55:34 +00:00
|
|
|
status
|
2014-07-10 20:49:20 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 06:02:48 +00:00
|
|
|
fn parse_radix(radix_str: Option<String>) -> Result<Radix, &'static str> {
|
|
|
|
match radix_str {
|
|
|
|
None => Ok(Radix::Octal),
|
2015-01-25 08:03:14 +00:00
|
|
|
Some(s) => {
|
|
|
|
let st = s.into_bytes();
|
|
|
|
if st.len() != 1 {
|
2015-01-26 06:02:48 +00:00
|
|
|
Err("Radix must be one of [d, o, b, x]\n")
|
2015-01-25 08:03:14 +00:00
|
|
|
} else {
|
2015-01-26 06:02:48 +00:00
|
|
|
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")
|
|
|
|
}
|
2015-01-25 08:03:14 +00:00
|
|
|
}
|
2015-01-26 06:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-10 20:49:20 +00:00
|
|
|
}
|
2016-04-26 02:55:34 +00:00
|
|
|
|
2015-01-26 06:39:49 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-04-26 02:05:16 +00:00
|
|
|
}
|