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

id: rework id_print()

Fixes #239
This commit is contained in:
Michael Gehring 2014-06-17 14:45:10 +02:00
parent c80fdc1a73
commit 2b2bdc89ff

View file

@ -34,6 +34,8 @@ use getopts::{getopts, optflag, usage};
use c_types::{ use c_types::{
c_passwd, c_passwd,
c_group, c_group,
get_groups,
get_group_list,
get_pw_from_args, get_pw_from_args,
getpwuid, getpwuid,
group group
@ -80,10 +82,6 @@ mod audit {
extern { extern {
fn getgrgid(gid: uid_t) -> *c_group; fn getgrgid(gid: uid_t) -> *c_group;
fn getgrouplist(name: *c_char,
basegid: gid_t,
groups: *gid_t,
ngroups: *mut c_int) -> c_int;
} }
static NAME: &'static str = "id"; static NAME: &'static str = "id";
@ -347,19 +345,14 @@ fn id_print(possible_pw: Option<c_passwd>,
gid = unsafe { getgid() }; gid = unsafe { getgid() };
} }
let mut ngroups; let groups = match possible_pw {
let mut groups = Vec::with_capacity(NGROUPS as uint); Some(pw) => get_group_list(pw.pw_name, pw.pw_gid),
None => get_groups(),
};
if possible_pw.is_some() { let groups = groups.unwrap_or_else(|errno| {
ngroups = NGROUPS; crash!(1, "failed to get group list (errno={:d})", errno);
let pw_name = possible_pw.unwrap().pw_name; });
unsafe { getgrouplist(pw_name, gid, groups.as_ptr(), &mut ngroups) };
} else {
ngroups = unsafe {
getgroups(NGROUPS, groups.as_mut_ptr() as *mut u32)
};
}
if possible_pw.is_some() { if possible_pw.is_some() {
print!( print!(
@ -400,16 +393,14 @@ fn id_print(possible_pw: Option<c_passwd>,
} }
} }
unsafe { groups.set_len(ngroups as uint) }; if groups.len() > 0 {
if ngroups > 0 {
print!(" groups="); print!(" groups=");
let mut first = true; let mut first = true;
for &gr in groups.iter() { for &gr in groups.iter() {
if !first { print!(",") } if !first { print!(",") }
print!("{:u}", gr); print!("{:u}", gr);
let group = unsafe { getgrgid(gr as u32) }; let group = unsafe { getgrgid(gr) };
if group.is_not_null() { if group.is_not_null() {
let name = unsafe { let name = unsafe {
from_c_str(read(group).gr_name) from_c_str(read(group).gr_name)
@ -418,7 +409,7 @@ fn id_print(possible_pw: Option<c_passwd>,
} }
first = false first = false
} }
println!("");
} }
println!("");
} }