2013-12-26 10:49:31 +00:00
|
|
|
#[crate_id(name="dirname", vers="1.0.0", author="Derek Chiang")];
|
2013-12-03 09:29:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Derek Chiang <derekchiang93@gmail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2014-02-16 21:29:31 +00:00
|
|
|
extern crate extra;
|
|
|
|
extern crate getopts;
|
2013-12-03 09:29:32 +00:00
|
|
|
|
|
|
|
use std::os;
|
2014-02-04 07:33:53 +00:00
|
|
|
use std::io::print;
|
2013-12-03 09:29:32 +00:00
|
|
|
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
|
|
|
let program = args[0].clone();
|
|
|
|
let opts = ~[
|
2014-02-07 06:39:07 +00:00
|
|
|
getopts::optflag("z", "zero", "separate output with NUL rather than newline"),
|
|
|
|
getopts::optflag("", "help", "display this help and exit"),
|
|
|
|
getopts::optflag("", "version", "output version information and exit"),
|
2013-12-03 09:29:32 +00:00
|
|
|
];
|
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
let matches = match getopts::getopts(args.tail(), opts) {
|
2013-12-03 09:29:32 +00:00
|
|
|
Ok(m) => m,
|
2014-02-04 07:33:53 +00:00
|
|
|
Err(f) => fail!("Invalid options\n{}", f.to_err_msg())
|
2013-12-03 09:29:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if matches.opt_present("help") {
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("dirname {:s} - strip last component from file name", VERSION);
|
|
|
|
println!("");
|
|
|
|
println!("Usage:");
|
2013-12-03 09:29:32 +00:00
|
|
|
println!(" {0:s} [OPTION] NAME...", program);
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("");
|
2014-02-07 06:39:07 +00:00
|
|
|
print(getopts::usage("Output each NAME with its last non-slash component and trailing slashes
|
2013-12-03 23:39:23 +00:00
|
|
|
removed; if NAME contains no /'s, output '.' (meaning the current
|
2013-12-03 09:29:32 +00:00
|
|
|
directory).", opts));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches.opt_present("version") {
|
2014-01-13 09:05:02 +00:00
|
|
|
return println!("dirname version: {:s}", VERSION);
|
2013-12-03 09:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let separator = match matches.opt_present("zero") {
|
|
|
|
true => "\0",
|
|
|
|
false => "\n"
|
|
|
|
};
|
|
|
|
|
|
|
|
if !matches.free.is_empty() {
|
|
|
|
for path in matches.free.iter() {
|
2013-12-06 05:31:49 +00:00
|
|
|
let p = std::path::Path::new(path.clone());
|
2014-01-22 11:27:08 +00:00
|
|
|
let d = std::str::from_utf8(p.dirname());
|
|
|
|
if d.is_some() {
|
|
|
|
print(d.unwrap());
|
|
|
|
}
|
2013-12-03 09:29:32 +00:00
|
|
|
print(separator);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
println!("{0:s}: missing operand", program);
|
|
|
|
println!("Try '{0:s} --help' for more information.", program);
|
|
|
|
}
|
|
|
|
}
|