1: /* @(#)pwtable.c 2.1 SCCS id keyword */
2: #include <whoami.h>
3: #include <stdio.h>
4: #include <sys/types.h>
5: #include <pwtable.h>
6: #define ENTRIES 1500 /* maximum number of entries */
7: #define MAXLINE BUFSIZ /* maximum line length */
8: #define NAMSIZ 8
9:
10: static char PASSWD[] = "/etc/passwd"; /* password file */
11:
12: #ifdef UCB_PWHASH
13: static char TBL[] = UCB_PWHASH; /* pwtable file */
14: #else
15: static char TBL[] = "/etc/pw_map";
16: #endif
17:
18: static unsigned filltable();
19: static int uidcmp(), namecmp();
20:
21: /*
22: * make tables that map user id to location in password file and
23: * user name to location in password file
24: */
25: pwtable() {
26: register unsigned nusers;
27:
28: struct pwtable table[ENTRIES];
29:
30: nusers = filltable(table);
31: writetable(table, nusers);
32: }
33:
34: static unsigned
35: filltable(table)
36: struct pwtable *table;
37: {
38: register struct pwtable *tp;
39:
40: char line[BUFSIZ];
41: char *t, *s;
42: unsigned u;
43: FILE *pwf;
44: struct pwtable *ep;
45: off_t loc = 0L;
46:
47: if ((pwf = fopen(PASSWD, "r")) == NULL) {
48: perror(PASSWD);
49: return(0);
50: }
51: ep = table + ENTRIES;
52: for (tp = table; tp < ep; tp++) {
53: tp->pwt_loc = loc;
54: if (fgets(line, BUFSIZ, pwf) == NULL)
55: break;
56: loc += strlen(line);
57: t = tp->pwt_name;
58: for (s = line; *s && *s != ':'; s++) {
59: if (t >= tp->pwt_name + NAMSIZ)
60: break;
61: *t++ = *s;
62: }
63: while (t < tp->pwt_name + NAMSIZ)
64: *t++ = '\0';
65: while (*++s != ':')
66: ;
67: u = 0;
68: while (*++s != ':')
69: u = 10 * u + (*s) - '0';
70: tp->pwt_uid = u;
71: }
72: fclose(pwf);
73: return(tp - table);
74: }
75:
76: static
77: writetable(table, nusers)
78: struct pwtable *table;
79: unsigned nusers;
80: {
81: register struct pwtable *tp;
82: register struct pwtable *bp;
83: struct pwtable *ep;
84: FILE *tbf;
85:
86: if ((tbf = fopen(TBL, "w")) == NULL) {
87: perror(TBL);
88: return;
89: }
90: ep = table + nusers;
91:
92: qsort(table, nusers, sizeof(struct pwtable), uidcmp);
93: for (tp = table; tp < ep; tp++) {
94: for (bp = tp - 1; bp >= table; bp--)
95: if (bp->pwt_uid != tp->pwt_uid)
96: break;
97: fwrite((char *) (bp + 1), sizeof(struct pwtable), 1, tbf);
98: }
99:
100: qsort(table, nusers, sizeof(struct pwtable), namecmp);
101: for (tp = table; tp < ep; tp++) {
102: for (bp = tp - 1; bp >= table; bp--)
103: if (strncmp(bp->pwt_name, tp->pwt_name, NAMSIZ))
104: break;
105: fwrite((char *) (bp + 1), sizeof(struct pwtable), 1, tbf);
106: }
107: fclose(tbf);
108: }
109:
110: static
111: uidcmp(a, b)
112: struct pwtable *a, *b;
113: {
114: if (a->pwt_uid < b->pwt_uid)
115: return(-1);
116: if (a->pwt_uid > b->pwt_uid)
117: return(1);
118: if (a->pwt_loc < b->pwt_loc)
119: return(-1);
120: return(1);
121: }
122:
123: static
124: namecmp(a, b)
125: struct pwtable *a, *b;
126: {
127: register differ;
128: if (differ = strncmp(a->pwt_name, b->pwt_name, NAMSIZ))
129: return(differ);
130: if (a->pwt_loc < b->pwt_loc)
131: return(-1);
132: return(1);
133: }
Defined functions
Defined variables
TBL
defined in line
15; used 2 times
Defined macros
NAMSIZ
defined in line
8; used 4 times