diff --git a/src/whoami/platform/unix.rs b/src/whoami/platform/unix.rs index e69034bfe..3250edc91 100644 --- a/src/whoami/platform/unix.rs +++ b/src/whoami/platform/unix.rs @@ -16,9 +16,10 @@ extern { pub fn geteuid() -> libc::uid_t; } -pub unsafe fn getusername() -> String { +pub unsafe fn getusername() -> Result { let passwd: *const c_passwd = getpwuid(geteuid()); let pw_name: *const libc::c_char = (*passwd).pw_name; - String::from_utf8_lossy(::std::ffi::CStr::from_ptr(pw_name).to_bytes()).to_string() + let username = String::from_utf8_lossy(::std::ffi::CStr::from_ptr(pw_name).to_bytes()).to_string(); + Ok(username) } diff --git a/src/whoami/platform/windows.rs b/src/whoami/platform/windows.rs index 9676d6d18..819da17bc 100644 --- a/src/whoami/platform/windows.rs +++ b/src/whoami/platform/windows.rs @@ -18,11 +18,12 @@ use std::ffi::OsString; use std::os::windows::ffi::OsStringExt; use self::wide::FromWide; -pub unsafe fn getusername() -> String { +pub unsafe fn getusername() -> Result { let mut buffer: [winapi::WCHAR; winapi::UNLEN as usize + 1] = mem::uninitialized(); let mut len = buffer.len() as winapi::DWORD; if advapi32::GetUserNameW(buffer.as_mut_ptr(), &mut len) == 0 { - crash!(1, "failed to get username"); + return Err("failed to get username".to_string()) } - String::from_wide(&buffer[..len as usize - 1]) + let username = String::from_wide(&buffer[..len as usize - 1]); + Ok(username) } diff --git a/src/whoami/whoami.rs b/src/whoami/whoami.rs index 5216688bf..fdfc4f9ea 100644 --- a/src/whoami/whoami.rs +++ b/src/whoami/whoami.rs @@ -54,7 +54,9 @@ pub fn uumain(args: Vec) -> i32 { pub fn exec() { unsafe { - let username = platform::getusername(); - println!("{}", username); + match platform::getusername() { + Ok(username) => println!("{}", username), + Err(msg) => crash!(libc::EXIT_FAILURE, "{}", msg), + } } }