1 /* getugroups.c -- return a list of the groups a user is in
2 Copyright (C) 1990, 1991, 1998 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David MacKenzie. */
24 #include <sys/types.h>
25 #include <stdio.h> /* grp.h on alpha OSF1 V2.0 uses "FILE *". */
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.
36 struct group
*getgrent ();
38 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
44 /* Like `getgroups', but for user USERNAME instead of for
45 the current process. */
48 getugroups (int maxcount
, GETGROUPS_T
*grouplist
, char *username
)
52 register int count
= 0;
55 while ((grp
= getgrent ()) != 0)
56 for (cp
= grp
->gr_mem
; *cp
; ++cp
)
57 if (!strcmp (username
, *cp
))
61 /* See if this group number is already on the list. */
62 for (n
= 0; n
< count
; ++n
)
63 if (grouplist
[n
] == grp
->gr_gid
)
66 /* If it's a new group number, then try to add it to the list. */
71 if (count
>= maxcount
)
76 grouplist
[count
] = grp
->gr_gid
;