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 static struct userid *user_alist;
61 /* The members of this list have names not in the local passwd file. */
62 static struct userid *nouser_alist;
65 /* Translate UID to a login name, with cache.
66 If UID cannot be resolved, return NULL.
67 Cache lookup failures, too. */
73 register struct userid *tail;
76 for (tail = user_alist; tail; tail = tail->next)
77 if (tail->id.u == uid)
80 pwent = getpwuid (uid);
81 tail = (struct userid *) xmalloc (sizeof (struct userid));
83 tail->name = (pwent ? xstrdup (pwent->pw_name) : NULL);
85 /* Add to the head of the list, so most recently used is first. */
86 tail->next = user_alist;
93 /* Translate USER to a UID, with cache.
94 Return NULL if there is no such user.
95 (We also cache which user names have no passwd entry,
96 so we don't keep looking them up.) */
102 register struct userid *tail;
103 struct passwd *pwent;
105 for (tail = user_alist; tail; tail = tail->next)
106 /* Avoid a function call for the most common case. */
107 if (*tail->name == *user && !strcmp (tail->name, user))
110 for (tail = nouser_alist; tail; tail = tail->next)
111 /* Avoid a function call for the most common case. */
112 if (*tail->name == *user && !strcmp (tail->name, user))
115 pwent = getpwnam (user);
117 tail = (struct userid *) xmalloc (sizeof (struct userid));
118 tail->name = xstrdup (user);
120 /* Add to the head of the list, so most recently used is first. */
123 tail->id.u = pwent->pw_uid;
124 tail->next = user_alist;
129 tail->next = nouser_alist;
134 #endif /* NOT_USED */
136 /* Use the same struct as for userids. */
137 static struct userid *group_alist;
139 static struct userid *nogroup_alist;
142 /* Translate GID to a group name or a stringified number,
149 register struct userid *tail;
152 for (tail = group_alist; tail; tail = tail->next)
153 if (tail->id.g == gid)
156 grent = getgrgid (gid);
157 tail = (struct userid *) xmalloc (sizeof (struct userid));
159 tail->name = (grent ? xstrdup (grent->gr_name) : NULL);
161 /* Add to the head of the list, so most recently used is first. */
162 tail->next = group_alist;
169 /* Translate GROUP to a GID, with cache.
170 Return NULL if there is no such group.
171 (We also cache which group names have no group entry,
172 so we don't keep looking them up.) */
178 register struct userid *tail;
181 for (tail = group_alist; tail; tail = tail->next)
182 /* Avoid a function call for the most common case. */
183 if (*tail->name == *group && !strcmp (tail->name, group))
186 for (tail = nogroup_alist; tail; tail = tail->next)
187 /* Avoid a function call for the most common case. */
188 if (*tail->name == *group && !strcmp (tail->name, group))
191 grent = getgrnam (group);
193 tail = (struct userid *) xmalloc (sizeof (struct userid));
194 tail->name = xstrdup (group);
196 /* Add to the head of the list, so most recently used is first. */
199 tail->id.g = grent->gr_gid;
200 tail->next = group_alist;
205 tail->next = nogroup_alist;
206 nogroup_alist = tail;
210 #endif /* NOT_USED */