.
[coreutils.git] / lib / idcache.c
blob0cfd9034854a0b092dbdcda4ab1e0e63e0b574bd
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)
7 any later version.
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. */
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <pwd.h>
25 #include <grp.h>
27 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
28 # include <string.h>
29 #else
30 # include <strings.h>
31 #endif
33 #if HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
37 #ifndef _POSIX_VERSION
38 struct passwd *getpwuid ();
39 struct passwd *getpwnam ();
40 struct group *getgrgid ();
41 struct group *getgrnam ();
42 #endif
44 char *xmalloc ();
45 char *xstrdup ();
47 struct userid
49 union
51 uid_t u;
52 gid_t g;
53 } id;
54 char *name;
55 struct userid *next;
58 /* The members of this list have already been looked up.
59 If a name is NULL, the corresponding id is not in the password file. */
60 static struct userid *user_alist;
62 #ifdef NOT_USED
63 /* The members of this list are names not in the local passwd file;
64 their names are always not NULL, and their ids are irrelevant. */
65 static struct userid *nouser_alist;
66 #endif /* NOT_USED */
68 /* Translate UID to a login name, with cache.
69 If UID cannot be resolved, return NULL.
70 Cache lookup failures, too. */
72 char *
73 getuser (uid)
74 uid_t uid;
76 register struct userid *tail;
77 struct passwd *pwent;
79 for (tail = user_alist; tail; tail = tail->next)
80 if (tail->id.u == uid)
81 return tail->name;
83 pwent = getpwuid (uid);
84 tail = (struct userid *) xmalloc (sizeof (struct userid));
85 tail->id.u = uid;
86 tail->name = (pwent ? xstrdup (pwent->pw_name) : NULL);
88 /* Add to the head of the list, so most recently added is first. */
89 tail->next = user_alist;
90 user_alist = tail;
91 return tail->name;
94 #ifdef NOT_USED
96 /* Translate USER to a UID, with cache.
97 Return NULL if there is no such user.
98 (We also cache which user names have no passwd entry,
99 so we don't keep looking them up.) */
101 uid_t *
102 getuidbyname (user)
103 const 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 && *tail->name == *user && !strcmp (tail->name, user))
111 return &tail->id.u;
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))
116 return 0;
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 added is first. */
124 if (pwent)
126 tail->id.u = pwent->pw_uid;
127 tail->next = user_alist;
128 user_alist = tail;
129 return &tail->id.u;
132 tail->next = nouser_alist;
133 nouser_alist = tail;
134 return 0;
137 #endif /* NOT_USED */
139 /* Use the same struct as for userids. */
140 static struct userid *group_alist;
141 #ifdef NOT_USED
142 static struct userid *nogroup_alist;
143 #endif
145 /* Translate GID to a group name, with cache.
146 Return NULL if the group has no name. */
148 char *
149 getgroup (gid)
150 gid_t gid;
152 register struct userid *tail;
153 struct group *grent;
155 for (tail = group_alist; tail; tail = tail->next)
156 if (tail->id.g == gid)
157 return tail->name;
159 grent = getgrgid (gid);
160 tail = (struct userid *) xmalloc (sizeof (struct userid));
161 tail->id.g = gid;
162 tail->name = (grent ? xstrdup (grent->gr_name) : NULL);
164 /* Add to the head of the list, so most recently used is first. */
165 tail->next = group_alist;
166 group_alist = tail;
167 return tail->name;
170 #ifdef NOT_USED
172 /* Translate GROUP to a GID, 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.) */
177 gid_t *
178 getgidbyname (group)
179 const char *group;
181 register struct userid *tail;
182 struct group *grent;
184 for (tail = group_alist; tail; tail = tail->next)
185 /* Avoid a function call for the most common case. */
186 if (tail->name && *tail->name == *group && !strcmp (tail->name, group))
187 return &tail->id.g;
189 for (tail = nogroup_alist; tail; tail = tail->next)
190 /* Avoid a function call for the most common case. */
191 if (*tail->name == *group && !strcmp (tail->name, group))
192 return 0;
194 grent = getgrnam (group);
196 tail = (struct userid *) xmalloc (sizeof (struct userid));
197 tail->name = xstrdup (group);
199 /* Add to the head of the list, so most recently used is first. */
200 if (grent)
202 tail->id.g = grent->gr_gid;
203 tail->next = group_alist;
204 group_alist = tail;
205 return &tail->id.g;
208 tail->next = nogroup_alist;
209 nogroup_alist = tail;
210 return 0;
213 #endif /* NOT_USED */