From a007dadeda0fe26ec1e66081d3c44e487e8afdce Mon Sep 17 00:00:00 2001 From: Heather Date: Thu, 5 Dec 2013 15:36:08 +0400 Subject: [PATCH] simple pwd implementation --- Makefile | 1 + README.md | 1 - pwd/pwd.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 pwd/pwd.rs diff --git a/Makefile b/Makefile index faa70ebf8..dd8a2e248 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ EXES := \ env \ false \ printenv \ + pwd \ true \ wc \ whoami \ diff --git a/README.md b/README.md index 3c05e623c..8f9f1f4be 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,6 @@ To do - printf - prog-fprintf - ptx -- pwd - readlink - realpath - relpath diff --git a/pwd/pwd.rs b/pwd/pwd.rs new file mode 100644 index 000000000..7e2db9417 --- /dev/null +++ b/pwd/pwd.rs @@ -0,0 +1,51 @@ +#[link(name="pwd", vers="1.0.0", author="Heather Cynede")]; + +/* + * 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("", "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!("pwd {}", VERSION); + println(""); + println("Usage:"); + println!(" {0:s} [OPTION] NAME...", program); + println(""); + print(groups::usage("Print the full filename of the current working directory.", opts)); + } else if matches.opt_present("version") { + return println("pwd version: " + VERSION); + } else { + let cwd = std::os::getcwd(); + println!("{}", cwd.display()); + } +}