mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
Fix id and groups for Macs. Also make every util depend on common
This commit is contained in:
parent
a2d5dc4995
commit
cf2d7a3bb7
3 changed files with 45 additions and 17 deletions
6
Makefile
6
Makefile
|
@ -97,11 +97,11 @@ command = sh -c '$(1)'
|
||||||
# Main exe build rule
|
# Main exe build rule
|
||||||
define EXE_BUILD
|
define EXE_BUILD
|
||||||
ifeq ($(wildcard $(1)/Makefile),)
|
ifeq ($(wildcard $(1)/Makefile),)
|
||||||
build/$(1): $(1)/$(1).rs | build
|
build/$(1): $(1)/$(1).rs common/*.rs | build
|
||||||
$(call command,$(RUSTC) $(RUSTCFLAGS) -o build/$(1) $(1)/$(1).rs)
|
$(call command,$(RUSTC) $(RUSTCFLAGS) -o build/$(1) $(1)/$(1).rs)
|
||||||
clean_$(1):
|
clean_$(1):
|
||||||
else
|
else
|
||||||
build/$(1): $(1)/$(1).rs | build
|
build/$(1): $(1)/$(1).rs common/*.rs | build
|
||||||
cd $(1) && make
|
cd $(1) && make
|
||||||
clean_$(1):
|
clean_$(1):
|
||||||
cd $(1) && make clean
|
cd $(1) && make clean
|
||||||
|
@ -109,7 +109,7 @@ endif
|
||||||
endef
|
endef
|
||||||
|
|
||||||
define CRATE_BUILD
|
define CRATE_BUILD
|
||||||
build/$(2): $(1)/$(1).rs | build
|
build/$(2): $(1)/$(1).rs common/*.rs | build
|
||||||
$(call command,$(RUSTC) $(RUSTCFLAGS) --crate-type rlib $(1)/$(1).rs --out-dir build)
|
$(call command,$(RUSTC) $(RUSTCFLAGS) --crate-type rlib $(1)/$(1).rs --out-dir build)
|
||||||
endef
|
endef
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ extern crate libc;
|
||||||
use self::libc::{
|
use self::libc::{
|
||||||
c_char,
|
c_char,
|
||||||
c_int,
|
c_int,
|
||||||
|
int32_t,
|
||||||
uid_t,
|
uid_t,
|
||||||
gid_t,
|
gid_t,
|
||||||
};
|
};
|
||||||
|
@ -81,16 +82,29 @@ 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 getgrouplist(name: *c_char,
|
pub fn getgrent() -> *c_group;
|
||||||
basegid: gid_t,
|
|
||||||
groups: *mut gid_t,
|
|
||||||
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 getgrnam(name: *c_char) -> *c_group;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
extern {
|
||||||
|
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();
|
||||||
|
@ -137,17 +151,31 @@ 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> {
|
pub fn get_group_list(name: *c_char, gid: gid_t) -> Vec<gid_t> {
|
||||||
let mut ngroups = 0 as c_int;
|
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) };
|
if unsafe { getgrouplist(name, gid, groups.as_mut_ptr(), &mut ngroups as *mut c_int) } == -1 {
|
||||||
let mut groups = Vec::from_elem(ngroups as uint, 0 as gid_t);
|
groups.reserve(ngroups as uint);
|
||||||
let err = unsafe { getgrouplist(name, gid, groups.as_mut_ptr(), &mut ngroups) };
|
unsafe { getgrouplist(name, gid, groups.as_mut_ptr(), &mut ngroups); }
|
||||||
if err == -1 {
|
|
||||||
Err(os::errno())
|
|
||||||
} else {
|
} else {
|
||||||
groups.truncate(ngroups as uint);
|
groups.truncate(ngroups as uint);
|
||||||
Ok(groups)
|
}
|
||||||
|
unsafe { groups.set_len(ngroups as uint); }
|
||||||
|
|
||||||
|
groups
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
unsafe fn getgrouplist(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 {
|
||||||
|
getgroups(ngroups, groups);
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
-1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,7 +198,7 @@ pub fn get_groups() -> Result<Vec<gid_t>, int> {
|
||||||
pub fn group(possible_pw: Option<c_passwd>, nflag: bool) {
|
pub fn group(possible_pw: Option<c_passwd>, nflag: bool) {
|
||||||
|
|
||||||
let groups = match possible_pw {
|
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(),
|
None => get_groups(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
2
id/id.rs
2
id/id.rs
|
@ -342,7 +342,7 @@ fn id_print(possible_pw: Option<c_passwd>,
|
||||||
}
|
}
|
||||||
|
|
||||||
let groups = match possible_pw {
|
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(),
|
None => get_groups(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue