1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 21:17:46 +00:00

Merge pull request #485 from amty/master

NULL-pointer check in logname
This commit is contained in:
Heather 2015-01-08 08:53:32 +03:00
commit 0c89fcda25

View file

@ -28,10 +28,15 @@ extern {
pub fn getlogin() -> *const libc::c_char; pub fn getlogin() -> *const libc::c_char;
} }
unsafe fn get_userlogin() -> String { fn get_userlogin() -> Option<String> {
let login: *const libc::c_char = getlogin(); unsafe {
let login: *const libc::c_char = getlogin();
String::from_raw_buf(login as *const u8) if login.is_null() {
None
} else {
Some(String::from_raw_buf(login as *const u8))
}
}
} }
static NAME: &'static str = "logname"; static NAME: &'static str = "logname";
@ -77,8 +82,8 @@ pub fn uumain(args: Vec<String>) -> int {
} }
fn exec() { fn exec() {
unsafe { match get_userlogin() {
let userlogin = get_userlogin(); Some(userlogin) => println!("{}", userlogin),
println!("{}", userlogin); None => println!("{}: no login name", NAME)
} }
} }