Use cleaner syntax: NULL rather than 0.
[gnulib.git] / lib / idcache.c
1 /* idcache.c -- map user and group IDs, cached for speed
2
3    Copyright (C) 1985, 1988, 1989, 1990, 1997, 1998, 2003, 2005, 2006
4    Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <pwd.h>
26 #include <grp.h>
27
28 #include <unistd.h>
29
30 #include "xalloc.h"
31
32 #ifdef __DJGPP__
33 static char digits[] = "0123456789";
34 #endif
35
36 struct userid
37 {
38   union
39     {
40       uid_t u;
41       gid_t g;
42     } id;
43   char *name;
44   struct userid *next;
45 };
46
47 static struct userid *user_alist;
48
49 /* The members of this list have names not in the local passwd file.  */
50 static struct userid *nouser_alist;
51
52 /* Translate UID to a login name, with cache, or NULL if unresolved.  */
53
54 char *
55 getuser (uid_t uid)
56 {
57   register struct userid *tail;
58   struct passwd *pwent;
59
60   for (tail = user_alist; tail; tail = tail->next)
61     if (tail->id.u == uid)
62       return tail->name;
63
64   pwent = getpwuid (uid);
65   tail = xmalloc (sizeof *tail);
66   tail->id.u = uid;
67   tail->name = pwent ? xstrdup (pwent->pw_name) : NULL;
68
69   /* Add to the head of the list, so most recently used is first.  */
70   tail->next = user_alist;
71   user_alist = tail;
72   return tail->name;
73 }
74
75 /* Translate USER to a UID, with cache.
76    Return NULL if there is no such user.
77    (We also cache which user names have no passwd entry,
78    so we don't keep looking them up.)  */
79
80 uid_t *
81 getuidbyname (const char *user)
82 {
83   register struct userid *tail;
84   struct passwd *pwent;
85
86   for (tail = user_alist; tail; tail = tail->next)
87     /* Avoid a function call for the most common case.  */
88     if (*tail->name == *user && !strcmp (tail->name, user))
89       return &tail->id.u;
90
91   for (tail = nouser_alist; tail; tail = tail->next)
92     /* Avoid a function call for the most common case.  */
93     if (*tail->name == *user && !strcmp (tail->name, user))
94       return NULL;
95
96   pwent = getpwnam (user);
97 #ifdef __DJGPP__
98   /* We need to pretend to be the user USER, to make
99      pwd functions know about an arbitrary user name.  */
100   if (!pwent && strspn (user, digits) < strlen (user))
101     {
102       setenv ("USER", user, 1);
103       pwent = getpwnam (user);  /* now it will succeed */
104     }
105 #endif
106
107   tail = xmalloc (sizeof *tail);
108   tail->name = xstrdup (user);
109
110   /* Add to the head of the list, so most recently used is first.  */
111   if (pwent)
112     {
113       tail->id.u = pwent->pw_uid;
114       tail->next = user_alist;
115       user_alist = tail;
116       return &tail->id.u;
117     }
118
119   tail->next = nouser_alist;
120   nouser_alist = tail;
121   return NULL;
122 }
123
124 /* Use the same struct as for userids.  */
125 static struct userid *group_alist;
126 static struct userid *nogroup_alist;
127
128 /* Translate GID to a group name, with cache, or NULL if unresolved.  */
129
130 char *
131 getgroup (gid_t gid)
132 {
133   register struct userid *tail;
134   struct group *grent;
135
136   for (tail = group_alist; tail; tail = tail->next)
137     if (tail->id.g == gid)
138       return tail->name;
139
140   grent = getgrgid (gid);
141   tail = xmalloc (sizeof *tail);
142   tail->id.g = gid;
143   tail->name = grent ? xstrdup (grent->gr_name) : NULL;
144
145   /* Add to the head of the list, so most recently used is first.  */
146   tail->next = group_alist;
147   group_alist = tail;
148   return tail->name;
149 }
150
151 /* Translate GROUP to a GID, with cache.
152    Return NULL if there is no such group.
153    (We also cache which group names have no group entry,
154    so we don't keep looking them up.)  */
155
156 gid_t *
157 getgidbyname (const char *group)
158 {
159   register struct userid *tail;
160   struct group *grent;
161
162   for (tail = group_alist; tail; tail = tail->next)
163     /* Avoid a function call for the most common case.  */
164     if (*tail->name == *group && !strcmp (tail->name, group))
165       return &tail->id.g;
166
167   for (tail = nogroup_alist; tail; tail = tail->next)
168     /* Avoid a function call for the most common case.  */
169     if (*tail->name == *group && !strcmp (tail->name, group))
170       return NULL;
171
172   grent = getgrnam (group);
173 #ifdef __DJGPP__
174   /* We need to pretend to belong to group GROUP, to make
175      grp functions know about any arbitrary group name.  */
176   if (!grent && strspn (group, digits) < strlen (group))
177     {
178       setenv ("GROUP", group, 1);
179       grent = getgrnam (group); /* now it will succeed */
180     }
181 #endif
182
183   tail = xmalloc (sizeof *tail);
184   tail->name = xstrdup (group);
185
186   /* Add to the head of the list, so most recently used is first.  */
187   if (grent)
188     {
189       tail->id.g = grent->gr_gid;
190       tail->next = group_alist;
191       group_alist = tail;
192       return &tail->id.g;
193     }
194
195   tail->next = nogroup_alist;
196   nogroup_alist = tail;
197   return NULL;
198 }