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 #include <sys/types.h>
23 #if defined(USG) || defined(STDC_HEADERS)
32 #ifndef _POSIX_VERSION
33 struct passwd *getpwuid ();
34 struct passwd *getpwnam ();
35 struct group *getgrgid ();
36 struct group *getgrnam ();
53 static struct userid *user_alist;
55 /* The members of this list have names not in the local passwd file. */
56 static struct userid *nouser_alist;
58 /* Translate UID to a login name or a stringified number,
65 register struct userid *tail;
67 char usernum_string[20];
69 for (tail = user_alist; tail; tail = tail->next)
70 if (tail->id.u == uid)
73 pwent = getpwuid (uid);
74 tail = (struct userid *) xmalloc (sizeof (struct userid));
78 sprintf (usernum_string, "%u", (unsigned) uid);
79 tail->name = xstrdup (usernum_string);
82 tail->name = xstrdup (pwent->pw_name);
84 /* Add to the head of the list, so most recently used is first. */
85 tail->next = user_alist;
90 /* Translate USER to a UID, with cache.
91 Return NULL if there is no such user.
92 (We also cache which user names have no passwd entry,
93 so we don't keep looking them up.) */
99 register struct userid *tail;
100 struct passwd *pwent;
102 for (tail = user_alist; tail; tail = tail->next)
103 /* Avoid a function call for the most common case. */
104 if (*tail->name == *user && !strcmp (tail->name, user))
107 for (tail = nouser_alist; tail; tail = tail->next)
108 /* Avoid a function call for the most common case. */
109 if (*tail->name == *user && !strcmp (tail->name, user))
112 pwent = getpwnam (user);
114 tail = (struct userid *) xmalloc (sizeof (struct userid));
115 tail->name = xstrdup (user);
117 /* Add to the head of the list, so most recently used is first. */
120 tail->id.u = pwent->pw_uid;
121 tail->next = user_alist;
126 tail->next = nouser_alist;
131 /* Use the same struct as for userids. */
132 static struct userid *group_alist;
133 static struct userid *nogroup_alist;
135 /* Translate GID to a group name or a stringified number,
142 register struct userid *tail;
144 char groupnum_string[20];
146 for (tail = group_alist; tail; tail = tail->next)
147 if (tail->id.g == gid)
150 grent = getgrgid (gid);
151 tail = (struct userid *) xmalloc (sizeof (struct userid));
155 sprintf (groupnum_string, "%u", (unsigned int) gid);
156 tail->name = xstrdup (groupnum_string);
159 tail->name = xstrdup (grent->gr_name);
161 /* Add to the head of the list, so most recently used is first. */
162 tail->next = group_alist;
167 /* Translate GROUP to a UID, with cache.
168 Return NULL if there is no such group.
169 (We also cache which group names have no group entry,
170 so we don't keep looking them up.) */
176 register struct userid *tail;
179 for (tail = group_alist; tail; tail = tail->next)
180 /* Avoid a function call for the most common case. */
181 if (*tail->name == *group && !strcmp (tail->name, group))
184 for (tail = nogroup_alist; tail; tail = tail->next)
185 /* Avoid a function call for the most common case. */
186 if (*tail->name == *group && !strcmp (tail->name, group))
189 grent = getgrnam (group);
191 tail = (struct userid *) xmalloc (sizeof (struct userid));
192 tail->name = xstrdup (group);
194 /* Add to the head of the list, so most recently used is first. */
197 tail->id.g = grent->gr_gid;
198 tail->next = group_alist;
203 tail->next = nogroup_alist;
204 nogroup_alist = tail;