From 7b6880d3489d38a8461d1ba3f8935cea5bf0be47 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Tue, 3 Dec 2013 04:29:32 -0500 Subject: [PATCH 1/3] Implement dirname --- Makefile | 1 + dirname/dirname.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 dirname/dirname.rs diff --git a/Makefile b/Makefile index f225ed4d7..faa70ebf8 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ RMFLAGS := # Output names EXES := \ cat \ + dirname \ echo \ env \ false \ diff --git a/dirname/dirname.rs b/dirname/dirname.rs new file mode 100644 index 000000000..03357c022 --- /dev/null +++ b/dirname/dirname.rs @@ -0,0 +1,70 @@ +#[link(name="dirname", vers="1.0.0", author="Derek Chiang")]; + +/* + * This file is part of the uutils coreutils package. + * + * (c) Derek Chiang + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +extern mod extra; + +use std::os; +use std::io::stderr; +use extra::getopts::groups; + +static VERSION: &'static str = "1.0.0"; + +fn main() { + let args = os::args(); + let program = args[0].clone(); + let opts = ~[ + groups::optflag("z", "zero", "separate output with NUL rather than newline"), + groups::optflag("", "help", "display this help and exit"), + groups::optflag("", "version", "output version information and exit"), + ]; + + let matches = match groups::getopts(args.tail(), opts) { + Ok(m) => m, + Err(f) => { + writeln!(&mut stderr() as &mut Writer, + "Invalid options\n{}", f.to_err_msg()); + os::set_exit_status(1); + return + } + }; + + if matches.opt_present("help") { + println("dirname " + VERSION + " - strip last component from file name"); + println(""); + println("Usage:"); + println!(" {0:s} [OPTION] NAME...", program); + println(""); + print(groups::usage("Output each NAME with its last non-slash component and trailing slashes\ +removed; if NAME contains no /'s, output '.' (meaning the current\ +directory).", opts)); + return; + } + + if matches.opt_present("version") { + return println("dirname version: " + VERSION); + } + + let separator = match matches.opt_present("zero") { + true => "\0", + false => "\n" + }; + + if !matches.free.is_empty() { + for path in matches.free.iter() { + let p = std::path::Path::init(path.clone()); + print(std::str::from_utf8_slice(p.dirname())); + print(separator); + } + } else { + println!("{0:s}: missing operand", program); + println!("Try '{0:s} --help' for more information.", program); + } +} From d96e104a87aa3f05f7e80575fd55d52a2c260024 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Tue, 3 Dec 2013 04:31:20 -0500 Subject: [PATCH 2/3] Remove dirname from to-do list --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 0fe404faa..3c05e623c 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,6 @@ To do - dd - df - dircolors -- dirname - du - expand - expr From 94dd3ff7d392847f82ca3406a0a47d742e6fcee3 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Tue, 3 Dec 2013 18:39:23 -0500 Subject: [PATCH 3/3] Remove unnecessary escaping --- dirname/dirname.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dirname/dirname.rs b/dirname/dirname.rs index 03357c022..e6e8222ed 100644 --- a/dirname/dirname.rs +++ b/dirname/dirname.rs @@ -42,8 +42,8 @@ fn main() { println("Usage:"); println!(" {0:s} [OPTION] NAME...", program); println(""); - print(groups::usage("Output each NAME with its last non-slash component and trailing slashes\ -removed; if NAME contains no /'s, output '.' (meaning the current\ + print(groups::usage("Output each NAME with its last non-slash component and trailing slashes +removed; if NAME contains no /'s, output '.' (meaning the current directory).", opts)); return; }