summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJared Neil <jaredneil@lucidchart.com>2019-10-31 12:09:12 -0600
committerManos Pitsidianakis <el13635@mail.ntua.gr>2019-11-03 21:48:59 +0200
commit7663fbed5dcb2209fbda53db50bf58c4782721ab (patch)
tree5fa15a51be416c5f75a9d9d6ec047861850b783d
parenta39fca567fe5dc4a7a75bcafaf7ebf40c9d059f7 (diff)
Fall back to uid if user is not found
Fixes #8
-rw-r--r--src/ui.rs26
1 files changed, 18 insertions, 8 deletions
diff --git a/src/ui.rs b/src/ui.rs
index 3aff635..3d2a713 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -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