2 unstable releases

0.2.1 Aug 27, 2023
0.1.0 Aug 13, 2023

#37 in Configuration

Download history 473/week @ 2024-01-03 1456/week @ 2024-01-10 2873/week @ 2024-01-17 3676/week @ 2024-01-24 2655/week @ 2024-01-31 3876/week @ 2024-02-07 3851/week @ 2024-02-14 3729/week @ 2024-02-21 3889/week @ 2024-02-28 2842/week @ 2024-03-06 4194/week @ 2024-03-13 7162/week @ 2024-03-20 6708/week @ 2024-03-27 8352/week @ 2024-04-03 7756/week @ 2024-04-10 5800/week @ 2024-04-17

29,923 downloads per month
Used in 12 crates (11 directly)

MIT/Apache

26KB
262 lines

homedir

This crate exists to provide a portable method to getting any user's home directory. The API is rather simple: there are two main functions, get_home and get_my_home. The former can get the home directory of any user provided you have their username. The latter can get the home directory of the user executing this process.

This crate aims to work on both Windows and Unix systems. However, Unix systems do not have a unified API. This crate may not work on Unix systems which do not have the getpwnam_r(3), getpwuid_r(3), and getuid(2) functions.

Usage

This crate is on crates.io and can be used by executing cargo add homedir or adding the following to the dependencies in your Cargo.toml file.

[dependencies]
homedir = "0.2.1"

Examples

Get the process' user's home directory.

use homedir::get_my_home;

// This assumes that the process' user has "/home/jpetersen" as home directory.
assert_eq!(
    std::path::Path::new("/home/jpetersen"),
    get_my_home().unwrap().unwrap().as_path()
);

Get an arbitrary user's home directory.

use homedir::get_home;

// This assumes there is a user named `Administrator` which has
// `C:\Users\Administrator` as a home directory.
assert_eq!(
    std::path::Path::new("C:\\Users\\Administrator"),
    get_home("Administrator").unwrap().unwrap().as_path()
);
assert!(get_home("NonExistentUser").unwrap().is_none());

The full documentation of the crate is available on the docs.rs page.

Licensing

Licensed under either of

at your option.

Contribution

Unless you explicitely state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Feel free to put a copyright header in your name in any files you contribute to.

Copyright (C) 2023 James Petersen m@jamespetersen.ca.

Dependencies

~1–34MB
~469K SLoC