This source file includes following definitions.
- getuser
- getuidbyname
- getgroup
- getgidbyname
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21 #include "idcache.h"
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <pwd.h>
26 #include <grp.h>
27
28 #include <unistd.h>
29
30 #include "flexmember.h"
31 #include "xalloc.h"
32
33 #ifdef __DJGPP__
34 static char digits[] = "0123456789";
35 #endif
36
37 struct userid
38 {
39 union
40 {
41 uid_t u;
42 gid_t g;
43 } id;
44 struct userid *next;
45 char name[FLEXIBLE_ARRAY_MEMBER];
46 };
47
48
49
50
51
52
53
54
55 static struct userid *user_alist;
56
57
58 static struct userid *nouser_alist;
59
60
61 static struct userid *group_alist;
62
63
64 static struct userid *nogroup_alist;
65
66
67
68 char *
69 getuser (uid_t uid)
70 {
71 struct userid *tail;
72 struct userid *match = NULL;
73
74 for (tail = user_alist; tail; tail = tail->next)
75 {
76 if (tail->id.u == uid)
77 {
78 match = tail;
79 break;
80 }
81 }
82
83 if (match == NULL)
84 {
85 struct passwd *pwent = getpwuid (uid);
86 char const *name = pwent ? pwent->pw_name : "";
87 match = xmalloc (FLEXSIZEOF (struct userid, name, strlen (name) + 1));
88 match->id.u = uid;
89 strcpy (match->name, name);
90
91
92 match->next = user_alist;
93 user_alist = match;
94 }
95
96 return match->name[0] ? match->name : NULL;
97 }
98
99
100
101
102
103
104 uid_t *
105 getuidbyname (const char *user)
106 {
107 struct userid *tail;
108 struct passwd *pwent;
109
110 for (tail = user_alist; tail; tail = tail->next)
111
112 if (*tail->name == *user && !strcmp (tail->name, user))
113 return &tail->id.u;
114
115 for (tail = nouser_alist; tail; tail = tail->next)
116
117 if (*tail->name == *user && !strcmp (tail->name, user))
118 return NULL;
119
120 pwent = getpwnam (user);
121 #ifdef __DJGPP__
122
123
124 if (!pwent && strspn (user, digits) < strlen (user))
125 {
126 setenv ("USER", user, 1);
127 pwent = getpwnam (user);
128 }
129 #endif
130
131 tail = xmalloc (FLEXSIZEOF (struct userid, name, strlen (user) + 1));
132 strcpy (tail->name, user);
133
134
135 if (pwent)
136 {
137 tail->id.u = pwent->pw_uid;
138 tail->next = user_alist;
139 user_alist = tail;
140 return &tail->id.u;
141 }
142
143 tail->next = nouser_alist;
144 nouser_alist = tail;
145 return NULL;
146 }
147
148
149
150 char *
151 getgroup (gid_t gid)
152 {
153 struct userid *tail;
154 struct userid *match = NULL;
155
156 for (tail = group_alist; tail; tail = tail->next)
157 {
158 if (tail->id.g == gid)
159 {
160 match = tail;
161 break;
162 }
163 }
164
165 if (match == NULL)
166 {
167 struct group *grent = getgrgid (gid);
168 char const *name = grent ? grent->gr_name : "";
169 match = xmalloc (FLEXSIZEOF (struct userid, name, strlen (name) + 1));
170 match->id.g = gid;
171 strcpy (match->name, name);
172
173
174 match->next = group_alist;
175 group_alist = match;
176 }
177
178 return match->name[0] ? match->name : NULL;
179 }
180
181
182
183
184
185
186 gid_t *
187 getgidbyname (const char *group)
188 {
189 struct userid *tail;
190 struct group *grent;
191
192 for (tail = group_alist; tail; tail = tail->next)
193
194 if (*tail->name == *group && !strcmp (tail->name, group))
195 return &tail->id.g;
196
197 for (tail = nogroup_alist; tail; tail = tail->next)
198
199 if (*tail->name == *group && !strcmp (tail->name, group))
200 return NULL;
201
202 grent = getgrnam (group);
203 #ifdef __DJGPP__
204
205
206 if (!grent && strspn (group, digits) < strlen (group))
207 {
208 setenv ("GROUP", group, 1);
209 grent = getgrnam (group);
210 }
211 #endif
212
213 tail = xmalloc (FLEXSIZEOF (struct userid, name, strlen (group) + 1));
214 strcpy (tail->name, group);
215
216
217 if (grent)
218 {
219 tail->id.g = grent->gr_gid;
220 tail->next = group_alist;
221 group_alist = tail;
222 return &tail->id.g;
223 }
224
225 tail->next = nogroup_alist;
226 nogroup_alist = tail;
227 return NULL;
228 }