1 /* group-member.c -- determine whether group id is in calling user's group list
2 Copyright (C) 1994 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #if defined (CONFIG_BROKETS)
20 /* We use <config.h> instead of "config.h" so that a compilation
21 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
22 (which it would do because it found this file in $srcdir). */
30 #include <sys/types.h>
40 #include "group-member.h"
61 static struct group_info *
66 struct group_info *gi;
69 /* getgroups () returns the number of elements that it was able to
70 place into the array. We simply continue to call getgroups ()
71 until the number of elements placed into the array is smaller than
72 the physical size of the array. */
77 while (n_groups == n_group_slots)
80 group = (GETGROUPS_T *) xrealloc (group,
81 n_group_slots * sizeof (GETGROUPS_T));
82 n_groups = getgroups (n_group_slots, group);
85 /* In case of error, the user loses. */
92 gi = (struct group_info *) xmalloc (sizeof (*gi));
93 gi->n_groups = n_groups;
99 #endif /* not HAVE_GETGROUPS */
101 /* Return non-zero if GID is one that we have in our groups list.
102 If there is no getgroups function, return non-zero if GID matches
103 either of the current or effective group IDs. */
109 #ifndef HAVE_GETGROUPS
110 return ((gid == getgid ()) || (gid == getegid ()));
114 struct group_info *gi;
116 gi = get_group_info ();
120 /* Search through the list looking for GID. */
122 for (i = 0; i < gi->n_groups; i++)
124 if (gid == gi->group[i])
131 free_group_info (gi);
134 #endif /* HAVE_GETGROUPS */
142 main (int argc, char** argv)
146 program_name = argv[0];
148 for (i=1; i<argc; i++)
152 gid = atoi (argv[i]);
153 printf ("%d: %s\n", gid, group_member (gid) ? "yes" : "no");