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. */
29 static struct userid *user_alist = NULL;
30 /* Use the same struct as for userids. */
31 static struct userid *group_alist = NULL;
33 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
35 /* Translate UID to a login name or a stringified number,
38 char *getuser(uid_t uid)
40 register struct userid *tail;
42 char usernum_string[20];
45 for (tail = user_alist; tail; tail = tail->next) {
46 if (tail->id.u == uid) {
52 pwent = getpwuid(uid);
53 tail = (struct userid *)malloc(sizeof (struct userid));
55 if (pwent == 0 || strcmp(pwent->pw_name, "????????") == 0) {
56 sprintf(usernum_string, "%u", (uint32_t)uid);
57 tail->name = bstrdup(usernum_string);
59 tail->name = bstrdup(pwent->pw_name);
62 /* Add to the head of the list, so most recently used is first. */
63 tail->next = user_alist;
69 void free_getuser_cache()
71 register struct userid *tail;
74 for (tail = user_alist; tail; ) {
75 struct userid *otail = tail;
86 /* Translate GID to a group name or a stringified number,
88 char *getgroup(gid_t gid)
90 register struct userid *tail;
92 char groupnum_string[20];
95 for (tail = group_alist; tail; tail = tail->next) {
96 if (tail->id.g == gid) {
102 grent = getgrgid(gid);
103 tail = (struct userid *)malloc(sizeof (struct userid));
105 if (grent == 0 || strcmp(grent->gr_name, "????????") == 0) {
106 sprintf (groupnum_string, "%u", (uint32_t)gid);
107 tail->name = bstrdup(groupnum_string);
109 tail->name = bstrdup(grent->gr_name);
112 /* Add to the head of the list, so most recently used is first. */
113 tail->next = group_alist;
119 void free_getgroup_cache()
121 register struct userid *tail;
124 for (tail = group_alist; tail; ) {
125 struct userid *otail = tail;