1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/// Return the path of the user's home directory.
pub fn home_dir() -> Option<std::path::PathBuf> {
    #[cfg(target_family = "windows")]
    {
        use windows_sys::Win32::{UI::Shell::*, *};

        let mut p = std::ptr::null_mut();
        let r = if unsafe { SHGetKnownFolderPath(&FOLDERID_Profile, 0, 0, &mut p) } == 0 {
            let w = unsafe { core::slice::from_raw_parts(p, Globalization::lstrlenW(p) as _) };
            let o: std::ffi::OsString = std::os::windows::ffi::OsStringExt::from_wide(w);
            Some(o.into())
        } else {
            None
        };
        unsafe { System::Com::CoTaskMemFree(p as _) }
        r
    }

    #[cfg(not(target_family = "windows"))]
    std::env::var_os("HOME").map(Into::into)
}