1 /* idcache.c -- map user and group IDs, cached for speed
2 Copyright (C) 1985, 1988, 1989, 1990, 1997 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include <sys/types.h>
27 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
37 #ifndef _POSIX_VERSION
38 struct passwd *getpwuid ();
39 struct passwd *getpwnam ();
40 struct group *getgrgid ();
41 struct group *getgrnam ();
58 /* The members of this list have already been looked up.
59 If a name is NULL, the corresponding id is not in the password file. */
60 static struct userid *user_alist;
63 /* The members of this list are names not in the local passwd file;
64 their names are always not NULL, and their ids are irrelevant. */
65 static struct userid *nouser_alist;
68 /* Translate UID to a login name, with cache.
69 If UID cannot be resolved, return NULL.
70 Cache lookup failures, too. */
76 register struct userid *tail;
79 for (tail = user_alist; tail; tail = tail->next)
80 if (tail->id.u == uid)
83 pwent = getpwuid (uid);
84 tail = (struct userid *) xmalloc (sizeof (struct userid));
86 tail->name = (pwent ? xstrdup (pwent->pw_name) : NULL);
88 /* Add to the head of the list, so most recently added is first. */
89 tail->next = user_alist;
96 /* Translate USER to a UID, with cache.
97 Return NULL if there is no such user.
98 (We also cache which user names have no passwd entry,
99 so we don't keep looking them up.) */
105 register struct userid *tail;
106 struct passwd *pwent;
108 for (tail = user_alist; tail; tail = tail->next)
109 /* Avoid a function call for the most common case. */
110 if (tail->name && *tail->name == *user && !strcmp (tail->name, user))
113 for (tail = nouser_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 pwent = getpwnam (user);
120 tail = (struct userid *) xmalloc (sizeof (struct userid));
121 tail->name = xstrdup (user);
123 /* Add to the head of the list, so most recently added is first. */
126 tail->id.u = pwent->pw_uid;
127 tail->next = user_alist;
132 tail->next = nouser_alist;
137 #endif /* NOT_USED */
139 /* Use the same struct as for userids. */
140 static struct userid *group_alist;
142 static struct userid *nogroup_alist;
145 /* Translate GID to a group name, with cache.
146 Return NULL if the group has no name. */
152 register struct userid *tail;
155 for (tail = group_alist; tail; tail = tail->next)
156 if (tail->id.g == gid)
159 grent = getgrgid (gid);
160 tail = (struct userid *) xmalloc (sizeof (struct userid));
162 tail->name = (grent ? xstrdup (grent->gr_name) : NULL);
164 /* Add to the head of the list, so most recently used is first. */
165 tail->next = group_alist;
172 /* Translate GROUP to a GID, with cache.
173 Return NULL if there is no such group.
174 (We also cache which group names have no group entry,
175 so we don't keep looking them up.) */
181 register struct userid *tail;
184 for (tail = group_alist; tail; tail = tail->next)
185 /* Avoid a function call for the most common case. */
186 if (tail->name && *tail->name == *group && !strcmp (tail->name, group))
189 for (tail = nogroup_alist; tail; tail = tail->next)
190 /* Avoid a function call for the most common case. */
191 if (*tail->name == *group && !strcmp (tail->name, group))
194 grent = getgrnam (group);
196 tail = (struct userid *) xmalloc (sizeof (struct userid));
197 tail->name = xstrdup (group);
199 /* Add to the head of the list, so most recently used is first. */
202 tail->id.g = grent->gr_gid;
203 tail->next = group_alist;
208 tail->next = nogroup_alist;
209 nogroup_alist = tail;
213 #endif /* NOT_USED */