Mercurial > hg > octave-lojdl > gnulib-hg
annotate lib/idcache.c @ 17249:e542fd46ad6f
maint: update all copyright year number ranges
Run "make update-copyright". Compare to commit 1602f0a from last year.
Signed-off-by: Eric Blake <eblake@redhat.com>
author | Eric Blake <eblake@redhat.com> |
---|---|
date | Tue, 01 Jan 2013 00:50:58 +0000 |
parents | 8250f2777afc |
children |
rev | line source |
---|---|
5 | 1 /* idcache.c -- map user and group IDs, cached for speed |
5907 | 2 |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
3 Copyright (C) 1985, 1988-1990, 1997-1998, 2003, 2005-2007, 2009-2013 Free |
12518
b5e42ef33b49
update nearly all FSF copyright year lists to include 2009
Jim Meyering <meyering@redhat.com>
parents:
12421
diff
changeset
|
4 Software Foundation, Inc. |
5 | 5 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9148
diff
changeset
|
6 This program is free software: you can redistribute it and/or modify |
5 | 7 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:
9148
diff
changeset
|
8 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:
9148
diff
changeset
|
9 (at your option) any later version. |
5 | 10 |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 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:
9148
diff
changeset
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
5
diff
changeset
|
18 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
19 #include <config.h> |
5 | 20 |
9148 | 21 #include "idcache.h" |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
22 #include <stddef.h> |
5 | 23 #include <stdio.h> |
4662 | 24 #include <string.h> |
5 | 25 #include <pwd.h> |
26 #include <grp.h> | |
27 | |
6275 | 28 #include <unistd.h> |
967
029c8dd12d60
(getuser): Return NULL (rather than stringified uid) upon lookup failure.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
29 |
4544 | 30 #include "xalloc.h" |
31 | |
1261
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
32 #ifdef __DJGPP__ |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
33 static char digits[] = "0123456789"; |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
34 #endif |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
35 |
5 | 36 struct userid |
37 { | |
38 union | |
39 { | |
40 uid_t u; | |
41 gid_t g; | |
42 } id; | |
43 struct userid *next; | |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
44 char name[FLEXIBLE_ARRAY_MEMBER]; |
5 | 45 }; |
46 | |
9148 | 47 /* FIXME: provide a function to free any malloc'd storage and reset lists, |
48 so that an application can use code like this just before exiting: | |
49 #ifdef lint | |
50 idcache_clear (); | |
51 #endif | |
52 */ | |
53 | |
5 | 54 static struct userid *user_alist; |
55 | |
7675
476857ef4611
* lib/idcache.c: Adjust comments in user- and group- portions to
Jim Meyering <jim@meyering.net>
parents:
7673
diff
changeset
|
56 /* Each entry on list is a user name for which the first lookup failed. */ |
5 | 57 static struct userid *nouser_alist; |
58 | |
9148 | 59 /* Use the same struct as for userids. */ |
60 static struct userid *group_alist; | |
61 | |
62 /* Each entry on list is a group name for which the first lookup failed. */ | |
63 static struct userid *nogroup_alist; | |
64 | |
1253 | 65 /* Translate UID to a login name, with cache, or NULL if unresolved. */ |
5 | 66 |
67 char * | |
1253 | 68 getuser (uid_t uid) |
5 | 69 { |
7672
d2c7e0fe0b94
* lib/idcache.c (getuser): Remove all uses of the register keyword.
Jim Meyering <jim@meyering.net>
parents:
7671
diff
changeset
|
70 struct userid *tail; |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
71 struct userid *match = NULL; |
5 | 72 |
73 for (tail = user_alist; tail; tail = tail->next) | |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
74 { |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
75 if (tail->id.u == uid) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
76 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
77 match = tail; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
78 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
79 } |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
80 } |
5 | 81 |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
82 if (match == NULL) |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
83 { |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
84 struct passwd *pwent = getpwuid (uid); |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
85 char const *name = pwent ? pwent->pw_name : ""; |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
86 match = xmalloc (offsetof (struct userid, name) + strlen (name) + 1); |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
87 match->id.u = uid; |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
88 strcpy (match->name, name); |
5 | 89 |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
90 /* Add to the head of the list, so most recently used is first. */ |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
91 match->next = user_alist; |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
92 user_alist = match; |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
93 } |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
94 |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
95 return match->name[0] ? match->name : NULL; |
5 | 96 } |
97 | |
98 /* Translate USER to a UID, with cache. | |
99 Return NULL if there is no such user. | |
100 (We also cache which user names have no passwd entry, | |
101 so we don't keep looking them up.) */ | |
102 | |
103 uid_t * | |
1253 | 104 getuidbyname (const char *user) |
5 | 105 { |
7672
d2c7e0fe0b94
* lib/idcache.c (getuser): Remove all uses of the register keyword.
Jim Meyering <jim@meyering.net>
parents:
7671
diff
changeset
|
106 struct userid *tail; |
5 | 107 struct passwd *pwent; |
108 | |
109 for (tail = user_alist; tail; tail = tail->next) | |
110 /* Avoid a function call for the most common case. */ | |
1253 | 111 if (*tail->name == *user && !strcmp (tail->name, user)) |
5 | 112 return &tail->id.u; |
113 | |
114 for (tail = nouser_alist; tail; tail = tail->next) | |
115 /* Avoid a function call for the most common case. */ | |
116 if (*tail->name == *user && !strcmp (tail->name, user)) | |
7671
0e4c9d56db95
Use cleaner syntax: NULL rather than 0.
Jim Meyering <jim@meyering.net>
parents:
7670
diff
changeset
|
117 return NULL; |
5 | 118 |
119 pwent = getpwnam (user); | |
1261
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
120 #ifdef __DJGPP__ |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
121 /* We need to pretend to be the user USER, to make |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
122 pwd functions know about an arbitrary user name. */ |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
123 if (!pwent && strspn (user, digits) < strlen (user)) |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
124 { |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
125 setenv ("USER", user, 1); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
126 pwent = getpwnam (user); /* now it will succeed */ |
1261
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
127 } |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
128 #endif |
5 | 129 |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
130 tail = xmalloc (offsetof (struct userid, name) + strlen (user) + 1); |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
131 strcpy (tail->name, user); |
5 | 132 |
1253 | 133 /* Add to the head of the list, so most recently used is first. */ |
5 | 134 if (pwent) |
135 { | |
136 tail->id.u = pwent->pw_uid; | |
137 tail->next = user_alist; | |
138 user_alist = tail; | |
139 return &tail->id.u; | |
140 } | |
141 | |
142 tail->next = nouser_alist; | |
143 nouser_alist = tail; | |
7671
0e4c9d56db95
Use cleaner syntax: NULL rather than 0.
Jim Meyering <jim@meyering.net>
parents:
7670
diff
changeset
|
144 return NULL; |
5 | 145 } |
146 | |
1253 | 147 /* Translate GID to a group name, with cache, or NULL if unresolved. */ |
5 | 148 |
149 char * | |
1253 | 150 getgroup (gid_t gid) |
5 | 151 { |
7672
d2c7e0fe0b94
* lib/idcache.c (getuser): Remove all uses of the register keyword.
Jim Meyering <jim@meyering.net>
parents:
7671
diff
changeset
|
152 struct userid *tail; |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
153 struct userid *match = NULL; |
5 | 154 |
155 for (tail = group_alist; tail; tail = tail->next) | |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
156 { |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
157 if (tail->id.g == gid) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
158 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
159 match = tail; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
160 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
161 } |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
162 } |
5 | 163 |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
164 if (match == NULL) |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
165 { |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
166 struct group *grent = getgrgid (gid); |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
167 char const *name = grent ? grent->gr_name : ""; |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
168 match = xmalloc (offsetof (struct userid, name) + strlen (name) + 1); |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
169 match->id.g = gid; |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
170 strcpy (match->name, name); |
5 | 171 |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
172 /* Add to the head of the list, so most recently used is first. */ |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
173 match->next = group_alist; |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
174 group_alist = match; |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
175 } |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
176 |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
177 return match->name[0] ? match->name : NULL; |
5 | 178 } |
179 | |
967
029c8dd12d60
(getuser): Return NULL (rather than stringified uid) upon lookup failure.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
180 /* Translate GROUP to a GID, with cache. |
5 | 181 Return NULL if there is no such group. |
182 (We also cache which group names have no group entry, | |
183 so we don't keep looking them up.) */ | |
184 | |
185 gid_t * | |
1253 | 186 getgidbyname (const char *group) |
5 | 187 { |
7672
d2c7e0fe0b94
* lib/idcache.c (getuser): Remove all uses of the register keyword.
Jim Meyering <jim@meyering.net>
parents:
7671
diff
changeset
|
188 struct userid *tail; |
5 | 189 struct group *grent; |
190 | |
191 for (tail = group_alist; tail; tail = tail->next) | |
192 /* Avoid a function call for the most common case. */ | |
1253 | 193 if (*tail->name == *group && !strcmp (tail->name, group)) |
5 | 194 return &tail->id.g; |
195 | |
196 for (tail = nogroup_alist; tail; tail = tail->next) | |
197 /* Avoid a function call for the most common case. */ | |
198 if (*tail->name == *group && !strcmp (tail->name, group)) | |
7671
0e4c9d56db95
Use cleaner syntax: NULL rather than 0.
Jim Meyering <jim@meyering.net>
parents:
7670
diff
changeset
|
199 return NULL; |
5 | 200 |
201 grent = getgrnam (group); | |
1261
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
202 #ifdef __DJGPP__ |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
203 /* We need to pretend to belong to group GROUP, to make |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
204 grp functions know about an arbitrary group name. */ |
1261
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
205 if (!grent && strspn (group, digits) < strlen (group)) |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
206 { |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
207 setenv ("GROUP", group, 1); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
208 grent = getgrnam (group); /* now it will succeed */ |
1261
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
209 } |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
210 #endif |
5 | 211 |
7673
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
212 tail = xmalloc (offsetof (struct userid, name) + strlen (group) + 1); |
a37b8b182d49
* lib/idcache.c: Restore most of the 2006-11-06 patch, so as to
Jim Meyering <jim@meyering.net>
parents:
7672
diff
changeset
|
213 strcpy (tail->name, group); |
5 | 214 |
215 /* Add to the head of the list, so most recently used is first. */ | |
216 if (grent) | |
217 { | |
218 tail->id.g = grent->gr_gid; | |
219 tail->next = group_alist; | |
220 group_alist = tail; | |
221 return &tail->id.g; | |
222 } | |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
5
diff
changeset
|
223 |
5 | 224 tail->next = nogroup_alist; |
225 nogroup_alist = tail; | |
7671
0e4c9d56db95
Use cleaner syntax: NULL rather than 0.
Jim Meyering <jim@meyering.net>
parents:
7670
diff
changeset
|
226 return NULL; |
5 | 227 } |