diff options
author | Jared Neil <jaredneil@lucidchart.com> | 2019-10-31 12:09:12 -0600 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2019-11-03 21:48:59 +0200 |
commit | 7663fbed5dcb2209fbda53db50bf58c4782721ab (patch) | |
tree | 5fa15a51be416c5f75a9d9d6ec047861850b783d | |
parent | a39fca567fe5dc4a7a75bcafaf7ebf40c9d059f7 (diff) |
Fall back to uid if user is not found
Fixes #8
-rw-r--r-- | src/ui.rs | 26 |
1 files changed, 18 insertions, 8 deletions
@@ -47,7 +47,7 @@ pub mod username { use libc; use std::ptr::null_mut; /* taken from whoami-0.1.1 */ - fn getpwuid(pw_uid: u32, buffer: &mut [i8; 16384]) -> libc::passwd { + fn getpwuid(pw_uid: u32, buffer: &mut [i8; 16384]) -> Option<libc::passwd> { let mut pwentp = null_mut(); #[cfg(any( target_os = "macos", @@ -74,7 +74,11 @@ pub mod username { libc::getpwuid_r(pw_uid, &mut pwent, buffer, 16384, &mut pwentp); } - pwent + if pwentp.is_null() { + None + } else { + Some(pwent) + } } #[cfg(target_os = "linux")] { @@ -91,8 +95,11 @@ pub mod username { unsafe { libc::getpwuid_r(pw_uid, &mut pwent, buffer.as_mut_ptr(), 16384, &mut pwentp); } - - pwent + if pwentp.is_null() { + None + } else { + Some(pwent) + } } } @@ -102,10 +109,13 @@ pub mod username { let string; unsafe { - string = ::std::ffi::CStr::from_ptr(pwent.pw_name) - .to_str() - .unwrap_or_else(|_| "") - .to_string(); + string = match pwent { + None => uid.to_string(), + Some(p) => ::std::ffi::CStr::from_ptr(p.pw_name) + .to_str() + .unwrap_or_else(|_| "") + .to_string() + } } string |