1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

Merge pull request #268 from Arcterus/id-group-mac-fix

Fix id and groups for Macs.  Also add dependency information.
This commit is contained in:
Oly Mi 2014-06-19 11:23:44 +04:00
commit b44f47d7f7
3 changed files with 47 additions and 18 deletions

View file

@ -97,9 +97,10 @@ command = sh -c '$(1)'
# Main exe build rule
define EXE_BUILD
-include build/$(1).d
ifeq ($(wildcard $(1)/Makefile),)
build/$(1): $(1)/$(1).rs | build
$(call command,$(RUSTC) $(RUSTCFLAGS) -o build/$(1) $(1)/$(1).rs)
$(call command,$(RUSTC) $(RUSTCFLAGS) --dep-info build/$(1).d -o build/$(1) $(1)/$(1).rs)
clean_$(1):
else
build/$(1): $(1)/$(1).rs | build
@ -110,8 +111,9 @@ endif
endef
define CRATE_BUILD
-include build/$(1).d
build/$(2): $(1)/$(1).rs | build
$(call command,$(RUSTC) $(RUSTCFLAGS) --crate-type rlib $(1)/$(1).rs --out-dir build)
$(call command,$(RUSTC) $(RUSTCFLAGS) --crate-type rlib --dep-info build/$(1).d $(1)/$(1).rs --out-dir build)
endef
# Test exe built rules
@ -129,8 +131,10 @@ all: $(EXES_PATHS)
else
all: build/uutils
-include build/uutils.d
build/uutils: uutils/uutils.rs $(addprefix build/, $(foreach crate,$(CRATES),$(shell $(RUSTC) --crate-type rlib --crate-file-name $(crate)/$(crate).rs)))
$(RUSTC) $(RUSTCFLAGS) -L build/ uutils/uutils.rs -o $@
$(RUSTC) $(RUSTCFLAGS) -L build/ --dep-info $@.d uutils/uutils.rs -o $@
endif
test: tmp $(addprefix test_,$(TESTS))

View file

@ -5,6 +5,7 @@ extern crate libc;
use self::libc::{
c_char,
c_int,
int32_t,
uid_t,
gid_t,
};
@ -83,12 +84,17 @@ pub struct c_tm {
extern {
pub fn getpwuid(uid: uid_t) -> *c_passwd;
pub fn getpwnam(login: *c_char) -> *c_passwd;
pub fn getgrouplist(name: *c_char,
basegid: gid_t,
groups: *mut 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 getgrouplist(name: *c_char,
gid: gid_t,
groups: *mut gid_t,
ngroups: *mut c_int) -> c_int;
}
#[cfg(target_os = "macos")]
extern {
pub fn getgroupcount(name: *c_char, gid: gid_t) -> int32_t;
}
pub fn get_pw_from_args(free: &Vec<String>) -> Option<c_passwd> {
@ -137,17 +143,37 @@ pub fn get_group(groupname: &str) -> Option<c_group> {
}
}
pub fn get_group_list(name: *c_char, gid: gid_t) -> Result<Vec<gid_t>, int> {
let mut ngroups = 0 as c_int;
pub fn get_group_list(name: *c_char, gid: gid_t) -> Vec<gid_t> {
let mut ngroups: c_int = 32;
let mut groups: Vec<gid_t> = Vec::with_capacity(ngroups as uint);
unsafe { getgrouplist(name, gid, 0 as *mut gid_t, &mut ngroups) };
let mut groups = Vec::from_elem(ngroups as uint, 0 as gid_t);
let err = unsafe { getgrouplist(name, gid, groups.as_mut_ptr(), &mut ngroups) };
if err == -1 {
Err(os::errno())
if unsafe { get_group_list_internal(name, gid, groups.as_mut_ptr(), &mut ngroups) } == -1 {
groups.reserve(ngroups as uint);
unsafe { get_group_list_internal(name, gid, groups.as_mut_ptr(), &mut ngroups); }
} else {
groups.truncate(ngroups as uint);
Ok(groups)
}
unsafe { groups.set_len(ngroups as uint); }
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")]
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 oldsize = *grcnt;
*grcnt = ngroups;
if oldsize >= ngroups {
getgrouplist(name, gid, groups, grcnt);
0
} else {
-1
}
}
@ -170,7 +196,7 @@ pub fn get_groups() -> Result<Vec<gid_t>, int> {
pub fn group(possible_pw: Option<c_passwd>, nflag: bool) {
let groups = match possible_pw {
Some(pw) => get_group_list(pw.pw_name, pw.pw_gid),
Some(pw) => Ok(get_group_list(pw.pw_name, pw.pw_gid)),
None => get_groups(),
};

View file

@ -21,7 +21,6 @@ extern crate libc;
use std::os;
use std::ptr::read;
use libc::{
c_int,
uid_t,
getgid,
getuid
@ -342,7 +341,7 @@ fn id_print(possible_pw: Option<c_passwd>,
}
let groups = match possible_pw {
Some(pw) => get_group_list(pw.pw_name, pw.pw_gid),
Some(pw) => Ok(get_group_list(pw.pw_name, pw.pw_gid)),
None => get_groups(),
};