mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 06:42:42 +00:00
Implement dirname
This commit is contained in:
parent
f03670b70a
commit
7b6880d348
2 changed files with 71 additions and 0 deletions
1
Makefile
1
Makefile
|
@ -9,6 +9,7 @@ RMFLAGS :=
|
|||
# Output names
|
||||
EXES := \
|
||||
cat \
|
||||
dirname \
|
||||
echo \
|
||||
env \
|
||||
false \
|
||||
|
|
70
dirname/dirname.rs
Normal file
70
dirname/dirname.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
#[link(name="dirname", vers="1.0.0", author="Derek Chiang")];
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
extern mod extra;
|
||||
|
||||
use std::os;
|
||||
use std::io::stderr;
|
||||
use extra::getopts::groups;
|
||||
|
||||
static VERSION: &'static str = "1.0.0";
|
||||
|
||||
fn main() {
|
||||
let args = os::args();
|
||||
let program = args[0].clone();
|
||||
let opts = ~[
|
||||
groups::optflag("z", "zero", "separate output with NUL rather than newline"),
|
||||
groups::optflag("", "help", "display this help and exit"),
|
||||
groups::optflag("", "version", "output version information and exit"),
|
||||
];
|
||||
|
||||
let matches = match groups::getopts(args.tail(), opts) {
|
||||
Ok(m) => m,
|
||||
Err(f) => {
|
||||
writeln!(&mut stderr() as &mut Writer,
|
||||
"Invalid options\n{}", f.to_err_msg());
|
||||
os::set_exit_status(1);
|
||||
return
|
||||
}
|
||||
};
|
||||
|
||||
if matches.opt_present("help") {
|
||||
println("dirname " + VERSION + " - strip last component from file name");
|
||||
println("");
|
||||
println("Usage:");
|
||||
println!(" {0:s} [OPTION] NAME...", program);
|
||||
println("");
|
||||
print(groups::usage("Output each NAME with its last non-slash component and trailing slashes\
|
||||
removed; if NAME contains no /'s, output '.' (meaning the current\
|
||||
directory).", opts));
|
||||
return;
|
||||
}
|
||||
|
||||
if matches.opt_present("version") {
|
||||
return println("dirname version: " + VERSION);
|
||||
}
|
||||
|
||||
let separator = match matches.opt_present("zero") {
|
||||
true => "\0",
|
||||
false => "\n"
|
||||
};
|
||||
|
||||
if !matches.free.is_empty() {
|
||||
for path in matches.free.iter() {
|
||||
let p = std::path::Path::init(path.clone());
|
||||
print(std::str::from_utf8_slice(p.dirname()));
|
||||
print(separator);
|
||||
}
|
||||
} else {
|
||||
println!("{0:s}: missing operand", program);
|
||||
println!("Try '{0:s} --help' for more information.", program);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue