diff --git a/examples/os.rs b/examples/os.rs index e538c45..5829625 100644 --- a/examples/os.rs +++ b/examples/os.rs @@ -1,6 +1,7 @@ extern crate users; use users::{Users, Groups, UsersCache}; use users::os::unix::{UserExt, GroupExt}; +//use users::os::bsd::UserExt as BSDUserExt; fn main() { let cache = UsersCache::new(); @@ -13,6 +14,11 @@ fn main() { println!("Your shell is {}", you.shell().display()); println!("Your home directory is {}", you.home_dir().display()); + // The two fields below are only available on BSD systems. + // Linux systems don’t have the fields in their `passwd` structs! + //println!("Your password change timestamp is {}", you.password_change_time()); + //println!("Your password expiry timestamp is {}", you.password_expire_time()); + let primary_group = cache.get_group_by_gid(you.primary_group_id()).expect("No entry for your primary group!"); println!("Your primary group has ID {} and name {}", primary_group.gid(), primary_group.name()); diff --git a/src/base.rs b/src/base.rs index 397af53..11650fd 100644 --- a/src/base.rs +++ b/src/base.rs @@ -555,6 +555,26 @@ pub mod os { } } + /// BSD-specific accessors for `User`s. + pub trait UserExt { + + /// Returns this user's password change timestamp. + fn password_change_time(&self) -> time_t; + + /// Returns this user's password expiry timestamp. + fn password_expire_time(&self) -> time_t; + } + + impl UserExt for User { + fn password_change_time(&self) -> time_t { + self.extras.change.clone() + } + + fn password_expire_time(&self) -> time_t { + self.extras.expire.clone() + } + } + impl Default for UserExtras { fn default() -> UserExtras { UserExtras {