(getuidbyname, getgidbyname): Names can now
[gnulib.git] / lib / idcache.c
1 /* idcache.c -- map user and group IDs, cached for speed
2    Copyright (C) 1985, 1988, 1989, 1990, 1997 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <pwd.h>
25 #include <grp.h>
26
27 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
28 # include <string.h>
29 #else
30 # include <strings.h>
31 #endif
32
33 #if HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36
37 #ifndef _POSIX_VERSION
38 struct passwd *getpwuid ();
39 struct passwd *getpwnam ();
40 struct group *getgrgid ();
41 struct group *getgrnam ();
42 #endif
43
44 char *xmalloc ();
45 char *xstrdup ();
46
47 struct userid
48 {
49   union
50     {
51       uid_t u;
52       gid_t g;
53     } id;
54   char *name;
55   struct userid *next;
56 };
57
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;
61
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 */
67
68 /* Translate UID to a login name, with cache.
69    If UID cannot be resolved, return NULL.
70    Cache lookup failures, too.  */
71
72 char *
73 getuser (uid)
74      uid_t uid;
75 {
76   register struct userid *tail;
77   struct passwd *pwent;
78
79   for (tail = user_alist; tail; tail = tail->next)
80     if (tail->id.u == uid)
81       return tail->name;
82
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);
87
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;
92 }
93
94 #ifdef NOT_USED
95
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.)  */
100
101 uid_t *
102 getuidbyname (user)
103      const char *user;
104 {
105   register struct userid *tail;
106   struct passwd *pwent;
107
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;
112
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;
117
118   pwent = getpwnam (user);
119
120   tail = (struct userid *) xmalloc (sizeof (struct userid));
121   tail->name = xstrdup (user);
122
123   /* Add to the head of the list, so most recently added is first.  */
124   if (pwent)
125     {
126       tail->id.u = pwent->pw_uid;
127       tail->next = user_alist;
128       user_alist = tail;
129       return &tail->id.u;
130     }
131
132   tail->next = nouser_alist;
133   nouser_alist = tail;
134   return 0;
135 }
136
137 #endif /* NOT_USED */
138
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
144
145 /* Translate GID to a group name, with cache.
146    Return NULL if the group has no name.  */
147
148 char *
149 getgroup (gid)
150      gid_t gid;
151 {
152   register struct userid *tail;
153   struct group *grent;
154
155   for (tail = group_alist; tail; tail = tail->next)
156     if (tail->id.g == gid)
157       return tail->name;
158
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);
163
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;
168 }
169
170 #ifdef NOT_USED
171
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.)  */
176
177 gid_t *
178 getgidbyname (group)
179      const char *group;
180 {
181   register struct userid *tail;
182   struct group *grent;
183
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;
188
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;
193
194   grent = getgrnam (group);
195
196   tail = (struct userid *) xmalloc (sizeof (struct userid));
197   tail->name = xstrdup (group);
198
199   /* Add to the head of the list, so most recently used is first.  */
200   if (grent)
201     {
202       tail->id.g = grent->gr_gid;
203       tail->next = group_alist;
204       group_alist = tail;
205       return &tail->id.g;
206     }
207
208   tail->next = nogroup_alist;
209   nogroup_alist = tail;
210   return 0;
211 }
212
213 #endif /* NOT_USED */