1 /* idcache.c -- map user and group IDs, cached for speed
2 Copyright (C) 1985, 1988, 1989, 1990, 1997, 1998 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 STDC_HEADERS || 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, with cache, or NULL if unresolved. */
68 register struct userid *tail;
71 for (tail = user_alist; tail; tail = tail->next)
72 if (tail->id.u == uid)
75 pwent = getpwuid (uid);
76 tail = (struct userid *) xmalloc (sizeof (struct userid));
78 tail->name = pwent ? xstrdup (pwent->pw_name) : NULL;
80 /* Add to the head of the list, so most recently used is first. */
81 tail->next = user_alist;
86 /* Translate USER to a UID, with cache.
87 Return NULL if there is no such user.
88 (We also cache which user names have no passwd entry,
89 so we don't keep looking them up.) */
92 getuidbyname (const char *user)
94 register struct userid *tail;
97 for (tail = user_alist; tail; tail = tail->next)
98 /* Avoid a function call for the most common case. */
99 if (*tail->name == *user && !strcmp (tail->name, user))
102 for (tail = nouser_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 pwent = getpwnam (user);
109 tail = (struct userid *) xmalloc (sizeof (struct userid));
110 tail->name = xstrdup (user);
112 /* Add to the head of the list, so most recently used is first. */
115 tail->id.u = pwent->pw_uid;
116 tail->next = user_alist;
121 tail->next = nouser_alist;
126 /* Use the same struct as for userids. */
127 static struct userid *group_alist;
128 static struct userid *nogroup_alist;
130 /* Translate GID to a group name, with cache, or NULL if unresolved. */
135 register struct userid *tail;
138 for (tail = group_alist; tail; tail = tail->next)
139 if (tail->id.g == gid)
142 grent = getgrgid (gid);
143 tail = (struct userid *) xmalloc (sizeof (struct userid));
145 tail->name = grent ? xstrdup (grent->gr_name) : NULL;
147 /* Add to the head of the list, so most recently used is first. */
148 tail->next = group_alist;
153 /* Translate GROUP to a GID, with cache.
154 Return NULL if there is no such group.
155 (We also cache which group names have no group entry,
156 so we don't keep looking them up.) */
159 getgidbyname (const char *group)
161 register struct userid *tail;
164 for (tail = group_alist; tail; tail = tail->next)
165 /* Avoid a function call for the most common case. */
166 if (*tail->name == *group && !strcmp (tail->name, group))
169 for (tail = nogroup_alist; tail; tail = tail->next)
170 /* Avoid a function call for the most common case. */
171 if (*tail->name == *group && !strcmp (tail->name, group))
174 grent = getgrnam (group);
176 tail = (struct userid *) xmalloc (sizeof (struct userid));
177 tail->name = xstrdup (group);
179 /* Add to the head of the list, so most recently used is first. */
182 tail->id.g = grent->gr_gid;
183 tail->next = group_alist;
188 tail->next = nogroup_alist;
189 nogroup_alist = tail;