* locate/frcode.c, locate/code.c, locate/bigram.c: change return
[findutils.git] / lib / idcache.c
blobb8104f1790b60dbc9f2bece89cd0c2da40361e30
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)
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 STDC_HEADERS || 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 #ifdef __DJGPP__
48 static char digits[] = "0123456789";
49 #endif
51 struct userid
53 union
55 uid_t u;
56 gid_t g;
57 } id;
58 char *name;
59 struct userid *next;
62 static struct userid *user_alist;
64 /* The members of this list have names not in the local passwd file. */
65 static struct userid *nouser_alist;
67 /* Translate UID to a login name, with cache, or NULL if unresolved. */
69 char *
70 getuser (uid_t uid)
72 register struct userid *tail;
73 struct passwd *pwent;
75 for (tail = user_alist; tail; tail = tail->next)
76 if (tail->id.u == uid)
77 return tail->name;
79 pwent = getpwuid (uid);
80 tail = (struct userid *) xmalloc (sizeof (struct userid));
81 tail->id.u = uid;
82 tail->name = pwent ? xstrdup (pwent->pw_name) : NULL;
84 /* Add to the head of the list, so most recently used is first. */
85 tail->next = user_alist;
86 user_alist = tail;
87 return tail->name;
90 /* Translate USER to a UID, with cache.
91 Return NULL if there is no such user.
92 (We also cache which user names have no passwd entry,
93 so we don't keep looking them up.) */
95 uid_t *
96 getuidbyname (const char *user)
98 register struct userid *tail;
99 struct passwd *pwent;
101 for (tail = user_alist; tail; tail = tail->next)
102 /* Avoid a function call for the most common case. */
103 if (*tail->name == *user && !strcmp (tail->name, user))
104 return &tail->id.u;
106 for (tail = nouser_alist; tail; tail = tail->next)
107 /* Avoid a function call for the most common case. */
108 if (*tail->name == *user && !strcmp (tail->name, user))
109 return 0;
111 pwent = getpwnam (user);
112 #ifdef __DJGPP__
113 /* We need to pretend to be the user USER, to make
114 pwd functions know about an arbitrary user name. */
115 if (!pwent && strspn (user, digits) < strlen (user))
117 setenv ("USER", user, 1);
118 pwent = getpwnam (user); /* now it will succeed */
120 #endif
122 tail = (struct userid *) xmalloc (sizeof (struct userid));
123 tail->name = xstrdup (user);
125 /* Add to the head of the list, so most recently used is first. */
126 if (pwent)
128 tail->id.u = pwent->pw_uid;
129 tail->next = user_alist;
130 user_alist = tail;
131 return &tail->id.u;
134 tail->next = nouser_alist;
135 nouser_alist = tail;
136 return 0;
139 /* Use the same struct as for userids. */
140 static struct userid *group_alist;
141 static struct userid *nogroup_alist;
143 /* Translate GID to a group name, with cache, or NULL if unresolved. */
145 char *
146 getgroup (gid_t gid)
148 register struct userid *tail;
149 struct group *grent;
151 for (tail = group_alist; tail; tail = tail->next)
152 if (tail->id.g == gid)
153 return tail->name;
155 grent = getgrgid (gid);
156 tail = (struct userid *) xmalloc (sizeof (struct userid));
157 tail->id.g = gid;
158 tail->name = grent ? xstrdup (grent->gr_name) : NULL;
160 /* Add to the head of the list, so most recently used is first. */
161 tail->next = group_alist;
162 group_alist = tail;
163 return tail->name;
166 /* Translate GROUP to a GID, with cache.
167 Return NULL if there is no such group.
168 (We also cache which group names have no group entry,
169 so we don't keep looking them up.) */
171 gid_t *
172 getgidbyname (const char *group)
174 register struct userid *tail;
175 struct group *grent;
177 for (tail = group_alist; tail; tail = tail->next)
178 /* Avoid a function call for the most common case. */
179 if (*tail->name == *group && !strcmp (tail->name, group))
180 return &tail->id.g;
182 for (tail = nogroup_alist; tail; tail = tail->next)
183 /* Avoid a function call for the most common case. */
184 if (*tail->name == *group && !strcmp (tail->name, group))
185 return 0;
187 grent = getgrnam (group);
188 #ifdef __DJGPP__
189 /* We need to pretend to belong to group GROUP, to make
190 grp functions know about any arbitrary group name. */
191 if (!grent && strspn (group, digits) < strlen (group))
193 setenv ("GROUP", group, 1);
194 grent = getgrnam (group); /* now it will succeed */
196 #endif
198 tail = (struct userid *) xmalloc (sizeof (struct userid));
199 tail->name = xstrdup (group);
201 /* Add to the head of the list, so most recently used is first. */
202 if (grent)
204 tail->id.g = grent->gr_gid;
205 tail->next = group_alist;
206 group_alist = tail;
207 return &tail->id.g;
210 tail->next = nogroup_alist;
211 nogroup_alist = tail;
212 return 0;