(getuidbyname): Declare parameter to be const.
[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 static struct userid *user_alist;
59
60 #ifdef NOT_USED
61 /* The members of this list have names not in the local passwd file.  */
62 static struct userid *nouser_alist;
63 #endif /* NOT_USED */
64
65 /* Translate UID to a login name, with cache.
66    If UID cannot be resolved, return NULL.
67    Cache lookup failures, too.  */
68
69 char *
70 getuser (uid)
71      uid_t uid;
72 {
73   register struct userid *tail;
74   struct passwd *pwent;
75
76   for (tail = user_alist; tail; tail = tail->next)
77     if (tail->id.u == uid)
78       return tail->name;
79
80   pwent = getpwuid (uid);
81   tail = (struct userid *) xmalloc (sizeof (struct userid));
82   tail->id.u = uid;
83   tail->name = (pwent ? xstrdup (pwent->pw_name) : NULL);
84
85   /* Add to the head of the list, so most recently used is first.  */
86   tail->next = user_alist;
87   user_alist = tail;
88   return tail->name;
89 }
90
91 #ifdef NOT_USED
92
93 /* Translate USER to a UID, with cache.
94    Return NULL if there is no such user.
95    (We also cache which user names have no passwd entry,
96    so we don't keep looking them up.)  */
97
98 uid_t *
99 getuidbyname (user)
100      const char *user;
101 {
102   register struct userid *tail;
103   struct passwd *pwent;
104
105   for (tail = user_alist; tail; tail = tail->next)
106     /* Avoid a function call for the most common case.  */
107     if (*tail->name == *user && !strcmp (tail->name, user))
108       return &tail->id.u;
109
110   for (tail = nouser_alist; tail; tail = tail->next)
111     /* Avoid a function call for the most common case.  */
112     if (*tail->name == *user && !strcmp (tail->name, user))
113       return 0;
114
115   pwent = getpwnam (user);
116
117   tail = (struct userid *) 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 #endif /* NOT_USED */
135
136 /* Use the same struct as for userids.  */
137 static struct userid *group_alist;
138 static struct userid *nogroup_alist;
139
140 /* Translate GID to a group name or a stringified number,
141    with cache.  */
142
143 char *
144 getgroup (gid)
145      gid_t gid;
146 {
147   register struct userid *tail;
148   struct group *grent;
149
150   for (tail = group_alist; tail; tail = tail->next)
151     if (tail->id.g == gid)
152       return tail->name;
153
154   grent = getgrgid (gid);
155   tail = (struct userid *) xmalloc (sizeof (struct userid));
156   tail->id.g = gid;
157   tail->name = (grent ? xstrdup (grent->gr_name) : NULL);
158
159   /* Add to the head of the list, so most recently used is first.  */
160   tail->next = group_alist;
161   group_alist = tail;
162   return tail->name;
163 }
164
165 #ifdef NOT_USED
166
167 /* Translate GROUP to a GID, with cache.
168    Return NULL if there is no such group.
169    (We also cache which group names have no group entry,
170    so we don't keep looking them up.)  */
171
172 gid_t *
173 getgidbyname (group)
174      const char *group;
175 {
176   register struct userid *tail;
177   struct group *grent;
178
179   for (tail = group_alist; tail; tail = tail->next)
180     /* Avoid a function call for the most common case.  */
181     if (*tail->name == *group && !strcmp (tail->name, group))
182       return &tail->id.g;
183
184   for (tail = nogroup_alist; tail; tail = tail->next)
185     /* Avoid a function call for the most common case.  */
186     if (*tail->name == *group && !strcmp (tail->name, group))
187       return 0;
188
189   grent = getgrnam (group);
190
191   tail = (struct userid *) xmalloc (sizeof (struct userid));
192   tail->name = xstrdup (group);
193
194   /* Add to the head of the list, so most recently used is first.  */
195   if (grent)
196     {
197       tail->id.g = grent->gr_gid;
198       tail->next = group_alist;
199       group_alist = tail;
200       return &tail->id.g;
201     }
202
203   tail->next = nogroup_alist;
204   nogroup_alist = tail;
205   return 0;
206 }
207
208 #endif /* NOT_USED */