mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
common: make get_group_list work for all users on Macs
This commit is contained in:
parent
b26b874829
commit
ce3b45b503
1 changed files with 14 additions and 16 deletions
|
@ -82,14 +82,14 @@ pub struct c_tm {
|
||||||
}
|
}
|
||||||
|
|
||||||
extern {
|
extern {
|
||||||
pub fn setgrent();
|
|
||||||
pub fn endgrent();
|
|
||||||
|
|
||||||
pub fn getpwuid(uid: uid_t) -> *c_passwd;
|
pub fn getpwuid(uid: uid_t) -> *c_passwd;
|
||||||
pub fn getpwnam(login: *c_char) -> *c_passwd;
|
pub fn getpwnam(login: *c_char) -> *c_passwd;
|
||||||
pub fn getgrent() -> *c_group;
|
|
||||||
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 getgrnam(name: *c_char) -> *c_group;
|
||||||
|
pub fn getgrouplist(name: *c_char,
|
||||||
|
gid: gid_t,
|
||||||
|
groups: *mut gid_t,
|
||||||
|
ngroups: *mut c_int) -> c_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
|
@ -97,14 +97,6 @@ extern {
|
||||||
pub fn getgroupcount(name: *c_char, gid: gid_t) -> int32_t;
|
pub fn getgroupcount(name: *c_char, gid: gid_t) -> int32_t;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
|
||||||
extern {
|
|
||||||
pub fn getgrouplist(name: *c_char,
|
|
||||||
gid: gid_t,
|
|
||||||
groups: *mut gid_t,
|
|
||||||
ngroups: *mut c_int) -> c_int;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_pw_from_args(free: &Vec<String>) -> Option<c_passwd> {
|
pub fn get_pw_from_args(free: &Vec<String>) -> Option<c_passwd> {
|
||||||
if free.len() == 1 {
|
if free.len() == 1 {
|
||||||
let username = free.get(0).as_slice();
|
let username = free.get(0).as_slice();
|
||||||
|
@ -155,9 +147,9 @@ pub fn get_group_list(name: *c_char, gid: gid_t) -> Vec<gid_t> {
|
||||||
let mut ngroups: c_int = 32;
|
let mut ngroups: c_int = 32;
|
||||||
let mut groups: Vec<gid_t> = Vec::with_capacity(ngroups as uint);
|
let mut groups: Vec<gid_t> = Vec::with_capacity(ngroups as uint);
|
||||||
|
|
||||||
if unsafe { getgrouplist(name, gid, groups.as_mut_ptr(), &mut ngroups) } == -1 {
|
if unsafe { get_group_list_internal(name, gid, groups.as_mut_ptr(), &mut ngroups) } == -1 {
|
||||||
groups.reserve(ngroups as uint);
|
groups.reserve(ngroups as uint);
|
||||||
unsafe { getgrouplist(name, gid, groups.as_mut_ptr(), &mut ngroups); }
|
unsafe { get_group_list_internal(name, gid, groups.as_mut_ptr(), &mut ngroups); }
|
||||||
} else {
|
} else {
|
||||||
groups.truncate(ngroups as uint);
|
groups.truncate(ngroups as uint);
|
||||||
}
|
}
|
||||||
|
@ -166,13 +158,19 @@ pub fn get_group_list(name: *c_char, gid: gid_t) -> Vec<gid_t> {
|
||||||
groups
|
groups
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
#[inline(always)]
|
||||||
|
unsafe fn get_group_list_internal(name: *c_char, gid: gid_t, groups: *mut gid_t, grcnt: *mut c_int) -> c_int {
|
||||||
|
getgrouplist(name, gid, groups, grcnt);
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
unsafe fn getgrouplist(name: *c_char, gid: gid_t, groups: *mut gid_t, grcnt: *mut c_int) -> c_int {
|
unsafe fn get_group_list_internal(name: *c_char, gid: gid_t, groups: *mut gid_t, grcnt: *mut c_int) -> c_int {
|
||||||
let ngroups = getgroupcount(name, gid);
|
let ngroups = getgroupcount(name, gid);
|
||||||
let oldsize = *grcnt;
|
let oldsize = *grcnt;
|
||||||
*grcnt = ngroups;
|
*grcnt = ngroups;
|
||||||
if oldsize >= ngroups {
|
if oldsize >= ngroups {
|
||||||
getgroups(ngroups, groups);
|
getgrouplist(name, gid, groups, grcnt);
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
-1
|
-1
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue