exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / idcache.c
blobccaf56dad2ffb69f6a0a9f5ec65a2fdaaeeb42c0
1 /* idcache.c -- map user and group IDs, cached for speed
3 Copyright (C) 1985, 1988-1990, 1997-1998, 2003, 2005-2007, 2009-2024 Free
4 Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 #include <config.h>
21 #include "idcache.h"
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <pwd.h>
26 #include <grp.h>
28 #include <unistd.h>
30 #include "flexmember.h"
31 #include "xalloc.h"
33 #ifdef __DJGPP__
34 static char digits[] = "0123456789";
35 #endif
37 struct userid
39 union
41 uid_t u;
42 gid_t g;
43 } id;
44 struct userid *next;
45 char name[FLEXIBLE_ARRAY_MEMBER];
48 /* FIXME: provide a function to free any malloc'd storage and reset lists,
49 so that an application can use code like this just before exiting:
50 #if defined GCC_LINT || defined lint
51 idcache_clear ();
52 #endif
55 static struct userid *user_alist;
57 /* Each entry on list is a user name for which the first lookup failed. */
58 static struct userid *nouser_alist;
60 /* Use the same struct as for userids. */
61 static struct userid *group_alist;
63 /* Each entry on list is a group name for which the first lookup failed. */
64 static struct userid *nogroup_alist;
66 /* Translate UID to a login name, with cache, or NULL if unresolved. */
68 char *
69 getuser (uid_t uid)
71 struct userid *tail;
72 struct userid *match = NULL;
74 for (tail = user_alist; tail; tail = tail->next)
76 if (tail->id.u == uid)
78 match = tail;
79 break;
83 if (match == NULL)
85 struct passwd *pwent = getpwuid (uid);
86 char const *name = pwent ? pwent->pw_name : "";
87 match = xmalloc (FLEXSIZEOF (struct userid, name, strlen (name) + 1));
88 match->id.u = uid;
89 strcpy (match->name, name);
91 /* Add to the head of the list, so most recently used is first. */
92 match->next = user_alist;
93 user_alist = match;
96 return match->name[0] ? match->name : NULL;
99 /* Translate USER to a UID, with cache.
100 Return NULL if there is no such user.
101 (We also cache which user names have no passwd entry,
102 so we don't keep looking them up.) */
104 uid_t *
105 getuidbyname (const char *user)
107 struct userid *tail;
108 struct passwd *pwent;
110 for (tail = user_alist; tail; tail = tail->next)
111 /* Avoid a function call for the most common case. */
112 if (*tail->name == *user && !strcmp (tail->name, user))
113 return &tail->id.u;
115 for (tail = nouser_alist; tail; tail = tail->next)
116 /* Avoid a function call for the most common case. */
117 if (*tail->name == *user && !strcmp (tail->name, user))
118 return NULL;
120 pwent = getpwnam (user);
121 #ifdef __DJGPP__
122 /* We need to pretend to be the user USER, to make
123 pwd functions know about an arbitrary user name. */
124 if (!pwent && strspn (user, digits) < strlen (user))
126 setenv ("USER", user, 1);
127 pwent = getpwnam (user); /* now it will succeed */
129 #endif
131 tail = xmalloc (FLEXSIZEOF (struct userid, name, strlen (user) + 1));
132 strcpy (tail->name, user);
134 /* Add to the head of the list, so most recently used is first. */
135 if (pwent)
137 tail->id.u = pwent->pw_uid;
138 tail->next = user_alist;
139 user_alist = tail;
140 return &tail->id.u;
143 tail->next = nouser_alist;
144 nouser_alist = tail;
145 return NULL;
148 /* Translate GID to a group name, with cache, or NULL if unresolved. */
150 char *
151 getgroup (gid_t gid)
153 struct userid *tail;
154 struct userid *match = NULL;
156 for (tail = group_alist; tail; tail = tail->next)
158 if (tail->id.g == gid)
160 match = tail;
161 break;
165 if (match == NULL)
167 struct group *grent = getgrgid (gid);
168 char const *name = grent ? grent->gr_name : "";
169 match = xmalloc (FLEXSIZEOF (struct userid, name, strlen (name) + 1));
170 match->id.g = gid;
171 strcpy (match->name, name);
173 /* Add to the head of the list, so most recently used is first. */
174 match->next = group_alist;
175 group_alist = match;
178 return match->name[0] ? match->name : NULL;
181 /* Translate GROUP to a GID, with cache.
182 Return NULL if there is no such group.
183 (We also cache which group names have no group entry,
184 so we don't keep looking them up.) */
186 gid_t *
187 getgidbyname (const char *group)
189 struct userid *tail;
190 struct group *grent;
192 for (tail = group_alist; tail; tail = tail->next)
193 /* Avoid a function call for the most common case. */
194 if (*tail->name == *group && !strcmp (tail->name, group))
195 return &tail->id.g;
197 for (tail = nogroup_alist; tail; tail = tail->next)
198 /* Avoid a function call for the most common case. */
199 if (*tail->name == *group && !strcmp (tail->name, group))
200 return NULL;
202 grent = getgrnam (group);
203 #ifdef __DJGPP__
204 /* We need to pretend to belong to group GROUP, to make
205 grp functions know about an arbitrary group name. */
206 if (!grent && strspn (group, digits) < strlen (group))
208 setenv ("GROUP", group, 1);
209 grent = getgrnam (group); /* now it will succeed */
211 #endif
213 tail = xmalloc (FLEXSIZEOF (struct userid, name, strlen (group) + 1));
214 strcpy (tail->name, group);
216 /* Add to the head of the list, so most recently used is first. */
217 if (grent)
219 tail->id.g = grent->gr_gid;
220 tail->next = group_alist;
221 group_alist = tail;
222 return &tail->id.g;
225 tail->next = nogroup_alist;
226 nogroup_alist = tail;
227 return NULL;