diff --git a/utshell-0.5/r_general/src/lib.rs b/utshell-0.5/r_general/src/lib.rs index a28bc85465b9dfbda04812da32da5357eea4bb34..85e3c546d037c6c059366f3e69d1338b5dc37ef4 100644 --- a/utshell-0.5/r_general/src/lib.rs +++ b/utshell-0.5/r_general/src/lib.rs @@ -1938,4 +1938,35 @@ unsafe extern "C" fn initialize_group_array() { *group_array.offset(0 as libc::c_int as isize) = current_user.gid; } } -} \ No newline at end of file +} + +/* Return non-zero if GID is one that we have in our groups list. */ +#[no_mangle] +pub unsafe extern "C" fn group_member(mut gid: gid_t) -> libc::c_int { + let mut i: libc::c_int = 0; + + /* Short-circuit if possible, maybe saving a call to getgroups(). */ + if gid == current_user.gid || gid == current_user.egid { + return 1 as libc::c_int; + } + if ngroups == 0 as libc::c_int { + initialize_group_array(); + } + + /* In case of error, the user loses. */ + if ngroups <= 0 as libc::c_int { + return 0 as libc::c_int; + } + + /* Search through the list looking for GID. */ + i = 0 as libc::c_int; + while i < ngroups { + if gid == *group_array.offset(i as isize) { + return 1 as libc::c_int; + } + i += 1; + i; + } + + return 0 as libc::c_int; +}