diff --git a/src/lib.rs b/src/lib.rs index 20ef2f0..f6c9439 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,6 +33,7 @@ pub use netrc::{Authenticator, Netrc}; use std::fs; use std::io; use std::io::ErrorKind; +use std::iter::repeat; use std::path::{Path, PathBuf}; use std::result; @@ -85,11 +86,15 @@ impl Netrc { /// Look up the `NETRC` environment variable if it is defined else that the /// default `$HOME/.netrc` file. pub fn get_file() -> Option { - std::env::var("NETRC") - .map(|x| PathBuf::from(&x)) - .or(std::env::var("HOME").map(|h| Path::new(&h).join(".netrc"))) - .ok() - .and_then(|f| if f.exists() { Some(f) } else { None }) + let env_var = std::env::var("NETRC").ok().map(|file| PathBuf::from(file)); + + let user_home = ["HOME", "USERPROFILE"] + .iter() + .filter_map(|x| std::env::var(x).ok()) + .flat_map(|home| repeat(home).zip([".netrc", "_netrc"])) + .map(|(home, file)| PathBuf::from(home).join(file)); + + env_var.into_iter().chain(user_home).find(|f| f.exists()) } }