Mercurial > hg > octave-jordi > gnulib-hg
annotate lib/getugroups.c @ 18070:d460ec17f09f
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Tue, 28 Jul 2015 13:57:32 -0700 (2015-07-28) |
parents | d86e349ad92d |
children |
rev | line source |
---|---|
9 | 1 /* getugroups.c -- return a list of the groups a user is in |
5159 | 2 |
17848 | 3 Copyright (C) 1990-1991, 1998-2000, 2003-2015 Free Software Foundation, Inc. |
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 |
17906
d86e349ad92d
getugroups: Fix Android build
Kevin Cernekee <cernekee@google.com>
parents:
17848
diff
changeset
|
30 #if !HAVE_GRP_H || defined __ANDROID__ |
12279
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 |
17906
d86e349ad92d
getugroups: Fix Android build
Kevin Cernekee <cernekee@google.com>
parents:
17848
diff
changeset
|
33 can do is fail with ENOSYS. |
d86e349ad92d
getugroups: Fix Android build
Kevin Cernekee <cernekee@google.com>
parents:
17848
diff
changeset
|
34 |
d86e349ad92d
getugroups: Fix Android build
Kevin Cernekee <cernekee@google.com>
parents:
17848
diff
changeset
|
35 Bionic declares e.g. getgrent() in <grp.h> but it isn't actually |
d86e349ad92d
getugroups: Fix Android build
Kevin Cernekee <cernekee@google.com>
parents:
17848
diff
changeset
|
36 defined in the library. */ |
5159 | 37 |
12279
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
38 int |
12482
11bc92a9e2bc
gnulib-common: prefer _GL_UNUSED over _UNUSED_PARAMETER_
Eric Blake <ebb9@byu.net>
parents:
12421
diff
changeset
|
39 getugroups (int maxcount _GL_UNUSED, |
11bc92a9e2bc
gnulib-common: prefer _GL_UNUSED over _UNUSED_PARAMETER_
Eric Blake <ebb9@byu.net>
parents:
12421
diff
changeset
|
40 gid_t *grouplist _GL_UNUSED, |
11bc92a9e2bc
gnulib-common: prefer _GL_UNUSED over _UNUSED_PARAMETER_
Eric Blake <ebb9@byu.net>
parents:
12421
diff
changeset
|
41 char const *username _GL_UNUSED, |
11bc92a9e2bc
gnulib-common: prefer _GL_UNUSED over _UNUSED_PARAMETER_
Eric Blake <ebb9@byu.net>
parents:
12421
diff
changeset
|
42 gid_t gid _GL_UNUSED) |
12279
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
43 { |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
44 errno = ENOSYS; |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
45 return -1; |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
46 } |
9 | 47 |
12279
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
48 #else /* HAVE_GRP_H */ |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
49 # include <grp.h> |
9 | 50 |
12772
2ea3e3fab7ee
define STREQ(a,b) consistently, removing useless parentheses
Jim Meyering <meyering@redhat.com>
parents:
12559
diff
changeset
|
51 # define STREQ(a, b) (strcmp (a, b) == 0) |
1843
d9e10c329845
(getugroups): Don't dereference a null pointer when
Jim Meyering <jim@meyering.net>
parents:
1733
diff
changeset
|
52 |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
53 /* Like 'getgroups', but for user USERNAME instead of for the current |
1733
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
54 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
|
55 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
|
56 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
|
57 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
|
58 Otherwise, return the number of IDs we've written into GROUPLIST. */ |
9 | 59 |
60 int | |
12280
39be377f1765
getgroups: don't expose GETGROUPS_T to user
Eric Blake <ebb9@byu.net>
parents:
12279
diff
changeset
|
61 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
|
62 gid_t gid) |
9 | 63 { |
9043
c69012fcd861
* lib/getugroups.h: New file.
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
64 int count = 0; |
9 | 65 |
2571
a3aef28bde56
(getugroups): Cast -1 to gid_t, for systems like
Jim Meyering <jim@meyering.net>
parents:
1843
diff
changeset
|
66 if (gid != (gid_t) -1) |
1733
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
67 { |
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
68 if (maxcount != 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
69 grouplist[count] = gid; |
1733
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
70 ++count; |
6b0ff21d3888
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1731
diff
changeset
|
71 } |
1730
05413a4755ee
(getugroups): Take new parameter, gid.
Jim Meyering <jim@meyering.net>
parents:
1263
diff
changeset
|
72 |
9 | 73 setgrent (); |
9049
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
74 while (1) |
1843
d9e10c329845
(getugroups): Don't dereference a null pointer when
Jim Meyering <jim@meyering.net>
parents:
1733
diff
changeset
|
75 { |
9043
c69012fcd861
* lib/getugroups.h: New file.
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
76 char **cp; |
9049
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
77 struct group *grp; |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
78 |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
79 errno = 0; |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
80 grp = getgrent (); |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
81 if (grp == NULL) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
82 break; |
9049
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
83 |
1843
d9e10c329845
(getugroups): Don't dereference a null pointer when
Jim Meyering <jim@meyering.net>
parents:
1733
diff
changeset
|
84 for (cp = grp->gr_mem; *cp; ++cp) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
85 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
86 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
|
87 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
88 if ( ! STREQ (username, *cp)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
89 continue; |
1843
d9e10c329845
(getugroups): Don't dereference a null pointer when
Jim Meyering <jim@meyering.net>
parents:
1733
diff
changeset
|
90 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
91 /* 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
|
92 for (n = 0; n < count; ++n) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
93 if (grouplist && grouplist[n] == grp->gr_gid) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
94 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
|
95 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
96 /* 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
|
97 if (n == count) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
98 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
99 if (maxcount != 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
100 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
101 if (count >= maxcount) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
102 goto done; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
103 grouplist[count] = grp->gr_gid; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
104 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
105 if (count == INT_MAX) |
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 errno = EOVERFLOW; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
108 goto done; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
109 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
110 count++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
111 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12280
diff
changeset
|
112 } |
1843
d9e10c329845
(getugroups): Don't dereference a null pointer when
Jim Meyering <jim@meyering.net>
parents:
1733
diff
changeset
|
113 } |
9044
20fba8637b7d
Close the group DB even when failing with 2^31 or more members.
Jim Meyering <jim@meyering.net>
parents:
9043
diff
changeset
|
114 |
9049
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
115 if (errno != 0) |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
116 count = -1; |
da9a878d7b51
* lib/getugroups.c (getugroups): Detect getgrent failure.
Jim Meyering <jim@meyering.net>
parents:
9045
diff
changeset
|
117 |
9044
20fba8637b7d
Close the group DB even when failing with 2^31 or more members.
Jim Meyering <jim@meyering.net>
parents:
9043
diff
changeset
|
118 done: |
9045
11f2466d2f5b
Don't let endgrent clobber errno, no matter how improbable.
Jim Meyering <jim@meyering.net>
parents:
9044
diff
changeset
|
119 { |
11f2466d2f5b
Don't let endgrent clobber errno, no matter how improbable.
Jim Meyering <jim@meyering.net>
parents:
9044
diff
changeset
|
120 int saved_errno = errno; |
11f2466d2f5b
Don't let endgrent clobber errno, no matter how improbable.
Jim Meyering <jim@meyering.net>
parents:
9044
diff
changeset
|
121 endgrent (); |
11f2466d2f5b
Don't let endgrent clobber errno, no matter how improbable.
Jim Meyering <jim@meyering.net>
parents:
9044
diff
changeset
|
122 errno = saved_errno; |
11f2466d2f5b
Don't let endgrent clobber errno, no matter how improbable.
Jim Meyering <jim@meyering.net>
parents:
9044
diff
changeset
|
123 } |
1843
d9e10c329845
(getugroups): Don't dereference a null pointer when
Jim Meyering <jim@meyering.net>
parents:
1733
diff
changeset
|
124 |
9 | 125 return count; |
126 } | |
12279
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
127 |
6a41b8f5f874
getgroups, getugroups: provide stubs for mingw
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
128 #endif /* HAVE_GRP_H */ |