1 /* idcache.c -- map user and group IDs, cached for speed
2 Copyright (C) 1985, 1988, 1989, 1990, 2004 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
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301 USA. */
24 #include <sys/types.h>
28 #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;
60 /* The members of this list have names not in the local passwd file. */
61 static struct userid *nouser_alist;
63 /* Translate UID to a login name or a stringified number,
69 register struct userid *tail;
71 char usernum_string[20];
73 for (tail = user_alist; tail; tail = tail->next)
74 if (tail->id.u == uid)
77 pwent = getpwuid (uid);
78 tail = (struct userid *) xmalloc (sizeof (struct userid));
82 sprintf (usernum_string, "%u", (unsigned) uid);
83 tail->name = xstrdup (usernum_string);
86 tail->name = xstrdup (pwent->pw_name);
88 /* Add to the head of the list, so most recently used is first. */
89 tail->next = user_alist;
94 /* Translate USER to a UID, with cache.
95 Return NULL if there is no such user.
96 (We also cache which user names have no passwd entry,
97 so we don't keep looking them up.) */
100 getuidbyname (char *user)
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 /* Use the same struct as for userids. */
135 static struct userid *group_alist;
136 static struct userid *nogroup_alist;
138 /* Translate GID to a group name or a stringified number,
144 register struct userid *tail;
146 char groupnum_string[20];
148 for (tail = group_alist; tail; tail = tail->next)
149 if (tail->id.g == gid)
152 grent = getgrgid (gid);
153 tail = (struct userid *) xmalloc (sizeof (struct userid));
157 sprintf (groupnum_string, "%u", (unsigned int) gid);
158 tail->name = xstrdup (groupnum_string);
161 tail->name = xstrdup (grent->gr_name);
163 /* Add to the head of the list, so most recently used is first. */
164 tail->next = group_alist;
169 /* Translate GROUP to a UID, 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.) */
175 getgidbyname (char *group)
177 register struct userid *tail;
180 for (tail = group_alist; tail; tail = tail->next)
181 /* Avoid a function call for the most common case. */
182 if (*tail->name == *group && !strcmp (tail->name, group))
185 for (tail = nogroup_alist; tail; tail = tail->next)
186 /* Avoid a function call for the most common case. */
187 if (*tail->name == *group && !strcmp (tail->name, group))
190 grent = getgrnam (group);
192 tail = (struct userid *) xmalloc (sizeof (struct userid));
193 tail->name = xstrdup (group);
195 /* Add to the head of the list, so most recently used is first. */
198 tail->id.g = grent->gr_gid;
199 tail->next = group_alist;
204 tail->next = nogroup_alist;
205 nogroup_alist = tail;