Changes the location of bison.simple after running bison on local
[findutils.git] / lib / idcache.c
blob7a827e3b004aab7200178b012563e5c3c5483049
1 /* idcache.c -- map user and group IDs, cached for speed
2 Copyright (C) 1985, 1988, 1989, 1990, 1997, 1998 Free Software
3 Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <pwd.h>
26 #include <grp.h>
28 #if STDC_HEADERS || HAVE_STRING_H
29 # include <string.h>
30 #else
31 # include <strings.h>
32 #endif
34 #if HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
38 #ifndef _POSIX_VERSION
39 struct passwd *getpwuid ();
40 struct passwd *getpwnam ();
41 struct group *getgrgid ();
42 struct group *getgrnam ();
43 #endif
45 char *xmalloc ();
46 char *xstrdup ();
48 #ifdef __DJGPP__
49 static char digits[] = "0123456789";
50 #endif
52 struct userid
54 union
56 uid_t u;
57 gid_t g;
58 } id;
59 char *name;
60 struct userid *next;
63 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, with cache, or NULL if unresolved. */
70 char *
71 getuser (uid_t uid)
73 register struct userid *tail;
74 struct passwd *pwent;
76 for (tail = user_alist; tail; tail = tail->next)
77 if (tail->id.u == uid)
78 return tail->name;
80 pwent = getpwuid (uid);
81 tail = (struct userid *) xmalloc (sizeof (struct userid));
82 tail->id.u = uid;
83 tail->name = pwent ? xstrdup (pwent->pw_name) : NULL;
85 /* Add to the head of the list, so most recently used is first. */
86 tail->next = user_alist;
87 user_alist = tail;
88 return tail->name;
91 /* Translate USER to a UID, with cache.
92 Return NULL if there is no such user.
93 (We also cache which user names have no passwd entry,
94 so we don't keep looking them up.) */
96 uid_t *
97 getuidbyname (const char *user)
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))
105 return &tail->id.u;
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))
110 return 0;
112 pwent = getpwnam (user);
113 #ifdef __DJGPP__
114 /* We need to pretend to be the user USER, to make
115 pwd functions know about an arbitrary user name. */
116 if (!pwent && strspn (user, digits) < strlen (user))
118 setenv ("USER", user, 1);
119 pwent = getpwnam (user); /* now it will succeed */
121 #endif
123 tail = (struct userid *) xmalloc (sizeof (struct userid));
124 tail->name = xstrdup (user);
126 /* Add to the head of the list, so most recently used is first. */
127 if (pwent)
129 tail->id.u = pwent->pw_uid;
130 tail->next = user_alist;
131 user_alist = tail;
132 return &tail->id.u;
135 tail->next = nouser_alist;
136 nouser_alist = tail;
137 return 0;
140 /* Use the same struct as for userids. */
141 static struct userid *group_alist;
142 static struct userid *nogroup_alist;
144 /* Translate GID to a group name, with cache, or NULL if unresolved. */
146 char *
147 getgroup (gid_t gid)
149 register struct userid *tail;
150 struct group *grent;
152 for (tail = group_alist; tail; tail = tail->next)
153 if (tail->id.g == gid)
154 return tail->name;
156 grent = getgrgid (gid);
157 tail = (struct userid *) xmalloc (sizeof (struct userid));
158 tail->id.g = gid;
159 tail->name = grent ? xstrdup (grent->gr_name) : NULL;
161 /* Add to the head of the list, so most recently used is first. */
162 tail->next = group_alist;
163 group_alist = tail;
164 return tail->name;
167 /* Translate GROUP to a GID, 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.) */
172 gid_t *
173 getgidbyname (const char *group)
175 register struct userid *tail;
176 struct group *grent;
178 for (tail = group_alist; tail; tail = tail->next)
179 /* Avoid a function call for the most common case. */
180 if (*tail->name == *group && !strcmp (tail->name, group))
181 return &tail->id.g;
183 for (tail = nogroup_alist; tail; tail = tail->next)
184 /* Avoid a function call for the most common case. */
185 if (*tail->name == *group && !strcmp (tail->name, group))
186 return 0;
188 grent = getgrnam (group);
189 #ifdef __DJGPP__
190 /* We need to pretend to belong to group GROUP, to make
191 grp functions know about any arbitrary group name. */
192 if (!grent && strspn (group, digits) < strlen (group))
194 setenv ("GROUP", group, 1);
195 grent = getgrnam (group); /* now it will succeed */
197 #endif
199 tail = (struct userid *) xmalloc (sizeof (struct userid));
200 tail->name = xstrdup (group);
202 /* Add to the head of the list, so most recently used is first. */
203 if (grent)
205 tail->id.g = grent->gr_gid;
206 tail->next = group_alist;
207 group_alist = tail;
208 return &tail->id.g;
211 tail->next = nogroup_alist;
212 nogroup_alist = tail;
213 return 0;