1 /* idcache.c -- map user and group IDs, cached for speed
2 Copyright (C) 1985, 1988, 1989, 1990 Free Software Foundation, Inc.
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)
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.
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include <sys/types.h>
27 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
36 #ifndef _POSIX_VERSION
37 struct passwd
*getpwuid ();
38 struct passwd
*getpwnam ();
39 struct group
*getgrgid ();
40 struct group
*getgrnam ();
57 static struct userid
*user_alist
;
59 /* The members of this list have names not in the local passwd file. */
60 static struct userid
*nouser_alist
;
62 /* Translate UID to a login name or a stringified number,
69 register struct userid
*tail
;
71 char usernum_string
[20];
73 for (tail
= user_alist
; tail
; tail
= tail
->next
)
74 if (tail
->id
.u
== uid
)
77 pwent
= getpwuid (uid
);
78 tail
= (struct userid
*) xmalloc (sizeof (struct userid
));
82 sprintf (usernum_string
, "%u", (unsigned) uid
);
83 tail
->name
= xstrdup (usernum_string
);
86 tail
->name
= xstrdup (pwent
->pw_name
);
88 /* Add to the head of the list, so most recently used is first. */
89 tail
->next
= user_alist
;
94 /* Translate USER to a UID, with cache.
95 Return NULL if there is no such user.
96 (We also cache which user names have no passwd entry,
97 so we don't keep looking them up.) */
103 register struct userid
*tail
;
104 struct passwd
*pwent
;
106 for (tail
= user_alist
; tail
; tail
= tail
->next
)
107 /* Avoid a function call for the most common case. */
108 if (*tail
->name
== *user
&& !strcmp (tail
->name
, user
))
111 for (tail
= nouser_alist
; tail
; tail
= tail
->next
)
112 /* Avoid a function call for the most common case. */
113 if (*tail
->name
== *user
&& !strcmp (tail
->name
, user
))
116 pwent
= getpwnam (user
);
118 tail
= (struct userid
*) xmalloc (sizeof (struct userid
));
119 tail
->name
= xstrdup (user
);
121 /* Add to the head of the list, so most recently used is first. */
124 tail
->id
.u
= pwent
->pw_uid
;
125 tail
->next
= user_alist
;
130 tail
->next
= nouser_alist
;
135 /* Use the same struct as for userids. */
136 static struct userid
*group_alist
;
137 static struct userid
*nogroup_alist
;
139 /* Translate GID to a group name or a stringified number,
146 register struct userid
*tail
;
148 char groupnum_string
[20];
150 for (tail
= group_alist
; tail
; tail
= tail
->next
)
151 if (tail
->id
.g
== gid
)
154 grent
= getgrgid (gid
);
155 tail
= (struct userid
*) xmalloc (sizeof (struct userid
));
159 sprintf (groupnum_string
, "%u", (unsigned int) gid
);
160 tail
->name
= xstrdup (groupnum_string
);
163 tail
->name
= xstrdup (grent
->gr_name
);
165 /* Add to the head of the list, so most recently used is first. */
166 tail
->next
= group_alist
;
171 /* Translate GROUP to a UID, with cache.
172 Return NULL if there is no such group.
173 (We also cache which group names have no group entry,
174 so we don't keep looking them up.) */
180 register struct userid
*tail
;
183 for (tail
= group_alist
; tail
; tail
= tail
->next
)
184 /* Avoid a function call for the most common case. */
185 if (*tail
->name
== *group
&& !strcmp (tail
->name
, group
))
188 for (tail
= nogroup_alist
; tail
; tail
= tail
->next
)
189 /* Avoid a function call for the most common case. */
190 if (*tail
->name
== *group
&& !strcmp (tail
->name
, group
))
193 grent
= getgrnam (group
);
195 tail
= (struct userid
*) xmalloc (sizeof (struct userid
));
196 tail
->name
= xstrdup (group
);
198 /* Add to the head of the list, so most recently used is first. */
201 tail
->id
.g
= grent
->gr_gid
;
202 tail
->next
= group_alist
;
207 tail
->next
= nogroup_alist
;
208 nogroup_alist
= tail
;