coreutils/src/whoami/whoami.rs

67 lines
1.6 KiB
Rust
Raw Normal View History

#![crate_name = "uu_whoami"]
2013-10-18 12:00:38 +00:00
/*
* This file is part of the uutils coreutils package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/* last synced with: whoami (GNU coreutils) 8.21 */
extern crate getopts;
2013-10-18 12:00:38 +00:00
#[macro_use]
extern crate uucore;
use getopts::Options;
2015-04-27 21:31:53 +00:00
use std::io::Write;
2015-04-27 21:31:53 +00:00
mod platform;
2013-10-18 12:00:38 +00:00
static NAME: &'static str = "whoami";
2015-11-25 09:52:10 +00:00
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
pub fn uumain(args: Vec<String>) -> i32 {
let mut opts = Options::new();
opts.optflag("h", "help", "display this help and exit");
opts.optflag("V", "version", "output version information and exit");
let matches = match opts.parse(&args[1..]) {
2013-10-18 12:00:38 +00:00
Ok(m) => m,
2014-06-15 10:50:40 +00:00
Err(f) => crash!(1, "{}", f),
2013-10-18 12:00:38 +00:00
};
2013-10-22 12:22:10 +00:00
if matches.opt_present("help") {
println!("{} {}", NAME, VERSION);
println!("");
println!("Usage:");
println!(" {} [OPTIONS]", NAME);
println!("");
println!("{}", opts.usage("print effective userid"));
return 0;
2013-10-18 12:00:38 +00:00
}
2013-10-22 12:22:10 +00:00
if matches.opt_present("version") {
println!("{} {}", NAME, VERSION);
return 0;
2013-10-18 12:00:38 +00:00
}
exec();
0
2013-10-18 12:00:38 +00:00
}
pub fn exec() {
unsafe {
match platform::getusername() {
Ok(username) => println!("{}", username),
2015-09-18 09:51:59 +00:00
Err(err) => match err.raw_os_error() {
Some(0) | None => crash!(1, "failed to get username"),
Some(_) => crash!(1, "failed to get username: {}", err),
}
}
2013-10-18 12:00:38 +00:00
}
}