Use #if, not #ifdef with HAVE_ macros
[gnulib.git] / lib / group-member.c
1 /* group-member.c -- determine whether group id is in calling user's group list
2    Copyright (C) 1994, 1997, 1998 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdio.h>
23 #include <sys/types.h>
24
25 #ifdef STDC_HEADERS
26 # include <stdlib.h>
27 #endif
28
29 #if HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32
33 #include "group-member.h"
34
35 char *xmalloc ();
36 char *xrealloc ();
37
38 struct group_info
39   {
40     int n_groups;
41     GETGROUPS_T *group;
42   };
43
44 #if HAVE_GETGROUPS
45
46 static void
47 free_group_info (struct group_info *g)
48 {
49   free (g->group);
50   free (g);
51 }
52
53 static struct group_info *
54 get_group_info (void)
55 {
56   int n_groups;
57   int n_group_slots;
58   struct group_info *gi;
59   GETGROUPS_T *group;
60
61   /* getgroups () returns the number of elements that it was able to
62      place into the array.  We simply continue to call getgroups ()
63      until the number of elements placed into the array is smaller than
64      the physical size of the array. */
65
66   group = NULL;
67   n_groups = 0;
68   n_group_slots = 0;
69   while (n_groups == n_group_slots)
70     {
71       n_group_slots += 64;
72       group = (GETGROUPS_T *) xrealloc (group,
73                                         n_group_slots * sizeof (GETGROUPS_T));
74       n_groups = getgroups (n_group_slots, group);
75     }
76
77   /* In case of error, the user loses. */
78   if (n_groups < 0)
79     {
80       free (group);
81       return NULL;
82     }
83
84   gi = (struct group_info *) xmalloc (sizeof (*gi));
85   gi->n_groups = n_groups;
86   gi->group = group;
87
88   return gi;
89 }
90
91 #endif /* not HAVE_GETGROUPS */
92
93 /* Return non-zero if GID is one that we have in our groups list.
94    If there is no getgroups function, return non-zero if GID matches
95    either of the current or effective group IDs.  */
96
97 int
98 group_member (gid_t gid)
99 {
100 #ifndef HAVE_GETGROUPS
101   return ((gid == getgid ()) || (gid == getegid ()));
102 #else
103   int i;
104   int found;
105   struct group_info *gi;
106
107   gi = get_group_info ();
108   if (gi == NULL)
109     return 0;
110
111   /* Search through the list looking for GID. */
112   found = 0;
113   for (i = 0; i < gi->n_groups; i++)
114     {
115       if (gid == gi->group[i])
116         {
117           found = 1;
118           break;
119         }
120     }
121
122   free_group_info (gi);
123
124   return found;
125 #endif /* HAVE_GETGROUPS */
126 }
127
128 #ifdef TEST
129
130 char *program_name;
131
132 int
133 main (int argc, char** argv)
134 {
135   int i;
136
137   program_name = argv[0];
138
139   for (i=1; i<argc; i++)
140     {
141       gid_t gid;
142
143       gid = atoi (argv[i]);
144       printf ("%d: %s\n", gid, group_member (gid) ? "yes" : "no");
145     }
146   exit (0);
147 }
148
149 #endif /* TEST */