(getuidbyname) [__DJGPP__]: Make function know about
[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 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 STDC_HEADERS || 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 #ifdef __DJGPP__
48 static char digits[] = "0123456789";
49 #endif
50
51 struct userid
52 {
53   union
54     {
55       uid_t u;
56       gid_t g;
57     } id;
58   char *name;
59   struct userid *next;
60 };
61
62 static struct userid *user_alist;
63
64 /* The members of this list have names not in the local passwd file.  */
65 static struct userid *nouser_alist;
66
67 /* Translate UID to a login name, with cache, or NULL if unresolved.  */
68
69 char *
70 getuser (uid_t uid)
71 {
72   register struct userid *tail;
73   struct passwd *pwent;
74
75   for (tail = user_alist; tail; tail = tail->next)
76     if (tail->id.u == uid)
77       return tail->name;
78
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;
83
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;
88 }
89
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.)  */
94
95 uid_t *
96 getuidbyname (const char *user)
97 {
98   register struct userid *tail;
99   struct passwd *pwent;
100
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;
105
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;
110
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))
116     {
117       setenv ("USER", user, 1);
118       pwent = getpwnam (user);  /* now it will succeed */
119     }
120 #endif
121
122   tail = (struct userid *) xmalloc (sizeof (struct userid));
123   tail->name = xstrdup (user);
124
125   /* Add to the head of the list, so most recently used is first.  */
126   if (pwent)
127     {
128       tail->id.u = pwent->pw_uid;
129       tail->next = user_alist;
130       user_alist = tail;
131       return &tail->id.u;
132     }
133
134   tail->next = nouser_alist;
135   nouser_alist = tail;
136   return 0;
137 }
138
139 /* Use the same struct as for userids.  */
140 static struct userid *group_alist;
141 static struct userid *nogroup_alist;
142
143 /* Translate GID to a group name, with cache, or NULL if unresolved.  */
144
145 char *
146 getgroup (gid_t gid)
147 {
148   register struct userid *tail;
149   struct group *grent;
150
151   for (tail = group_alist; tail; tail = tail->next)
152     if (tail->id.g == gid)
153       return tail->name;
154
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;
159
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;
164 }
165
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.)  */
170
171 gid_t *
172 getgidbyname (const char *group)
173 {
174   register struct userid *tail;
175   struct group *grent;
176
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;
181
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;
186
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))
192     {
193       setenv ("GROUP", group, 1);
194       grent = getgrnam (group); /* now it will succeed */
195     }
196 #endif
197
198   tail = (struct userid *) xmalloc (sizeof (struct userid));
199   tail->name = xstrdup (group);
200
201   /* Add to the head of the list, so most recently used is first.  */
202   if (grent)
203     {
204       tail->id.g = grent->gr_gid;
205       tail->next = group_alist;
206       group_alist = tail;
207       return &tail->id.g;
208     }
209
210   tail->next = nogroup_alist;
211   nogroup_alist = tail;
212   return 0;
213 }