Skip to content

Commit

Permalink
Rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
xen0n authored and soc committed May 11, 2019
1 parent 88294e4 commit 7c53ecd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ pub fn video_dir() -> Option<PathBuf> {
sys::video_dir()
}


#[cfg(test)]
mod tests {
#[test]
Expand Down
37 changes: 20 additions & 17 deletions src/unix.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
#![cfg(unix)]

use std::env;
use std::path::PathBuf;
use std::mem;
use std::ptr;
use std::ffi::CStr;
use std::ffi::OsString;
use std::mem;
use std::os::unix::ffi::OsStringExt;
use std::path::PathBuf;
use std::ptr;

extern crate libc;

// https://github.com/rust-lang/rust/blob/master/src/libstd/sys/unix/os.rs#L498
pub fn home_dir() -> Option<PathBuf> {
return env::var_os("HOME").and_then(|h| { if h.is_empty() { None } else { Some(h) } } ).or_else(|| unsafe {
fallback()
}).map(PathBuf::from);
return env::var_os("HOME")
.and_then(|h| if h.is_empty() { None } else { Some(h) })
.or_else(|| unsafe { fallback() })
.map(PathBuf::from);

#[cfg(any(target_os = "android",
target_os = "ios",
target_os = "emscripten"))]
unsafe fn fallback() -> Option<OsString> { None }
#[cfg(not(any(target_os = "android",
target_os = "ios",
target_os = "emscripten")))]
#[cfg(any(target_os = "android", target_os = "ios", target_os = "emscripten"))]
unsafe fn fallback() -> Option<OsString> {
None
}
#[cfg(not(any(target_os = "android", target_os = "ios", target_os = "emscripten")))]
unsafe fn fallback() -> Option<OsString> {
let amt = match libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) {
n if n < 0 => 512 as usize,
Expand All @@ -31,8 +30,13 @@ pub fn home_dir() -> Option<PathBuf> {
let mut buf = Vec::with_capacity(amt);
let mut passwd: libc::passwd = mem::zeroed();
let mut result = ptr::null_mut();
match libc::getpwuid_r(libc::getuid(), &mut passwd, buf.as_mut_ptr(),
buf.capacity(), &mut result) {
match libc::getpwuid_r(
libc::getuid(),
&mut passwd,
buf.as_mut_ptr(),
buf.capacity(),
&mut result,
) {
0 if !result.is_null() => {
let ptr = passwd.pw_dir as *const _;
let bytes = CStr::from_ptr(ptr).to_bytes();
Expand All @@ -41,9 +45,8 @@ pub fn home_dir() -> Option<PathBuf> {
} else {
Some(OsStringExt::from_vec(bytes.to_vec()))
}
},
}
_ => None,
}
}
}

0 comments on commit 7c53ecd

Please sign in to comment.