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