From cfc7b21498bf1bf177e10e4ebccec47cea77cbf6 Mon Sep 17 00:00:00 2001 From: benhirsch24 Date: Thu, 10 Jul 2014 13:49:20 -0700 Subject: [PATCH 1/2] Started od, have at least octal working! --- Makefile | 1 + od/od.rs | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 od/od.rs diff --git a/Makefile b/Makefile index 1ec3b5612..8c90727ba 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ PROGS := \ hashsum \ mkdir \ nl \ + od \ paste \ printenv \ pwd \ diff --git a/od/od.rs b/od/od.rs new file mode 100644 index 000000000..242ee098c --- /dev/null +++ b/od/od.rs @@ -0,0 +1,113 @@ +#![crate_name = "od"] + +/* + * This file is part of the uutils coreutils package. + * + * (c) Ben Hirsch + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +extern crate getopts; +extern crate collections; + +use collections::string::String; +use std::slice::bytes; + +use std::io::File; + +#[deriving(Show)] +enum Radix { Decimal, Hexadecimal, Octal, Binary } + +pub fn uumain(args: Vec) -> int { + let opts = [ + getopts::optopt("A", "address-radix", "Select the base in which file offsets are printed.", "RADIX"), + getopts::optopt("j", "skip-bytes", "Skip bytes input bytes before formatting and writing.", "BYTES"), + ]; + + let matches = match getopts::getopts(args.tail(), opts) { + Ok(m) => m, + Err(f) => fail!("Invalid options\n{}", f) + }; + + + let mut rad = Octal; + if matches.opt_present("A") { + rad = parse_radix(matches.opt_str("A")); + } else { + println!("{}", getopts::usage("od", opts)); + } + + let mut fname; + match args.last() { + Some(n) => fname = n, + None => { fail!("Need fname for now") ; } + }; + + main(rad, fname.clone()); + + 0 +} + +fn main(radix: Radix, fname: String) { + let mut f = match File::open(&Path::new(fname)) { + Ok(f) => f, + Err(e) => fail!("file error: {}", e) + }; + + let mut addr = 0; + let mut bytes = [0, .. 16]; + loop { + match f.read(bytes) { + Ok(n) => { + print!("{:07o}", addr); + match radix { + Decimal => { + }, + Octal => { + for b in range(0, n/std::u16::BYTES) { + let bs = bytes.slice(2*b, 2*b+2); + let p: u16 = bs[1] as u16 << 8 | bs[0] as u16; + print!(" {:06o}", p); + } + if n % std::u16::BYTES == 1 { + print!(" {:06o}", bytes[n-1]); + } + } + _ => { } + }; + print!("\n"); + addr += n; + }, + Err(_) => { println!("{:07o}", addr); break; } + }; + }; +} + +fn parse_radix(radix_str: Option) -> Radix { + let rad = match radix_str { + Some(s) => { + let st = s.into_bytes(); + if st.len() != 1 { + fail!("Radix must be one of [d, o, b, x]\n"); + } + + let radix: char = *st.get(0) as char; + if radix == 'd' { + Decimal + } else if radix == 'x' { + Hexadecimal + } else if radix == 'o' { + Octal + } else if radix == 'b' { + Binary + } else { + fail!("Radix must be one of [d, o, b, x]\n"); + } + }, + None => Octal + }; + + rad +} From 10dc7a2deb50a1fa1df343e2b836f42d798204f1 Mon Sep 17 00:00:00 2001 From: benhirsch24 Date: Thu, 10 Jul 2014 16:17:09 -0700 Subject: [PATCH 2/2] Added more options, removed od from README --- README.md | 1 - od/od.rs | 9 +++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 335496c63..5db626d53 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,6 @@ To do - nice - nproc - numfmt -- od - pathchk - pinky - pr diff --git a/od/od.rs b/od/od.rs index 242ee098c..128d3699c 100644 --- a/od/od.rs +++ b/od/od.rs @@ -13,8 +13,6 @@ extern crate getopts; extern crate collections; use collections::string::String; -use std::slice::bytes; - use std::io::File; #[deriving(Show)] @@ -24,6 +22,13 @@ pub fn uumain(args: Vec) -> int { let opts = [ getopts::optopt("A", "address-radix", "Select the base in which file offsets are printed.", "RADIX"), getopts::optopt("j", "skip-bytes", "Skip bytes input bytes before formatting and writing.", "BYTES"), + getopts::optopt("N", "read-bytes", "limit dump to BYTES input bytes", "BYTES"), + getopts::optopt("S", "strings", "output strings of at least BYTES graphic chars. 3 is assumed when BYTES is not specified.", "BYTES"), + getopts::optopt("t", "format", "select output format or formats", "TYPE"), + getopts::optflag("v", "output-duplicates", "do not use * to mark line suppression"), + getopts::optopt("w", "width", "output BYTES bytes per output line. 32 is implied when BYTES is not specified.", "BYTES"), + getopts::optflag("h", "help", "display this help and exit."), + getopts::optflag("v", "version", "output version information and exit."), ]; let matches = match getopts::getopts(args.tail(), opts) {