simplify home_dir by removing unnecessary getpwuid_r wrapper

This commit is contained in:
Shawn Walker-Salas 2017-02-15 22:52:47 -08:00
parent ebf70a9a20
commit 5789539311
1 changed files with 9 additions and 18 deletions

View File

@ -483,30 +483,21 @@ pub fn home_dir() -> Option<PathBuf> {
target_os = "nacl", target_os = "nacl",
target_os = "emscripten")))] target_os = "emscripten")))]
unsafe fn fallback() -> Option<OsString> { unsafe fn fallback() -> Option<OsString> {
unsafe fn getpwduid_r(me: libc::uid_t, passwd: &mut libc::passwd,
buf: &mut Vec<c_char>) -> Option<()> {
let mut result = ptr::null_mut();
match libc::getpwuid_r(me, passwd, buf.as_mut_ptr(),
buf.capacity(),
&mut result) {
0 if !result.is_null() => Some(()),
_ => None
}
}
let amt = match libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) { let amt = match libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) {
n if n < 0 => 512 as usize, n if n < 0 => 512 as usize,
n => n as usize, n => n as usize,
}; };
let mut buf = Vec::with_capacity(amt); let mut buf = Vec::with_capacity(amt);
let mut passwd: libc::passwd = mem::zeroed(); let mut passwd: libc::passwd = mem::zeroed();
let mut result = ptr::null_mut();
if getpwduid_r(libc::getuid(), &mut passwd, &mut buf).is_some() { match libc::getpwuid_r(libc::getuid(), &mut passwd, buf.as_mut_ptr(),
let ptr = passwd.pw_dir as *const _; buf.capacity(), &mut result) {
let bytes = CStr::from_ptr(ptr).to_bytes().to_vec(); 0 if !result.is_null() => {
Some(OsStringExt::from_vec(bytes)) let ptr = passwd.pw_dir as *const _;
} else { let bytes = CStr::from_ptr(ptr).to_bytes().to_vec();
None Some(OsStringExt::from_vec(bytes))
},
_ => None,
} }
} }
} }