From cfc7b21498bf1bf177e10e4ebccec47cea77cbf6 Mon Sep 17 00:00:00 2001 From: benhirsch24 Date: Thu, 10 Jul 2014 13:49:20 -0700 Subject: [PATCH 1/7] 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/7] 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) { From 3739c82c956afa7a7e8d11fd29b8447e1d468c97 Mon Sep 17 00:00:00 2001 From: Keunwoo Lee Date: Sat, 24 Jan 2015 23:16:13 -0800 Subject: [PATCH 3/7] mv od/ -> src/ --- {od => src/od}/od.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {od => src/od}/od.rs (100%) diff --git a/od/od.rs b/src/od/od.rs similarity index 100% rename from od/od.rs rename to src/od/od.rs From 2d8e7f6decc6458aa55225ea61bec1702fd093a8 Mon Sep 17 00:00:00 2001 From: Keunwoo Lee Date: Sat, 24 Jan 2015 23:56:10 -0800 Subject: [PATCH 4/7] od: fix build Just the minimal stuff needed to make od build again. I have restrained myself from making more invasive changes. --- src/od/od.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/od/od.rs b/src/od/od.rs index 128d3699c..f0a153155 100644 --- a/src/od/od.rs +++ b/src/od/od.rs @@ -18,7 +18,7 @@ use std::io::File; #[deriving(Show)] enum Radix { Decimal, Hexadecimal, Octal, Binary } -pub fn uumain(args: Vec) -> int { +pub fn uumain(args: Vec) -> isize { 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"), @@ -31,23 +31,23 @@ pub fn uumain(args: Vec) -> int { getopts::optflag("v", "version", "output version information and exit."), ]; - let matches = match getopts::getopts(args.tail(), opts) { + let matches = match getopts::getopts(args.tail(), &opts) { Ok(m) => m, - Err(f) => fail!("Invalid options\n{}", f) + Err(f) => panic!("Invalid options\n{}", f) }; - let mut rad = Octal; + let mut rad = Radix::Octal; if matches.opt_present("A") { rad = parse_radix(matches.opt_str("A")); } else { - println!("{}", getopts::usage("od", opts)); + println!("{}", getopts::usage("od", &opts)); } let mut fname; match args.last() { Some(n) => fname = n, - None => { fail!("Need fname for now") ; } + None => { panic!("Need fname for now") ; } }; main(rad, fname.clone()); @@ -58,22 +58,22 @@ pub fn uumain(args: Vec) -> int { fn main(radix: Radix, fname: String) { let mut f = match File::open(&Path::new(fname)) { Ok(f) => f, - Err(e) => fail!("file error: {}", e) + Err(e) => panic!("file error: {}", e) }; let mut addr = 0; - let mut bytes = [0, .. 16]; + let bytes = &mut [b'\x00'; 16]; loop { match f.read(bytes) { Ok(n) => { print!("{:07o}", addr); match radix { - Decimal => { + Radix::Decimal => { }, - Octal => { + Radix::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; + let p: u16 = (bs[1] as u16) << 8 | bs[0] as u16; print!(" {:06o}", p); } if n % std::u16::BYTES == 1 { @@ -95,23 +95,23 @@ fn parse_radix(radix_str: Option) -> Radix { Some(s) => { let st = s.into_bytes(); if st.len() != 1 { - fail!("Radix must be one of [d, o, b, x]\n"); + panic!("Radix must be one of [d, o, b, x]\n"); } - let radix: char = *st.get(0) as char; + let radix: char = *(st.get(0).expect("byte string of length 1 lacks a 0th elem")) as char; if radix == 'd' { - Decimal + Radix::Decimal } else if radix == 'x' { - Hexadecimal + Radix::Hexadecimal } else if radix == 'o' { - Octal + Radix::Octal } else if radix == 'b' { - Binary + Radix::Binary } else { - fail!("Radix must be one of [d, o, b, x]\n"); + panic!("Radix must be one of [d, o, b, x]\n"); } }, - None => Octal + None => Radix::Octal }; rad From a6e5deaa166bbb7fe5d0af7bf5332d60a076edc5 Mon Sep 17 00:00:00 2001 From: Keunwoo Lee Date: Sun, 25 Jan 2015 00:03:14 -0800 Subject: [PATCH 5/7] od: whitespace fix (4-space indents, 99-column) --- src/od/od.rs | 172 +++++++++++++++++++++++++++------------------------ 1 file changed, 91 insertions(+), 81 deletions(-) diff --git a/src/od/od.rs b/src/od/od.rs index f0a153155..692485358 100644 --- a/src/od/od.rs +++ b/src/od/od.rs @@ -19,100 +19,110 @@ use std::io::File; enum Radix { Decimal, Hexadecimal, Octal, Binary } pub fn uumain(args: Vec) -> isize { - 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 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) { - Ok(m) => m, - Err(f) => panic!("Invalid options\n{}", f) - }; + let matches = match getopts::getopts(args.tail(), &opts) { + Ok(m) => m, + Err(f) => panic!("Invalid options\n{}", f) + }; - let mut rad = Radix::Octal; - if matches.opt_present("A") { - rad = parse_radix(matches.opt_str("A")); - } else { - println!("{}", getopts::usage("od", &opts)); - } + let mut rad = Radix::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 => { panic!("Need fname for now") ; } - }; + let mut fname; + match args.last() { + Some(n) => fname = n, + None => { panic!("Need fname for now") ; } + }; - main(rad, fname.clone()); + main(rad, fname.clone()); - 0 + 0 } fn main(radix: Radix, fname: String) { - let mut f = match File::open(&Path::new(fname)) { - Ok(f) => f, - Err(e) => panic!("file error: {}", e) - }; + let mut f = match File::open(&Path::new(fname)) { + Ok(f) => f, + Err(e) => panic!("file error: {}", e) + }; - let mut addr = 0; - let bytes = &mut [b'\x00'; 16]; - loop { - match f.read(bytes) { - Ok(n) => { - print!("{:07o}", addr); - match radix { - Radix::Decimal => { - }, - Radix::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; } - }; - }; + let mut addr = 0; + let bytes = &mut [b'\x00'; 16]; + loop { + match f.read(bytes) { + Ok(n) => { + print!("{:07o}", addr); + match radix { + Radix::Decimal => { + }, + Radix::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 { - panic!("Radix must be one of [d, o, b, x]\n"); - } + let rad = match radix_str { + Some(s) => { + let st = s.into_bytes(); + if st.len() != 1 { + 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; - if radix == 'd' { - Radix::Decimal - } else if radix == 'x' { - Radix::Hexadecimal - } else if radix == 'o' { - Radix::Octal - } else if radix == 'b' { - Radix::Binary - } else { - panic!("Radix must be one of [d, o, b, x]\n"); - } - }, - None => Radix::Octal - }; + 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 { + panic!("Radix must be one of [d, o, b, x]\n"); + } + }, + None => Radix::Octal + }; - rad + rad } From 19542747001f60592343da68c3ea067eda6ecfd7 Mon Sep 17 00:00:00 2001 From: Keunwoo Lee Date: Sun, 25 Jan 2015 00:17:38 -0800 Subject: [PATCH 6/7] od: fix warnings --- src/od/od.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/od/od.rs b/src/od/od.rs index 692485358..5a290ecea 100644 --- a/src/od/od.rs +++ b/src/od/od.rs @@ -1,4 +1,5 @@ #![crate_name = "od"] +#![allow(unstable)] /* * This file is part of the uutils coreutils package. @@ -15,7 +16,7 @@ extern crate collections; use collections::string::String; use std::io::File; -#[deriving(Show)] +#[derive(Show)] enum Radix { Decimal, Hexadecimal, Octal, Binary } pub fn uumain(args: Vec) -> isize { @@ -81,7 +82,7 @@ fn main(radix: Radix, fname: String) { }, Radix::Octal => { for b in range(0, n/std::u16::BYTES) { - let bs = bytes.slice(2*b, 2*b+2); + let bs = &bytes[2*b .. 2*b+2]; let p: u16 = (bs[1] as u16) << 8 | bs[0] as u16; print!(" {:06o}", p); } From 288179be49eee00e15ed19bd7147ba721c670e85 Mon Sep 17 00:00:00 2001 From: Keunwoo Lee Date: Tue, 27 Jan 2015 19:31:11 -0800 Subject: [PATCH 7/7] address Arcterus comments on uutils/coreutils PR 515 Mostly style things. --- README.md | 1 + src/od/od.rs | 13 ++++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index eaba3f293..c0cfb6a63 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ To do - mktemp - mv (almost done, one more option) - numfmt +- od (in progress, needs lots of work) - pathchk - pinky - pr diff --git a/src/od/od.rs b/src/od/od.rs index 5a290ecea..fca7f2962 100644 --- a/src/od/od.rs +++ b/src/od/od.rs @@ -39,7 +39,7 @@ pub fn uumain(args: Vec) -> isize { "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) { Ok(m) => m, @@ -78,16 +78,15 @@ fn main(radix: Radix, fname: String) { Ok(n) => { print!("{:07o}", addr); match radix { - Radix::Decimal => { - }, + Radix::Decimal => {}, Radix::Octal => { - for b in range(0, n/std::u16::BYTES) { - let bs = &bytes[2*b .. 2*b+2]; + for b in range(0, n / std::u16::BYTES) { + 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 % std::u16::BYTES == 1 { - print!(" {:06o}", bytes[n-1]); + print!(" {:06o}", bytes[n - 1]); } } _ => { } @@ -109,7 +108,7 @@ fn parse_radix(radix_str: Option) -> Radix { } let radix: char = *(st.get(0) - .expect("byte string of length 1 lacks a 0th elem")) as char; + .expect("byte string of length 1 lacks a 0th elem")) as char; if radix == 'd' { Radix::Decimal } else if radix == 'x' {