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