1 /* idcache.c -- map user and group IDs, cached for speed
2 Copyright (C) 1985, 1988, 1989, 1990 Free Software Foundation, Inc.
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)
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #if defined (CONFIG_BROKETS)
20 /* We use <config.h> instead of "config.h" so that a compilation
21 using -I. -I will use ./config.h rather than /config.h
22 (which it would do because it found this file in ). */
30 #include <sys/types.h>
34 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
43 #ifndef _POSIX_VERSION
44 struct passwd *getpwuid ();
45 struct passwd *getpwnam ();
46 struct group *getgrgid ();
47 struct group *getgrnam ();
64 static struct userid *user_alist;
66 /* The members of this list have names not in the local passwd file. */
67 static struct userid *nouser_alist;
69 /* Translate UID to a login name or a stringified number,
76 register struct userid *tail;
78 char usernum_string[20];
80 for (tail = user_alist; tail; tail = tail->next)
81 if (tail->id.u == uid)
84 pwent = getpwuid (uid);
85 tail = (struct userid *) xmalloc (sizeof (struct userid));
89 sprintf (usernum_string, "%u", (unsigned) uid);
90 tail->name = xstrdup (usernum_string);
93 tail->name = xstrdup (pwent->pw_name);
95 /* Add to the head of the list, so most recently used is first. */
96 tail->next = user_alist;
101 /* Translate USER to a UID, with cache.
102 Return NULL if there is no such user.
103 (We also cache which user names have no passwd entry,
104 so we don't keep looking them up.) */
110 register struct userid *tail;
111 struct passwd *pwent;
113 for (tail = user_alist; tail; tail = tail->next)
114 /* Avoid a function call for the most common case. */
115 if (*tail->name == *user && !strcmp (tail->name, user))
118 for (tail = nouser_alist; tail; tail = tail->next)
119 /* Avoid a function call for the most common case. */
120 if (*tail->name == *user && !strcmp (tail->name, user))
123 pwent = getpwnam (user);
125 tail = (struct userid *) xmalloc (sizeof (struct userid));
126 tail->name = xstrdup (user);
128 /* Add to the head of the list, so most recently used is first. */
131 tail->id.u = pwent->pw_uid;
132 tail->next = user_alist;
137 tail->next = nouser_alist;
142 /* Use the same struct as for userids. */
143 static struct userid *group_alist;
144 static struct userid *nogroup_alist;
146 /* Translate GID to a group name or a stringified number,
153 register struct userid *tail;
155 char groupnum_string[20];
157 for (tail = group_alist; tail; tail = tail->next)
158 if (tail->id.g == gid)
161 grent = getgrgid (gid);
162 tail = (struct userid *) xmalloc (sizeof (struct userid));
166 sprintf (groupnum_string, "%u", (unsigned int) gid);
167 tail->name = xstrdup (groupnum_string);
170 tail->name = xstrdup (grent->gr_name);
172 /* Add to the head of the list, so most recently used is first. */
173 tail->next = group_alist;
178 /* Translate GROUP to a UID, with cache.
179 Return NULL if there is no such group.
180 (We also cache which group names have no group entry,
181 so we don't keep looking them up.) */
187 register struct userid *tail;
190 for (tail = group_alist; tail; tail = tail->next)
191 /* Avoid a function call for the most common case. */
192 if (*tail->name == *group && !strcmp (tail->name, group))
195 for (tail = nogroup_alist; tail; tail = tail->next)
196 /* Avoid a function call for the most common case. */
197 if (*tail->name == *group && !strcmp (tail->name, group))
200 grent = getgrnam (group);
202 tail = (struct userid *) xmalloc (sizeof (struct userid));
203 tail->name = xstrdup (group);
205 /* Add to the head of the list, so most recently used is first. */
208 tail->id.g = grent->gr_gid;
209 tail->next = group_alist;
214 tail->next = nogroup_alist;
215 nogroup_alist = tail;