Add tests with filenames containing newline and backslash characters.
[coreutils.git] / lib / getugroups.c
blobafa425b6e53c43c5ad3946dc9f5ff9801648152d
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)
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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 (int maxcount, GETGROUPS_T *grouplist, char *username)
50 struct group *grp;
51 register char **cp;
52 register int count = 0;
54 setgrent ();
55 while ((grp = getgrent ()) != 0)
56 for (cp = grp->gr_mem; *cp; ++cp)
57 if (!strcmp (username, *cp))
59 int n;
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)
64 break;
66 /* If it's a new group number, then try to add it to the list. */
67 if (n == count)
69 if (maxcount != 0)
71 if (count >= maxcount)
73 endgrent ();
74 return count;
76 grouplist[count] = grp->gr_gid;
78 count++;
81 endgrent ();
82 return count;