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. */
23 #include <sys/types.h>
33 #include "group-member.h"
54 static struct group_info
*
59 struct group_info
*gi
;
62 /* getgroups () returns the number of elements that it was able to
63 place into the array. We simply continue to call getgroups ()
64 until the number of elements placed into the array is smaller than
65 the physical size of the array. */
70 while (n_groups
== n_group_slots
)
73 group
= (GETGROUPS_T
*) xrealloc (group
,
74 n_group_slots
* sizeof (GETGROUPS_T
));
75 n_groups
= getgroups (n_group_slots
, group
);
78 /* In case of error, the user loses. */
85 gi
= (struct group_info
*) xmalloc (sizeof (*gi
));
86 gi
->n_groups
= n_groups
;
92 #endif /* not HAVE_GETGROUPS */
94 /* Return non-zero if GID is one that we have in our groups list.
95 If there is no getgroups function, return non-zero if GID matches
96 either of the current or effective group IDs. */
102 #ifndef HAVE_GETGROUPS
103 return ((gid
== getgid ()) || (gid
== getegid ()));
107 struct group_info
*gi
;
109 gi
= get_group_info ();
113 /* Search through the list looking for GID. */
115 for (i
= 0; i
< gi
->n_groups
; i
++)
117 if (gid
== gi
->group
[i
])
124 free_group_info (gi
);
127 #endif /* HAVE_GETGROUPS */
135 main (int argc
, char** argv
)
139 program_name
= argv
[0];
141 for (i
=1; i
<argc
; i
++)
145 gid
= atoi (argv
[i
]);
146 printf ("%d: %s\n", gid
, group_member (gid
) ? "yes" : "no");