Imported Upstream version 4.5.10
[platform/upstream/findutils.git] / tests / getugroups.c
1 /* getugroups.c -- return a list of the groups a user is in
2
3    Copyright (C) 1990-1991, 1998-2000, 2003-2011 Free Software Foundation, Inc.
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 3 of the License, or
8    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
17
18 /* Written by David MacKenzie. */
19
20 #include <config.h>
21
22 #include "getugroups.h"
23
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdio.h> /* grp.h on alpha OSF1 V2.0 uses "FILE *". */
27 #include <string.h>
28 #include <unistd.h>
29
30 #if !HAVE_GRP_H
31
32 /* Mingw lacks all things related to group management.  The best we
33    can do is fail with ENOSYS.  */
34
35 int
36 getugroups (int maxcount _GL_UNUSED,
37             gid_t *grouplist _GL_UNUSED,
38             char const *username _GL_UNUSED,
39             gid_t gid _GL_UNUSED)
40 {
41   errno = ENOSYS;
42   return -1;
43 }
44
45 #else /* HAVE_GRP_H */
46 # include <grp.h>
47
48 # define STREQ(a, b) (strcmp (a, b) == 0)
49
50 /* Like `getgroups', but for user USERNAME instead of for the current
51    process.  Store at most MAXCOUNT group IDs in the GROUPLIST array.
52    If GID is not -1, store it first (if possible).  GID should be the
53    group ID (pw_gid) obtained from getpwuid, in case USERNAME is not
54    listed in /etc/groups.  Upon failure, set errno and return -1.
55    Otherwise, return the number of IDs we've written into GROUPLIST.  */
56
57 int
58 getugroups (int maxcount, gid_t *grouplist, char const *username,
59             gid_t gid)
60 {
61   int count = 0;
62
63   if (gid != (gid_t) -1)
64     {
65       if (maxcount != 0)
66         grouplist[count] = gid;
67       ++count;
68     }
69
70   setgrent ();
71   while (1)
72     {
73       char **cp;
74       struct group *grp;
75
76       errno = 0;
77       grp = getgrent ();
78       if (grp == NULL)
79         break;
80
81       for (cp = grp->gr_mem; *cp; ++cp)
82         {
83           int n;
84
85           if ( ! STREQ (username, *cp))
86             continue;
87
88           /* See if this group number is already on the list.  */
89           for (n = 0; n < count; ++n)
90             if (grouplist && grouplist[n] == grp->gr_gid)
91               break;
92
93           /* If it's a new group number, then try to add it to the list.  */
94           if (n == count)
95             {
96               if (maxcount != 0)
97                 {
98                   if (count >= maxcount)
99                     goto done;
100                   grouplist[count] = grp->gr_gid;
101                 }
102               if (count == INT_MAX)
103                 {
104                   errno = EOVERFLOW;
105                   goto done;
106                 }
107               count++;
108             }
109         }
110     }
111
112   if (errno != 0)
113     count = -1;
114
115  done:
116   {
117     int saved_errno = errno;
118     endgrent ();
119     errno = saved_errno;
120   }
121
122   return count;
123 }
124
125 #endif /* HAVE_GRP_H */