1 /* getugroups.c -- return a list of the groups a user is in
3 Copyright (C) 1990, 1991, 1998-2000, 2003-2007 Free Software Foundation.
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.
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.
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/>. */
18 /* Written by David MacKenzie. */
22 #include "getugroups.h"
25 #include <stdio.h> /* grp.h on alpha OSF1 V2.0 uses "FILE *". */
32 # define EOVERFLOW EINVAL
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.
38 struct group *getgrent ();
42 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
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. Upon failure, set errno and return -1.
49 Otherwise, return the number of IDs we've written into GROUPLIST. */
52 getugroups (int maxcount, GETGROUPS_T *grouplist, char const *username,
57 if (gid != (gid_t) -1)
60 grouplist[count] = gid;
75 for (cp = grp->gr_mem; *cp; ++cp)
79 if ( ! STREQ (username, *cp))
82 /* See if this group number is already on the list. */
83 for (n = 0; n < count; ++n)
84 if (grouplist && grouplist[n] == grp->gr_gid)
87 /* If it's a new group number, then try to add it to the list. */
92 if (count >= maxcount)
94 grouplist[count] = grp->gr_gid;
111 int saved_errno = errno;