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)
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 #include <sys/types.h>
27 /* Even though SunOS 4, Ultrix 4, and 386BSD are mostly POSIX.1 compliant,
28 their getgroups system call (except in the `System V' environment, which
29 is troublesome in other ways) fills in an array of int, not gid_t
30 (which is `short' on those systems). We do the same, for consistency.
34 #if !defined(sun) && !defined(ultrix) && !defined(__386BSD__)
35 #define GETGROUPS_T gid_t
36 #else /* sun or ultrix or 386BSD */
37 #define GETGROUPS_T int
38 #endif /* sun or ultrix or 386BSD */
39 #else /* not _POSIX_VERSION */
40 #define GETGROUPS_T int
41 #endif /* not _POSIX_VERSION */
43 /* setgrent, getgrent, and endgrent are not specified by POSIX.1,
44 so header files might not declare them.
45 If you don't have them at all, we can't implement this function.
47 struct group
*getgrent ();
49 #if defined(USG) || defined(STDC_HEADERS)
55 /* Like `getgroups', but for user USERNAME instead of for
56 the current process. */
59 getugroups (maxcount
, grouplist
, username
)
61 GETGROUPS_T
*grouplist
;
66 register int count
= 0;
69 while ((grp
= getgrent ()) != 0)
70 for (cp
= grp
->gr_mem
; *cp
; ++cp
)
71 if (!strcmp (username
, *cp
))
75 if (count
>= maxcount
)
80 grouplist
[count
] = grp
->gr_gid
;