* cycle-check.h (CYCLE_CHECK_REFLECT_CHDIR_UP): Abort if this
[gnulib.git] / lib / getugroups.c
1 /* getugroups.c -- return a list of the groups a user is in
2
3    Copyright (C) 1990, 1991, 1998, 1999, 2000, 2003, 2004, 2005 Free
4    Software Foundation.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Written by David MacKenzie. */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <sys/types.h>
27 #include <stdio.h> /* grp.h on alpha OSF1 V2.0 uses "FILE *". */
28 #include <grp.h>
29
30 #include <unistd.h>
31
32 #include <errno.h>
33 #ifndef EOVERFLOW
34 # define EOVERFLOW EINVAL
35 #endif
36
37 /* Some old header files might not declare setgrent, getgrent, and endgrent.
38    If you don't have them at all, we can't implement this function.
39    You lose!  */
40 struct group *getgrent ();
41
42 #include <string.h>
43
44 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
45
46 /* Like `getgroups', but for user USERNAME instead of for the current
47    process.  Store at most MAXCOUNT group IDs in the GROUPLIST array.
48    If GID is not -1, store it first (if possible).  GID should be the
49    group ID (pw_gid) obtained from getpwuid, in case USERNAME is not
50    listed in /etc/groups.
51    Always return the number of groups of which USERNAME is a member.  */
52
53 int
54 getugroups (int maxcount, GETGROUPS_T *grouplist, char *username, gid_t gid)
55 {
56   struct group *grp;
57   register char **cp;
58   register int count = 0;
59
60   if (gid != (gid_t) -1)
61     {
62       if (maxcount != 0)
63         grouplist[count] = gid;
64       ++count;
65     }
66
67   setgrent ();
68   while ((grp = getgrent ()) != 0)
69     {
70       for (cp = grp->gr_mem; *cp; ++cp)
71         {
72           int n;
73
74           if ( ! STREQ (username, *cp))
75             continue;
76
77           /* See if this group number is already on the list.  */
78           for (n = 0; n < count; ++n)
79             if (grouplist && grouplist[n] == grp->gr_gid)
80               break;
81
82           /* If it's a new group number, then try to add it to the list.  */
83           if (n == count)
84             {
85               if (maxcount != 0)
86                 {
87                   if (count >= maxcount)
88                     {
89                       endgrent ();
90                       return count;
91                     }
92                   grouplist[count] = grp->gr_gid;
93                 }
94               count++;
95               if (count < 0)
96                 {
97                   errno = EOVERFLOW;
98                   return -1;
99                 }
100             }
101         }
102     }
103   endgrent ();
104
105   return count;
106 }