|
|
1.1 root 1: #define _POSIX_SOURCE
2: #include <limits.h>
3: #include <stdio.h>
4: #include <time.h>
5: #include <pwd.h>
6: #include <grp.h>
7: #include "lib.h"
8:
9: struct idmap {
10: char *str;
11: int num;
12: struct idmap *next;
13: };
14:
15: static struct idmap *umap, *gmap;
16:
17: static struct idmap *
18: idnum(struct idmap **map, char *str)
19: {
20: while (*map) {
21: if ((*map)->str && strcmp((*map)->str, str) == 0)
22: return *map;
23: map = &(*map)->next;
24: }
25: return 0;
26: }
27:
28: static struct idmap *
29: idstr(struct idmap **map, int num)
30: {
31: while (*map) {
32: if ((*map)->num == num)
33: return *map;
34: map = &(*map)->next;
35: }
36: return 0;
37: }
38:
39: static struct idmap *
40: idset(struct idmap **map, char *str, int num)
41: {
42: struct idmap *mapent;
43:
44: mapent = xmalloc(sizeof (struct idmap));
45: if (str)
46: mapent->str = xstrdup(str);
47: else
48: mapent->str = 0;
49: mapent->num = num;
50: mapent->next = *map;
51: *map = mapent;
52: return mapent;
53: }
54:
55: int
56: uidnum(char *str)
57: {
58: struct idmap *id;
59: struct passwd *pw;
60: int num;
61:
62: id = idnum(&umap, str);
63: if (!id) {
64: pw = getpwnam(str);
65: id = idset(&umap, str, pw ? pw->pw_uid : -1);
66: }
67: return id->num;
68: }
69:
70: char *
71: uidstr(int num)
72: {
73: struct idmap *id;
74: struct passwd *pw;
75: char *str;
76:
77: id = idstr(&umap, num);
78: if (!id) {
79: pw = getpwuid(num);
80: id = idset(&umap, pw ? pw->pw_name : 0, num);
81: }
82: return id->str;
83: }
84:
85: int
86: gidnum(char *str)
87: {
88: struct idmap *id;
89: struct group *gr;
90: int num;
91:
92: id = idnum(&gmap, str);
93: if (!id) {
94: gr = getgrnam(str);
95: id = idset(&gmap, str, gr ? gr->gr_gid : -1);
96: }
97: return id->num;
98: }
99:
100: char *
101: gidstr(int num)
102: {
103: struct idmap *id;
104: struct group *gr;
105: char *str;
106:
107: id = idstr(&gmap, num);
108: if (!id) {
109: gr = getgrgid(num);
110: id = idset(&gmap, gr ? gr->gr_name : 0, num);
111: }
112: return id->str;
113: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.