|
|
1.1 root 1: #include "asd.h"
2: #include <grp.h>
3: #include <stdio.h>
4: #include <stdlib.h>
5: #include <string.h>
6: #include <unistd.h>
7:
8: #define CHUNK 16
9:
10: static struct gtab {
11: unsigned gid;
12: char *name;
13: } *gtab;
14:
15: static int size, salloc;
16:
17: char *
18: gidstr (gid_t gid)
19: {
20: register int i;
21: static char buf[12];
22: struct group *g;
23:
24: /* search the cache for the gid */
25: for (i = 0; i < size; i++)
26: if (gtab[i].gid == gid)
27: return gtab[i].name;
28:
29: /* try to find it in the system's database */
30: if (g = getgrgid(gid)) {
31: if (size % CHUNK == 0)
32: gtab = (struct gtab *) ralloc((char *) gtab, salloc += CHUNK);
33: gtab[size].gid = g->gr_gid;
34: gtab[size].name = copy(g->gr_name);
35: ++size;
36: return gtab[size - 1].name;
37: }
38:
39: /* failure, invent a string */
40: sprintf (buf, "#%u", gid);
41: return buf;
42: }
43:
44: gid_t
45: gidnum (char *name)
46: {
47: register int i;
48: struct group *g;
49:
50: /* if it starts with a #, use the number */
51: if (name[0] == '#')
52: return atoi (name + 1);
53:
54: /* search the cache */
55: for (i = 0; i < size; i++)
56: if (strcmp (gtab[i].name, name) == 0)
57: return gtab[i].gid;
58:
59: /* try to find it in the system's database */
60: if (g = getgrnam(name)) {
61: if (size % CHUNK == 0)
62: gtab = (struct gtab *) ralloc((char *) gtab, salloc += CHUNK);
63: gtab[size].gid = g->gr_gid;
64: gtab[size].name = copy(g->gr_name);
65: ++size;
66: return gtab[size - 1].gid;
67: }
68:
69:
70: /* failure, invent a value */
71: return getgid();
72: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.