Remove K&R cruft.
[gnulib.git] / lib / idcache.c
1 /* idcache.c -- map user and group IDs, cached for speed
2    Copyright (C) 1985, 1988, 1989, 1990, 1997, 1998, 2003 Free Software
3    Foundation, Inc.
4
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.
9
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.
14
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.  */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <pwd.h>
27 #include <grp.h>
28
29 #if HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32
33 #include "xalloc.h"
34
35 #ifndef _POSIX_VERSION
36 struct passwd *getpwuid ();
37 struct passwd *getpwnam ();
38 struct group *getgrgid ();
39 struct group *getgrnam ();
40 #endif
41
42 #ifdef __DJGPP__
43 static char digits[] = "0123456789";
44 #endif
45
46 struct userid
47 {
48   union
49     {
50       uid_t u;
51       gid_t g;
52     } id;
53   char *name;
54   struct userid *next;
55 };
56
57 static struct userid *user_alist;
58
59 /* The members of this list have names not in the local passwd file.  */
60 static struct userid *nouser_alist;
61
62 /* Translate UID to a login name, with cache, or NULL if unresolved.  */
63
64 char *
65 getuser (uid_t uid)
66 {
67   register struct userid *tail;
68   struct passwd *pwent;
69
70   for (tail = user_alist; tail; tail = tail->next)
71     if (tail->id.u == uid)
72       return tail->name;
73
74   pwent = getpwuid (uid);
75   tail = xmalloc (sizeof (struct userid));
76   tail->id.u = uid;
77   tail->name = pwent ? xstrdup (pwent->pw_name) : NULL;
78
79   /* Add to the head of the list, so most recently used is first.  */
80   tail->next = user_alist;
81   user_alist = tail;
82   return tail->name;
83 }
84
85 /* Translate USER to a UID, with cache.
86    Return NULL if there is no such user.
87    (We also cache which user names have no passwd entry,
88    so we don't keep looking them up.)  */
89
90 uid_t *
91 getuidbyname (const char *user)
92 {
93   register struct userid *tail;
94   struct passwd *pwent;
95
96   for (tail = user_alist; tail; tail = tail->next)
97     /* Avoid a function call for the most common case.  */
98     if (*tail->name == *user && !strcmp (tail->name, user))
99       return &tail->id.u;
100
101   for (tail = nouser_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 0;
105
106   pwent = getpwnam (user);
107 #ifdef __DJGPP__
108   /* We need to pretend to be the user USER, to make
109      pwd functions know about an arbitrary user name.  */
110   if (!pwent && strspn (user, digits) < strlen (user))
111     {
112       setenv ("USER", user, 1);
113       pwent = getpwnam (user);  /* now it will succeed */
114     }
115 #endif
116
117   tail = xmalloc (sizeof (struct userid));
118   tail->name = xstrdup (user);
119
120   /* Add to the head of the list, so most recently used is first.  */
121   if (pwent)
122     {
123       tail->id.u = pwent->pw_uid;
124       tail->next = user_alist;
125       user_alist = tail;
126       return &tail->id.u;
127     }
128
129   tail->next = nouser_alist;
130   nouser_alist = tail;
131   return 0;
132 }
133
134 /* Use the same struct as for userids.  */
135 static struct userid *group_alist;
136 static struct userid *nogroup_alist;
137
138 /* Translate GID to a group name, with cache, or NULL if unresolved.  */
139
140 char *
141 getgroup (gid_t gid)
142 {
143   register struct userid *tail;
144   struct group *grent;
145
146   for (tail = group_alist; tail; tail = tail->next)
147     if (tail->id.g == gid)
148       return tail->name;
149
150   grent = getgrgid (gid);
151   tail = xmalloc (sizeof (struct userid));
152   tail->id.g = gid;
153   tail->name = grent ? xstrdup (grent->gr_name) : NULL;
154
155   /* Add to the head of the list, so most recently used is first.  */
156   tail->next = group_alist;
157   group_alist = tail;
158   return tail->name;
159 }
160
161 /* Translate GROUP to a GID, with cache.
162    Return NULL if there is no such group.
163    (We also cache which group names have no group entry,
164    so we don't keep looking them up.)  */
165
166 gid_t *
167 getgidbyname (const char *group)
168 {
169   register struct userid *tail;
170   struct group *grent;
171
172   for (tail = group_alist; tail; tail = tail->next)
173     /* Avoid a function call for the most common case.  */
174     if (*tail->name == *group && !strcmp (tail->name, group))
175       return &tail->id.g;
176
177   for (tail = nogroup_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 0;
181
182   grent = getgrnam (group);
183 #ifdef __DJGPP__
184   /* We need to pretend to belong to group GROUP, to make
185      grp functions know about any arbitrary group name.  */
186   if (!grent && strspn (group, digits) < strlen (group))
187     {
188       setenv ("GROUP", group, 1);
189       grent = getgrnam (group); /* now it will succeed */
190     }
191 #endif
192
193   tail = xmalloc (sizeof (struct userid));
194   tail->name = xstrdup (group);
195
196   /* Add to the head of the list, so most recently used is first.  */
197   if (grent)
198     {
199       tail->id.g = grent->gr_gid;
200       tail->next = group_alist;
201       group_alist = tail;
202       return &tail->id.g;
203     }
204
205   tail->next = nogroup_alist;
206   nogroup_alist = tail;
207   return 0;
208 }