coreutils/logname/logname.rs

86 lines
1.8 KiB
Rust
Raw Normal View History

2014-03-31 16:40:21 +00:00
#![crate_id(name="logname", version="1.0.0", author="Benoit Benedetti")]
2014-02-24 21:24:01 +00:00
/*
* This file is part of the uutils coreutils package.
*
* (c) Benoit Benedetti <benoit.benedetti@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/* last synced with: logname (GNU coreutils) 8.22 */
2014-03-31 16:40:21 +00:00
#![allow(non_camel_case_types)]
2014-02-24 21:24:01 +00:00
2014-03-31 16:40:21 +00:00
#![feature(macro_rules)]
2014-02-24 21:24:01 +00:00
extern crate getopts;
extern crate libc;
2014-02-24 21:24:01 +00:00
2014-05-23 12:28:40 +00:00
use std::io::print;
2014-02-24 21:24:01 +00:00
use std::str;
use libc::c_char;
2014-02-24 21:24:01 +00:00
#[path = "../common/util.rs"] mod util;
extern {
// POSIX requires using getlogin (or equivalent code)
pub fn getlogin() -> *const libc::c_char;
2014-02-24 21:24:01 +00:00
}
2014-05-25 09:20:52 +00:00
unsafe fn get_userlogin() -> String {
let login: *const libc::c_char = getlogin();
2014-02-24 21:24:01 +00:00
str::raw::from_c_str(login)
}
static NAME: &'static str = "logname";
static VERSION: &'static str = "1.0.0";
fn version() {
2014-05-23 12:28:40 +00:00
println!("{} {}", NAME, VERSION);
2014-02-24 21:24:01 +00:00
}
pub fn uumain(args: Vec<String>) -> int {
2014-05-16 08:32:58 +00:00
let program = args.get(0).clone();
2014-02-24 21:24:01 +00:00
//
// Argument parsing
//
2014-05-30 08:35:54 +00:00
let opts = [
2014-02-24 21:24:01 +00:00
getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit"),
];
let matches = match getopts::getopts(args.tail(), opts) {
Ok(m) => m,
2014-06-15 10:50:40 +00:00
Err(f) => crash!(1, "Invalid options\n{}", f)
2014-02-24 21:24:01 +00:00
};
if matches.opt_present("help") {
version();
println!("");
println!("Usage:");
println!(" {:s}", program);
println!("");
2014-05-17 10:32:14 +00:00
print(getopts::usage("print user's login name", opts).as_slice());
return 0;
2014-02-24 21:24:01 +00:00
}
if matches.opt_present("version") {
version();
return 0;
2014-02-24 21:24:01 +00:00
}
exec();
0
2014-02-24 21:24:01 +00:00
}
fn exec() {
unsafe {
let userlogin = get_userlogin();
println!("{:s}", userlogin);
}
}