2014-03-31 16:40:21 +00:00
|
|
|
#![feature(macro_rules)]
|
|
|
|
#![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.
|
|
|
|
*/
|
|
|
|
|
2014-02-16 21:29:31 +00:00
|
|
|
extern crate getopts;
|
2014-04-07 22:43:34 +00:00
|
|
|
extern crate libc;
|
2013-12-03 04:41:16 +00:00
|
|
|
|
2014-02-04 05:58:53 +00:00
|
|
|
use std::io::{print, println};
|
2013-12-03 04:41:16 +00:00
|
|
|
use std::os;
|
|
|
|
use std::str::StrSlice;
|
|
|
|
|
2014-02-23 22:17:48 +00:00
|
|
|
#[path = "../common/util.rs"]
|
2014-02-07 06:39:07 +00:00
|
|
|
mod util;
|
2013-12-03 04:41:16 +00:00
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
static NAME: &'static str = "basename";
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
2014-02-05 10:23:53 +00:00
|
|
|
|
2013-12-03 04:41:16 +00:00
|
|
|
fn main() {
|
2014-05-23 12:28:40 +00:00
|
|
|
let args = os::args();
|
|
|
|
let program = strip_dir(args.get(0).as_slice());
|
2013-12-03 04:41:16 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Argument parsing
|
|
|
|
//
|
|
|
|
let opts = ~[
|
2014-02-07 06:39:07 +00:00
|
|
|
getopts::optflag("h", "help", "display this help and exit"),
|
|
|
|
getopts::optflag("V", "version", "output version information and exit"),
|
2013-12-03 04:41:16 +00:00
|
|
|
];
|
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
let matches = match getopts::getopts(args.tail(), opts) {
|
2014-02-04 05:58:53 +00:00
|
|
|
Ok(m) => m,
|
2014-02-05 10:23:53 +00:00
|
|
|
Err(f) => crash!(1, "Invalid options\n{}", f.to_err_msg())
|
2013-12-03 04:41:16 +00:00
|
|
|
};
|
|
|
|
|
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);
|
2014-01-13 09:05:02 +00:00
|
|
|
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
|
|
|
|
2014-05-17 10:32:14 +00:00
|
|
|
print(getopts::usage("", opts).as_slice());
|
2013-12-03 04:41:16 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-04 15:41:32 +00:00
|
|
|
if matches.opt_present("version") {
|
2014-05-23 12:28:40 +00:00
|
|
|
println!("{} {}", program, VERSION);
|
2013-12-03 04:41:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// too few arguments
|
|
|
|
if args.len() < 2 {
|
2014-05-23 12:28:40 +00:00
|
|
|
println!("{}: {}", program, "missing operand");
|
|
|
|
println!("Try '{} --help' for more information.", program);
|
2013-12-03 04:41:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// too many arguments
|
|
|
|
else if args.len() > 3 {
|
2014-05-23 12:28:40 +00:00
|
|
|
println!("{}: extra operand '{}'", program, args.get(3));
|
|
|
|
println!("Try '{} --help' for more information.", program);
|
2013-12-03 04:41:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Main Program Processing
|
|
|
|
//
|
|
|
|
|
2014-05-23 12:28:40 +00:00
|
|
|
let fullname = args.get(1);
|
2013-12-03 04:41:16 +00:00
|
|
|
|
2014-05-23 12:28:40 +00:00
|
|
|
let mut name = strip_dir(fullname.as_slice());
|
2013-12-03 04:41:16 +00:00
|
|
|
|
|
|
|
if args.len() > 2 {
|
2014-05-16 08:32:58 +00:00
|
|
|
let suffix = args.get(2).clone();
|
2014-05-23 12:28:40 +00:00
|
|
|
name = strip_suffix(name.as_slice(), suffix.as_slice());
|
2013-12-03 04:41:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 12:28:40 +00:00
|
|
|
println(name.as_slice());
|
2013-12-03 04:41:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 09:20:52 +00:00
|
|
|
fn strip_dir(fullname: &str) -> String {
|
|
|
|
let mut name = String::new();
|
2013-12-03 04:41:16 +00:00
|
|
|
|
2014-05-07 06:25:49 +00:00
|
|
|
for c in fullname.chars().rev() {
|
2013-12-03 04:41:16 +00:00
|
|
|
if c == '/' || c == '\\' {
|
2014-05-07 06:25:49 +00:00
|
|
|
break;
|
2013-12-03 04:41:16 +00:00
|
|
|
}
|
2014-05-07 06:25:49 +00:00
|
|
|
name.push_char(c);
|
2013-12-03 04:41:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-07 06:25:49 +00:00
|
|
|
return name.as_slice().chars().rev().collect();
|
2013-12-03 04:41:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 09:20:52 +00:00
|
|
|
fn strip_suffix(name: &str, suffix: &str) -> String {
|
2013-12-03 04:41:16 +00:00
|
|
|
if name == suffix {
|
2014-05-23 12:28:40 +00:00
|
|
|
return name.into_strbuf();
|
2013-12-03 04:41:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 12:28:40 +00:00
|
|
|
if name.ends_with(suffix) {
|
|
|
|
return name.slice_to(name.len() - suffix.len()).into_strbuf();
|
2013-12-03 04:41:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 12:28:40 +00:00
|
|
|
return name.into_strbuf();
|
2013-12-03 04:41:16 +00:00
|
|
|
}
|