Skip to content

Instantly share code, notes, and snippets.

@jgarzik
Created April 17, 2024 01:43
Show Gist options
  • Save jgarzik/70bc2e379e27a0a0619945b8c555e3c6 to your computer and use it in GitHub Desktop.
Save jgarzik/70bc2e379e27a0a0619945b8c555e3c6 to your computer and use it in GitHub Desktop.
enumerage groups, Take Two
fn enumerate_groups() {
let mut groupent: *mut libc::group;
unsafe {
setgrent();
groupent = getgrent();
}
while groupent != ptr::null_mut() {
let c_str: &CStr = unsafe { CStr::from_ptr((*groupent).gr_name) };
println!("{}", c_str.to_str().unwrap());
unsafe {
if (*groupent).gr_mem != ptr::null_mut() {
let member_ptr_arr: *mut *mut c_char = (*groupent).gr_mem;
if member_ptr_arr == ptr::null_mut() {
continue;
}
for i in 0.. {
let member_ptr: *const c_char = *(member_ptr_arr.offset(i));
if member_ptr != ptr::null() {
let member: &CStr = CStr::from_ptr(member_ptr);
println!(" {}", member.to_str().unwrap());
} else {
break;
}
}
}
}
unsafe {
groupent = getgrent();
}
}
unsafe {
endgrent();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment