From dd4ace32513efd9e836ad9d0491684d78bcf375a Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Sun, 22 Jun 2014 16:28:51 +0200 Subject: [PATCH] Implement link --- Makefile | 1 + link/link.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ uutils/uutils.rs | 2 ++ 3 files changed, 70 insertions(+) create mode 100644 link/link.rs diff --git a/Makefile b/Makefile index a5f4de24f..b07e19192 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,7 @@ PROGS := \ false \ fmt \ fold \ + link \ hashsum \ mkdir \ nl \ diff --git a/link/link.rs b/link/link.rs new file mode 100644 index 000000000..033031635 --- /dev/null +++ b/link/link.rs @@ -0,0 +1,67 @@ +#![crate_id(name="link", vers="1.0.0", author="Michael Gehring")] +#![feature(macro_rules)] + +/* + * This file is part of the uutils coreutils package. + * + * (c) Michael Gehring + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +extern crate getopts; + +use std::io::fs::link; +use std::os; +use std::path::Path; + +#[path="../common/util.rs"] +mod util; + +static NAME : &'static str = "link"; +static VERSION : &'static str = "1.0.0"; + +#[allow(dead_code)] +fn main() { os::set_exit_status(uumain(os::args())); } + +pub fn uumain(args: Vec) -> int { + let opts = [ + 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(err) => fail!("{}", err), + }; + + if matches.opt_present("version") { + println!("{} {}", NAME, VERSION); + return 0; + } + + if matches.opt_present("help") || matches.free.len() != 2 { + println!("{} {}", NAME, VERSION); + println!(""); + println!("Usage:"); + println!(" {} [OPTIONS] FILE1 FILE2", NAME); + println!(""); + print!("{}", getopts::usage("Create a link named FILE2 to FILE1.", opts.as_slice()).as_slice()); + if matches.free.len() != 2 { + return 1; + } + return 0; + } + + let old = Path::new(matches.free.get(0).as_slice()); + let new = Path::new(matches.free.get(1).as_slice()); + + match link(&old, &new) { + Ok(_) => 0, + Err(err) => { + show_error!("{}", err); + 1 + } + } +} diff --git a/uutils/uutils.rs b/uutils/uutils.rs index 0615edcab..2369dbf56 100644 --- a/uutils/uutils.rs +++ b/uutils/uutils.rs @@ -31,6 +31,7 @@ extern crate hostid; extern crate hostname; extern crate id; extern crate kill; +extern crate link; extern crate logname; extern crate md5sum; extern crate mkdir; @@ -92,6 +93,7 @@ fn util_map() -> HashMap<&str, fn(Vec) -> int> { map.insert("hostname", hostname::uumain); map.insert("id", id::uumain); map.insert("kill", kill::uumain); + map.insert("link", link::uumain); map.insert("logname", logname::uumain); map.insert("md5sum", md5sum::uumain); map.insert("mkdir", mkdir::uumain);