From 9372260a7e15c2cabc601cfe1ddcd65044f27f40 Mon Sep 17 00:00:00 2001 From: mengfansheng Date: Thu, 13 Jun 2024 09:44:11 +0800 Subject: [PATCH] add group_member --- utshell-0.5/r_general/src/lib.rs | 33 +++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/utshell-0.5/r_general/src/lib.rs b/utshell-0.5/r_general/src/lib.rs index a28bc854..85e3c546 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; +} -- Gitee