From 09af3ecaa2ca08d22ce0d041241bcb2ca7f6bfb9 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Sat, 18 Oct 2014 23:17:33 +0200 Subject: [PATCH] add nproc --- Makefile | 1 + README.md | 1 - src/nproc/nproc.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/nproc/nproc.rs diff --git a/Makefile b/Makefile index b46a86187..f6ef564c7 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,7 @@ PROGS := \ mkdir \ mv \ nl \ + nproc \ paste \ printenv \ pwd \ diff --git a/README.md b/README.md index e43146874..0ecaa96d4 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,6 @@ To do - mktemp - mv - nice -- nproc - numfmt - od - pathchk diff --git a/src/nproc/nproc.rs b/src/nproc/nproc.rs new file mode 100644 index 000000000..1212f6fa6 --- /dev/null +++ b/src/nproc/nproc.rs @@ -0,0 +1,51 @@ +#![crate_name = "nproc"] + +/* + * 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::os; + +static NAME : &'static str = "nproc"; +static VERSION : &'static str = "0.0.0"; + +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") { + println!("{} {}", NAME, VERSION); + println!(""); + println!("Usage:"); + println!(" {} [OPTIONS] NAME...", NAME); + println!(""); + print!("{}", getopts::usage("Print the number of cores available.", opts.as_slice()).as_slice()); + if matches.free.is_empty() { + return 1; + } + return 0; + } + + println!("{}", os::num_cpus()); + + return 0 +}