Merge branch 'all-users' of https://github.com/polachok/rust-users into polachok-all-users

# Conflicts:
#	src/lib.rs

I put the users iterator functionality into the 'base' module, where it belongs now (but didn't when the code was written)
This commit is contained in:
Ben S
2016-01-29 11:20:07 +00:00
3 changed files with 41 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
extern crate users;
use users::{User, AllUsers};
fn main() {
let mut users: Vec<User> = unsafe { AllUsers::new() }.collect();
users.sort_by(|a, b| a.uid().cmp(&b.uid()));
for user in users {
println!("User {} has name {}", user.uid(), user.name());
}
}
+29
View File
@@ -90,6 +90,10 @@ extern {
fn getgid() -> gid_t;
fn getegid() -> gid_t;
fn setpwent();
fn getpwent() -> *const c_passwd;
fn endpwent();
}
@@ -385,6 +389,31 @@ pub fn get_effective_groupname() -> Option<String> {
}
pub struct AllUsers(());
impl AllUsers {
pub unsafe fn new() -> AllUsers {
unsafe { setpwent() };
AllUsers(())
}
}
impl Drop for AllUsers {
fn drop(&mut self) {
unsafe { endpwent() };
}
}
impl Iterator for AllUsers {
type Item = User;
fn next(&mut self) -> Option<User> {
unsafe { passwd_to_user(getpwent()) }
}
}
/// OS-specific extensions to users and groups.
///
/// Every OS has a different idea of what data a user or a group comes with.
+1
View File
@@ -120,6 +120,7 @@ pub use base::{get_current_uid, get_current_username};
pub use base::{get_effective_uid, get_effective_username};
pub use base::{get_current_gid, get_current_groupname};
pub use base::{get_effective_gid, get_effective_groupname};
pub use base::AllUsers;
pub mod cache;