Remove K&R cruft.
[gnulib.git] / lib / getugroups.c
1 /* getugroups.c -- return a list of the groups a user is in
2    Copyright (C) 1990, 1991, 1998, 1999, 2000, 2003 Free Software Foundation.
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 /* Written by David MacKenzie. */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <sys/types.h>
25 #include <stdio.h> /* grp.h on alpha OSF1 V2.0 uses "FILE *". */
26 #include <grp.h>
27
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31
32 /* setgrent, getgrent, and endgrent are not specified by POSIX.1,
33    so header files might not declare them.
34    If you don't have them at all, we can't implement this function.
35    You lose!  */
36 struct group *getgrent ();
37
38 #include <string.h>
39
40 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
41
42 /* Like `getgroups', but for user USERNAME instead of for the current
43    process.  Store at most MAXCOUNT group IDs in the GROUPLIST array.
44    If GID is not -1, store it first (if possible).  GID should be the
45    group ID (pw_gid) obtained from getpwuid, in case USERNAME is not
46    listed in /etc/groups.
47    Always return the number of groups of which USERNAME is a member.  */
48
49 int
50 getugroups (int maxcount, GETGROUPS_T *grouplist, char *username, gid_t gid)
51 {
52   struct group *grp;
53   register char **cp;
54   register int count = 0;
55
56   if (gid != (gid_t) -1)
57     {
58       if (maxcount != 0)
59         grouplist[count] = gid;
60       ++count;
61     }
62
63   setgrent ();
64   while ((grp = getgrent ()) != 0)
65     {
66       for (cp = grp->gr_mem; *cp; ++cp)
67         {
68           int n;
69
70           if ( ! STREQ (username, *cp))
71             continue;
72
73           /* See if this group number is already on the list.  */
74           for (n = 0; n < count; ++n)
75             if (grouplist && grouplist[n] == grp->gr_gid)
76               break;
77
78           /* If it's a new group number, then try to add it to the list.  */
79           if (n == count)
80             {
81               if (maxcount != 0)
82                 {
83                   if (count >= maxcount)
84                     {
85                       endgrent ();
86                       return count;
87                     }
88                   grouplist[count] = grp->gr_gid;
89                 }
90               count++;
91             }
92         }
93     }
94   endgrent ();
95
96   return count;
97 }