Skip to content

Commit

Permalink
chore: prioritize $USER on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
XOR-op committed Sep 12, 2024
1 parent 9c0f6f4 commit 0a5d612
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions boltconn/src/platform/sys/linux_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,20 @@ impl Drop for SystemDnsHandle {
}

pub fn get_user_info() -> Option<(String, libc::uid_t, libc::gid_t)> {
let user_name = unsafe { libc::getlogin() };
if user_name.is_null() {
return None;
}
let name = unsafe { CStr::from_ptr(user_name) }
.to_string_lossy()
.into_owned();
let user_info = unsafe { libc::getpwnam(user_name) };
let (name, user_info) = if let Ok(n) = std::env::var("USER") {
let user_info = unsafe { libc::getpwnam(n.as_ptr() as *const i8) };
(n, user_info)
} else {
let user_name = unsafe { libc::getlogin() };
if user_name.is_null() {
return None;
}
let name = unsafe { CStr::from_ptr(user_name) }
.to_string_lossy()
.into_owned();
let user_info = unsafe { libc::getpwnam(user_name) };
(name, user_info)
};
if user_info.is_null() {
return None;
}
Expand Down

0 comments on commit 0a5d612

Please sign in to comment.