.
[coreutils.git] / lib / getugroups.c
blob98e752609bfb93a4a7d5d6f36acd983e9bbdbd41
1 /* getugroups.c -- return a list of the groups a user is in
2 Copyright (C) 1990, 1991 Free Software Foundation.
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.
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.
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie. */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <sys/types.h>
25 #include <stdio.h> /* grp.h on alpha OSF1 V2.0 uses "FILE *". */
26 #include <grp.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
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 ();
38 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
39 #include <string.h>
40 #else
41 #include <strings.h>
42 #endif
44 /* Like `getgroups', but for user USERNAME instead of for
45 the current process. */
47 int
48 getugroups (maxcount, grouplist, username)
49 int maxcount;
50 GETGROUPS_T *grouplist;
51 char *username;
53 struct group *grp;
54 register char **cp;
55 register int count = 0;
57 setgrent ();
58 while ((grp = getgrent ()) != 0)
59 for (cp = grp->gr_mem; *cp; ++cp)
60 if (!strcmp (username, *cp))
62 if (maxcount != 0)
64 if (count >= maxcount)
66 endgrent ();
67 return count;
69 grouplist[count] = grp->gr_gid;
71 count++;
73 endgrent ();
74 return count;