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. */
25 #include <sys/types.h>
29 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
38 #ifndef _POSIX_VERSION
39 struct passwd *getpwuid ();
40 struct passwd *getpwnam ();
41 struct group *getgrgid ();
42 struct group *getgrnam ();
45 #define xstrdup strdup
46 #define xmalloc malloc
61 static struct userid *user_alist;
65 /* The members of this list have names not in the local passwd file. */
66 static struct userid *nouser_alist;
68 /* Translate UID to a login name or a stringified number,
71 char *getuser (uid_t uid)
73 register struct userid *tail;
75 char usernum_string[20];
77 for (tail = user_alist; tail; tail = tail->next)
78 if (tail->id.u == uid)
81 pwent = getpwuid (uid);
82 tail = (struct userid *) xmalloc (sizeof (struct userid));
85 sprintf (usernum_string, "%u", (unsigned) uid);
86 tail->name = xstrdup (usernum_string);
88 tail->name = xstrdup (pwent->pw_name);
91 /* Add to the head of the list, so most recently used is first. */
92 tail->next = user_alist;
97 /* Translate USER to a UID, with cache.
98 Return NULL if there is no such user.
99 (We also cache which user names have no passwd entry,
100 so we don't keep looking them up.) */
103 getuidbyname (char *user)
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 == *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 used is first. */
126 tail->id.u = pwent->pw_uid;
127 tail->next = user_alist;
132 tail->next = nouser_alist;
137 /* Use the same struct as for userids. */
138 static struct userid *group_alist;
139 static struct userid *nogroup_alist;
141 /* Translate GID to a group name or a stringified number,
147 register struct userid *tail;
149 char groupnum_string[20];
151 for (tail = group_alist; tail; tail = tail->next)
152 if (tail->id.g == gid)
155 grent = getgrgid (gid);
156 tail = (struct userid *) xmalloc (sizeof (struct userid));
160 sprintf (groupnum_string, "%u", (unsigned int) gid);
161 tail->name = xstrdup (groupnum_string);
164 tail->name = xstrdup (grent->gr_name);
166 /* Add to the head of the list, so most recently used is first. */
167 tail->next = group_alist;
172 /* Translate GROUP to a UID, 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.) */
178 getgidbyname (char *group)
180 register struct userid *tail;
183 for (tail = group_alist; tail; tail = tail->next)
184 /* Avoid a function call for the most common case. */
185 if (*tail->name == *group && !strcmp (tail->name, group))
188 for (tail = nogroup_alist; tail; tail = tail->next)
189 /* Avoid a function call for the most common case. */
190 if (*tail->name == *group && !strcmp (tail->name, group))
193 grent = getgrnam (group);
195 tail = (struct userid *) xmalloc (sizeof (struct userid));
196 tail->name = xstrdup (group);
198 /* Add to the head of the list, so most recently used is first. */
201 tail->id.g = grent->gr_gid;
202 tail->next = group_alist;
207 tail->next = nogroup_alist;
208 nogroup_alist = tail;