mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
Common function to get user group info
This commit is contained in:
parent
9f78e46484
commit
6efcc02c39
1 changed files with 17 additions and 1 deletions
|
@ -88,6 +88,7 @@ extern {
|
||||||
groups: *gid_t,
|
groups: *gid_t,
|
||||||
ngroups: *mut c_int) -> c_int;
|
ngroups: *mut c_int) -> c_int;
|
||||||
pub fn getgrgid(gid: gid_t) -> *c_group;
|
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> {
|
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
|
// Passed the username as a string
|
||||||
} else {
|
} else {
|
||||||
let pw_pointer = unsafe {
|
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() {
|
if pw_pointer.is_not_null() {
|
||||||
Some(unsafe { read(pw_pointer) })
|
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;
|
static NGROUPS: i32 = 20;
|
||||||
|
|
||||||
pub fn group(possible_pw: Option<c_passwd>, nflag: bool) {
|
pub fn group(possible_pw: Option<c_passwd>, nflag: bool) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue