1: /* @(#)getmap.c 2.1 SCCS id keyword */
2: #include <whoami.h>
3: #include <sys/types.h>
4: #include <pwtable.h>
5: #define NAMSIZ 8
6:
7: static int tbf = -1; /* table file */
8: static char TBL[] = UCB_PWHASH; /* name of map table */
9: static unsigned nusers; /* number of users */
10:
11: long lseek();
12:
13: static struct pwtable mapentry; /* table entry from file */
14:
15: static
16: setmapent()
17: {
18: if (tbf == -1) {
19: if ((tbf = open (TBL, 0)) < 0)
20: return (0);
21: /* compute number of entries */
22: nusers = lseek(tbf, 0L, 2) / (2 * sizeof(struct pwtable));
23: }
24: return(1);
25: }
26:
27: endmapent()
28: {
29: if (tbf >= 0) {
30: close(tbf);
31: tbf = -1;
32: }
33: }
34:
35: struct pwtable *
36: getmapuid(uid)
37: register uid;
38: {
39: unsigned high; /* high offset to file */
40: unsigned low; /* low offset to file */
41: unsigned test; /* current offset to file */
42:
43: if (!setmapent())
44: return(0);
45: low = 0; /* initialize pointers */
46: high = nusers;
47:
48: do {
49: test = (low + high) / 2; /* find midpoint */
50: lseek (tbf, (long) test * sizeof (struct pwtable), 0);
51: if (read(tbf, &mapentry, sizeof (struct pwtable)) !=
52: sizeof (struct pwtable))
53: return (0);
54: if (mapentry.pwt_uid == uid)
55: return (&mapentry);
56: if (mapentry.pwt_uid < uid)
57: low = test + 1;
58: else
59: high = test;
60: } while (low != high);
61: return(0);
62: }
63:
64: struct pwtable *
65: getmapnam(name)
66: char *name;
67: {
68: unsigned high; /* high offset to file */
69: unsigned low; /* low offset to file */
70: unsigned test; /* current offset to file */
71: int differ;
72:
73: if (!setmapent())
74: return(0);
75: low = 0; /* initialize pointer */
76: high = nusers;
77:
78: do {
79: test = (low + high) / 2; /* find midpoint */
80: lseek (tbf, ((long) test + nusers) * sizeof (struct pwtable), 0);
81: if (read(tbf, &mapentry, sizeof (struct pwtable)) !=
82: sizeof (struct pwtable))
83: return (0);
84:
85: if ((differ = strncmp (mapentry.pwt_name,name,NAMSIZ)) == 0)
86: return (&mapentry);
87:
88: if (differ < 0) /* adjust pointers */
89: low = test + 1;
90: else
91: high = test;
92: } while (low != high);
93: return(0);
94: }
Defined functions
Defined variables
TBL
defined in line
8; used 1 times
nusers
defined in line
9; used 4 times
tbf
defined in line
7; used 10 times
Defined macros
NAMSIZ
defined in line
5; used 1 times