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