1: /*
2: * Modified version of getpwuid which doesn't use stdio.
3: * This is done to keep it (and hence csh) small at a small
4: * cost in speed.
5: */
6: #include <pwd.h>
7: #include <whoami.h>
8:
9: #ifdef UCB_PWHASH
10: #include <sys/pwdtbl.h>
11: #include <sys/stat.h>
12: #define pwf _pw_file
13:
14: static char UTBL[] = "/etc/uid_pw_map"; /* name of user id table */
15: extern int pwf; /* pointer to password file */
16: static int uf = -1; /* user id table */
17: static int nusers; /* number of users */
18: static off_t uidloc(); /* finds uid from table */
19: struct stat sbuf; /* stat buffer */
20: #endif
21:
22: struct passwd *
23: getpwuid(uid)
24: register uid;
25: {
26: register struct passwd *p;
27: struct passwd *getpwent();
28:
29: #ifdef UCB_PWHASH
30: off_t loc;
31: #endif
32:
33: #ifdef UCB_PWHASH
34: if (pwf == -1)
35: setpwent();
36:
37: loc = uidloc (uid);
38: if (loc >= 0) {
39: lseek (pwf,loc,0);
40: p = getpwent();
41: if (p->pw_uid == uid)
42: return (p);
43: }
44: #endif
45:
46: /* begin linear search */
47: setpwent();
48: while( (p = getpwent()) && p->pw_uid != uid );
49: endpwent();
50: return(p);
51: }
52:
53: #ifdef UCB_PWHASH
54: static off_t
55: uidloc (uid) /* finds uid from uid->loc table */
56: register unsigned uid;
57: {
58: off_t high; /* high pointer to file */
59: off_t low; /* low pointer to file */
60: off_t test; /* current pointer to file */
61: struct pwloc p; /* table entry from file */
62:
63: /* get status of uid table file
64: * and open it */
65: if (uf == -1) {
66: if (stat (UTBL,&sbuf) || (uf = open (UTBL, 0)) == -1)
67: return ((off_t) -1);
68:
69: /* compute number of entries */
70: nusers = sbuf.st_size/sizeof(struct pwloc);
71: }
72:
73: low = 0; /* initialize pointers */
74: high = nusers;
75:
76: compare:
77: test = (high+low+1)/2; /* find midpoint */
78: if (test == high) /* no valid midpoint */
79: return ((off_t) -1);
80:
81: /* read table entry */
82: lseek (uf, test*sizeof (struct pwloc), 0);
83: if (read(uf, &p, sizeof (struct pwloc)) != sizeof (struct pwloc))
84: return ((off_t) -1);
85: if (p.pwl_uid == uid) /* return if location found */
86: return (p.pwl_loc);
87:
88: if (p.pwl_uid < uid) /* adjust pointers */
89: low = test;
90: else
91: high = test;
92: goto compare;
93: }
94: #endif
Defined functions
Defined variables
UTBL
defined in line
14; used 2 times
sbuf
defined in line
19; used 2 times
uf
defined in line
16; used 4 times
Defined macros
pwf
defined in line
12; used 2 times