1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Common function to get user group info

This commit is contained in:
Vsevolod Velichko 2014-06-16 19:35:50 +04:00
parent 9f78e46484
commit 6efcc02c39

View file

@ -88,6 +88,7 @@ extern {
groups: *gid_t,
ngroups: *mut c_int) -> c_int;
pub fn getgrgid(gid: gid_t) -> *c_group;
pub fn getgrnam(name: *c_char) -> *c_group;
}
pub fn get_pw_from_args(free: &Vec<String>) -> Option<c_passwd> {
@ -108,7 +109,7 @@ pub fn get_pw_from_args(free: &Vec<String>) -> Option<c_passwd> {
// Passed the username as a string
} else {
let pw_pointer = unsafe {
getpwnam(username.as_slice().as_ptr() as *i8)
getpwnam(username.as_slice().to_c_str().unwrap() as *libc::c_char)
};
if pw_pointer.is_not_null() {
Some(unsafe { read(pw_pointer) })
@ -121,6 +122,21 @@ pub fn get_pw_from_args(free: &Vec<String>) -> Option<c_passwd> {
}
}
pub fn get_group(groupname: &str) -> Option<c_group> {
let group = if groupname.chars().all(|c| c.is_digit()) {
unsafe { getgrgid(from_str::<uid_t>(groupname).unwrap()) }
} else {
unsafe { getgrnam(groupname.to_c_str().unwrap() as *c_char) }
};
if group.is_not_null() {
Some(unsafe { read(group) })
}
else {
None
}
}
static NGROUPS: i32 = 20;
pub fn group(possible_pw: Option<c_passwd>, nflag: bool) {