2014-01-07 00:54:02 +00:00
|
|
|
#[crate_id(name="basename", vers="1.0.0", author="Jimmy Lu")];
|
2013-12-03 04:41:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Jimmy Lu <jimmy.lu.2011@gmail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern mod extra;
|
|
|
|
|
|
|
|
use std::io::stderr;
|
|
|
|
use std::os;
|
|
|
|
use std::str;
|
|
|
|
use std::str::StrSlice;
|
|
|
|
use extra::getopts::groups;
|
|
|
|
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
|
|
|
|
2013-12-04 15:41:32 +00:00
|
|
|
let program = strip_dir(&args[ 0 ].clone());
|
2013-12-03 04:41:16 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Argument parsing
|
|
|
|
//
|
|
|
|
|
|
|
|
let opts = ~[
|
|
|
|
groups::optflag("h", "help", "display this help and exit"),
|
|
|
|
groups::optflag("V", "version", "output version information and exit"),
|
|
|
|
];
|
|
|
|
|
|
|
|
let matches = match groups::getopts(args.tail(), opts) {
|
|
|
|
Ok(m) => m,
|
|
|
|
Err(f) => {
|
2014-01-07 00:54:02 +00:00
|
|
|
writeln!(&mut stderr() as &mut Writer,
|
|
|
|
"Invalid options\n{}", f.to_err_msg());
|
2013-12-03 04:41:16 +00:00
|
|
|
os::set_exit_status(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-04 15:41:32 +00:00
|
|
|
if matches.opt_present("help") {
|
|
|
|
println!("Usage: {0:s} NAME [SUFFIX]", program);
|
|
|
|
println!(" or: {0:s} OPTION", program);
|
|
|
|
println("Print NAME with any leading directory components removed.");
|
|
|
|
println("If specified, also remove a trailing SUFFIX.");
|
2013-12-03 04:41:16 +00:00
|
|
|
|
2013-12-04 15:41:32 +00:00
|
|
|
print(groups::usage("", opts));
|
2013-12-03 04:41:16 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-04 15:41:32 +00:00
|
|
|
if matches.opt_present("version") {
|
|
|
|
println(program + " " + VERSION);
|
2013-12-03 04:41:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// too few arguments
|
|
|
|
if args.len() < 2 {
|
2013-12-04 15:41:32 +00:00
|
|
|
println(program + ": missing operand");
|
|
|
|
println("Try `" + program + " --help' for more information.");
|
2013-12-03 04:41:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// too many arguments
|
|
|
|
else if args.len() > 3 {
|
2013-12-04 15:41:32 +00:00
|
|
|
println(program + ": extra operand `" + args[ 3 ] + "'");
|
|
|
|
println("Try `" + program + " --help' for more information.");
|
2013-12-03 04:41:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Main Program Processing
|
|
|
|
//
|
|
|
|
|
|
|
|
let fullname = args[ 1 ].clone();
|
|
|
|
|
2013-12-04 15:41:32 +00:00
|
|
|
let mut name = strip_dir(&fullname);
|
2013-12-03 04:41:16 +00:00
|
|
|
|
|
|
|
if args.len() > 2 {
|
|
|
|
let suffix = args[ 2 ].clone();
|
2013-12-04 15:41:32 +00:00
|
|
|
name = strip_suffix(&name, &suffix);
|
2013-12-03 04:41:16 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 15:41:32 +00:00
|
|
|
println(name);
|
2013-12-03 04:41:16 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 15:41:32 +00:00
|
|
|
fn strip_dir(fullname :&~str) -> ~str {
|
2013-12-03 04:41:16 +00:00
|
|
|
let mut name = ~"";
|
|
|
|
|
2014-01-07 00:54:02 +00:00
|
|
|
for c in fullname.chars_rev() {
|
2013-12-03 04:41:16 +00:00
|
|
|
if c == '/' || c == '\\' {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2013-12-04 15:41:32 +00:00
|
|
|
name = str::from_char(c) + name;
|
2013-12-03 04:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fullname.clone();
|
|
|
|
}
|
|
|
|
|
2013-12-04 15:41:32 +00:00
|
|
|
fn strip_suffix(name: &~str, suffix: &~str) -> ~str {
|
2013-12-03 04:41:16 +00:00
|
|
|
if name == suffix {
|
|
|
|
return name.clone();
|
|
|
|
}
|
|
|
|
|
2013-12-04 15:41:32 +00:00
|
|
|
if name.ends_with(*suffix) {
|
|
|
|
return name.slice_to(name.len() - suffix.len()).into_owned();
|
2013-12-03 04:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return name.clone();
|
|
|
|
}
|