|
|
1.1 root 1: #include "asd.h"
2: #include <pwd.h>
3:
4: #define CHUNK 16
5:
6: struct passwd *getpwent();
7:
8: static struct utab {
9: unsigned uid;
10: char *name;
11: } *utab;
12:
13: static int size;
14:
15: char *
16: struid (uid)
17: register unsigned short uid;
18: {
19: register int i;
20: static char buf[12];
21:
22: /* search the cache for the uid */
23: for (i = 0; i < size; i++)
24: if (utab[i].uid == uid)
25: return utab[i].name;
26:
27: /* search the file, caching */
28: while (expand())
29: if (utab[size-1].uid == uid)
30: return utab[size-1].name;
31:
32: /* failure, invent a string */
33: sprintf (buf, "#%u", uid);
34: return buf;
35: }
36:
37: int
38: numuid (name)
39: register char *name;
40: {
41: register int i;
42:
43: /* if it starts with a #, use the number */
44: if (name[0] == '#')
45: return atoi (name + 1);
46:
47: /* search the cache */
48: for (i = 0; i < size; i++)
49: if (strcmp (utab[i].name, name) == 0)
50: return utab[i].uid;
51:
52: /* search the file */
53: while (expand())
54: if (strcmp (utab[size-1].name, name) == 0)
55: return utab[size-1].uid;
56:
57: /* failure, invent a value */
58: return getuid();
59: }
60:
61: static int
62: expand()
63: {
64: register struct passwd *pw;
65: static ended;
66:
67: if (!ended) {
68: pw = getpwent();
69: if (pw == NULL) {
70: ended = 1;
71: endpwent();
72: return 0;
73: }
74: if (size % CHUNK == 0) {
75: utab = (struct utab *) ralloc ((char *) utab,
76: (unsigned) ((size + CHUNK) * sizeof (*utab)));
77: }
78: utab[size].uid = pw->pw_uid;
79: utab[size].name = copy (pw->pw_name);
80: size++;
81: return 1;
82: }
83:
84: return 0;
85: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.