Change one-line summary to reflect that shred does
[coreutils.git] / lib / getugroups.c
blobdbd86ef9390caf14fdd070e7b4bf62fbb8a0e2b5
1 /* getugroups.c -- return a list of the groups a user is in
3 Copyright (C) 1990, 1991, 1998, 1999, 2000, 2003, 2004 Free
4 Software Foundation.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Written by David MacKenzie. */
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <sys/types.h>
27 #include <stdio.h> /* grp.h on alpha OSF1 V2.0 uses "FILE *". */
28 #include <grp.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #include <errno.h>
35 #ifndef EOVERFLOW
36 # define EOVERFLOW EINVAL
37 #endif
39 /* setgrent, getgrent, and endgrent are not specified by POSIX.1,
40 so header files might not declare them.
41 If you don't have them at all, we can't implement this function.
42 You lose! */
43 struct group *getgrent ();
45 #include <string.h>
47 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
49 /* Like `getgroups', but for user USERNAME instead of for the current
50 process. Store at most MAXCOUNT group IDs in the GROUPLIST array.
51 If GID is not -1, store it first (if possible). GID should be the
52 group ID (pw_gid) obtained from getpwuid, in case USERNAME is not
53 listed in /etc/groups.
54 Always return the number of groups of which USERNAME is a member. */
56 int
57 getugroups (int maxcount, GETGROUPS_T *grouplist, char *username, gid_t gid)
59 struct group *grp;
60 register char **cp;
61 register int count = 0;
63 if (gid != (gid_t) -1)
65 if (maxcount != 0)
66 grouplist[count] = gid;
67 ++count;
70 setgrent ();
71 while ((grp = getgrent ()) != 0)
73 for (cp = grp->gr_mem; *cp; ++cp)
75 int n;
77 if ( ! STREQ (username, *cp))
78 continue;
80 /* See if this group number is already on the list. */
81 for (n = 0; n < count; ++n)
82 if (grouplist && grouplist[n] == grp->gr_gid)
83 break;
85 /* If it's a new group number, then try to add it to the list. */
86 if (n == count)
88 if (maxcount != 0)
90 if (count >= maxcount)
92 endgrent ();
93 return count;
95 grouplist[count] = grp->gr_gid;
97 count++;
98 if (count < 0)
100 errno = EOVERFLOW;
101 return -1;
106 endgrent ();
108 return count;