Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/getugroups.c @ 12421:e8d2c6fc33ad
Use spaces for indentation, not tabs.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 10 Dec 2009 20:28:30 +0100 |
parents | 39be377f1765 |
children | 11bc92a9e2bc |
rev | line source |
---|---|
9 | 1 /* getugroups.c -- return a list of the groups a user is in |
5159 | 2 |
11709
61fffcc71917
remove superfluous parentheses in STREQ definition
Jim Meyering <meyering@redhat.com>
parents:
11110
diff
changeset
|
3 Copyright (C) 1990, 1991, 1998-2000, 2003-2009 Free Software Foundation. |
9 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9049
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
9 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9049
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9049
diff
changeset
|
8 (at your option) any later version. |
9 | 9 |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9049
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
9 | 17 |
18 /* Written by David MacKenzie. */ | |
19 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6793
diff
changeset
|
20 #include <config.h> |
578
3cea4259ae09
Include stdio.h before grp.h.
Jim Meyering <jim@meyering.net>
parents:
9
diff
changeset
|
21 |
9043
c69012fcd861
* lib/getugroups.h: New file.
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
22 #include "getugroups.h" |
c69012fcd861
* lib/getugroups.h: New file.
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
23 |
12279
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
24 #include <errno.h> |
9703
e29f811d31c0
Don't rely on signed integer overflowing to negative value.
Lasse Collin <lasse.collin@tukaani.org>
parents:
9309
diff
changeset
|
25 #include <limits.h> |
578
3cea4259ae09
Include stdio.h before grp.h.
Jim Meyering <jim@meyering.net>
parents:
9
diff
changeset
|
26 #include <stdio.h> /* grp.h on alpha OSF1 V2.0 uses "FILE *". */ |
12279
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
27 #include <string.h> |
6275 | 28 #include <unistd.h> |
9 | 29 |
12279
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
30 #if !HAVE_GRP_H |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
31 |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
32 /* Mingw lacks all things related to group management. The best we |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
33 can do is fail with ENOSYS. */ |
5159 | 34 |
12279
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
35 int |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
36 getugroups (int maxcount _UNUSED_PARAMETER_, |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
37 gid_t *grouplist _UNUSED_PARAMETER_, |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
38 char const *username _UNUSED_PARAMETER_, |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
39 gid_t gid _UNUSED_PARAMETER_) |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
40 { |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
41 errno = ENOSYS; |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
42 return -1; |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
43 } |
9 | 44 |
12279
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
45 #else /* HAVE_GRP_H */ |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
46 # include <grp.h> |
9 | 47 |
12279
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
48 # define STREQ(s1, s2) (strcmp (s1, s2) == 0) |
1843
d9e10c329845
(getugroups): Don't dereference a null pointer when
Jim Meyering <jim@meyering.net>
parents:
1733
diff
changeset
|
49 |
1733
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
50 /* Like `getgroups', but for user USERNAME instead of for the current |
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
51 process. Store at most MAXCOUNT group IDs in the GROUPLIST array. |
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
52 If GID is not -1, store it first (if possible). GID should be the |
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
53 group ID (pw_gid) obtained from getpwuid, in case USERNAME is not |
9049
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
54 listed in /etc/groups. Upon failure, set errno and return -1. |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
55 Otherwise, return the number of IDs we've written into GROUPLIST. */ |
9 | 56 |
57 int | |
12280
39be377f1765
getgroups: don't expose GETGROUPS_T to user
Eric Blake <ebb9@byu.net>
parents:
12279
diff
changeset
|
58 getugroups (int maxcount, gid_t *grouplist, char const *username, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
59 gid_t gid) |
9 | 60 { |
9043
c69012fcd861
* lib/getugroups.h: New file.
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
61 int count = 0; |
9 | 62 |
2571
a3aef28bde56
(getugroups): Cast -1 to gid_t, for systems like
Jim Meyering <jim@meyering.net>
parents:
1843
diff
changeset
|
63 if (gid != (gid_t) -1) |
1733
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
64 { |
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
65 if (maxcount != 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
66 grouplist[count] = gid; |
1733
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
67 ++count; |
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
68 } |
1730
05413a4755ee
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1263
diff
changeset
|
69 |
9 | 70 setgrent (); |
9049
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
71 while (1) |
1843
d9e10c329845
(getugroups): Don't dereference a null pointer when
Jim Meyering <jim@meyering.net>
parents:
1733
diff
changeset
|
72 { |
9043
c69012fcd861
* lib/getugroups.h: New file.
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
73 char **cp; |
9049
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
74 struct group *grp; |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
75 |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
76 errno = 0; |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
77 grp = getgrent (); |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
78 if (grp == NULL) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
79 break; |
9049
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
80 |
1843
d9e10c329845
(getugroups): Don't dereference a null pointer when
Jim Meyering <jim@meyering.net>
parents:
1733
diff
changeset
|
81 for (cp = grp->gr_mem; *cp; ++cp) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
82 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
83 int n; |
1262
662622cf616f
(getugroups): Don't add a group number if it would be a duplicate.
Jim Meyering <jim@meyering.net>
parents:
653
diff
changeset
|
84 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
85 if ( ! STREQ (username, *cp)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
86 continue; |
1843
d9e10c329845
(getugroups): Don't dereference a null pointer when
Jim Meyering <jim@meyering.net>
parents:
1733
diff
changeset
|
87 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
88 /* See if this group number is already on the list. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
89 for (n = 0; n < count; ++n) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
90 if (grouplist && grouplist[n] == grp->gr_gid) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
91 break; |
1262
662622cf616f
(getugroups): Don't add a group number if it would be a duplicate.
Jim Meyering <jim@meyering.net>
parents:
653
diff
changeset
|
92 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
93 /* If it's a new group number, then try to add it to the list. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
94 if (n == count) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
95 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
96 if (maxcount != 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
97 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
98 if (count >= maxcount) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
99 goto done; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
100 grouplist[count] = grp->gr_gid; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
101 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
102 if (count == INT_MAX) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
103 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
104 errno = EOVERFLOW; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
105 goto done; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
106 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
107 count++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
108 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
109 } |
1843
d9e10c329845
(getugroups): Don't dereference a null pointer when
Jim Meyering <jim@meyering.net>
parents:
1733
diff
changeset
|
110 } |
9044
20fba8637b7d
Close the group DB even when failing with 2^31 or more members.
Jim Meyering <jim@meyering.net>
parents:
9043
diff
changeset
|
111 |
9049
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
112 if (errno != 0) |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
113 count = -1; |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
114 |
9044
20fba8637b7d
Close the group DB even when failing with 2^31 or more members.
Jim Meyering <jim@meyering.net>
parents:
9043
diff
changeset
|
115 done: |
9045
11f2466d2f5b
Don't let endgrent clobber errno, no matter how improbable.
Jim Meyering <jim@meyering.net>
parents:
9044
diff
changeset
|
116 { |
11f2466d2f5b
Don't let endgrent clobber errno, no matter how improbable.
Jim Meyering <jim@meyering.net>
parents:
9044
diff
changeset
|
117 int saved_errno = errno; |
11f2466d2f5b
Don't let endgrent clobber errno, no matter how improbable.
Jim Meyering <jim@meyering.net>
parents:
9044
diff
changeset
|
118 endgrent (); |
11f2466d2f5b
Don't let endgrent clobber errno, no matter how improbable.
Jim Meyering <jim@meyering.net>
parents:
9044
diff
changeset
|
119 errno = saved_errno; |
11f2466d2f5b
Don't let endgrent clobber errno, no matter how improbable.
Jim Meyering <jim@meyering.net>
parents:
9044
diff
changeset
|
120 } |
1843
d9e10c329845
(getugroups): Don't dereference a null pointer when
Jim Meyering <jim@meyering.net>
parents:
1733
diff
changeset
|
121 |
9 | 122 return count; |
123 } | |
12279
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
124 |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
125 #endif /* HAVE_GRP_H */ |