Annotation of src/usr.bin/top/username.c, Revision 1.2
1.2 ! downsj 1: /* $OpenBSD: username.c,v 1.1 1997/08/14 14:00:27 downsj Exp $ */
1.1 downsj 2:
3: /*
4: * Top users/processes display for Unix
5: * Version 3
6: *
7: * This program may be freely redistributed,
8: * but this entire comment MUST remain intact.
9: *
10: * Copyright (c) 1984, 1989, William LeFebvre, Rice University
11: * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
12: */
13:
14: /*
15: * Username translation code for top.
16: *
17: * These routines handle uid to username mapping.
18: * They use a hashing table scheme to reduce reading overhead.
19: * For the time being, these are very straightforward hashing routines.
20: * Maybe someday I'll put in something better. But with the advent of
21: * "random access" password files, it might not be worth the effort.
22: *
23: * Changes to these have been provided by John Gilmore (gnu@toad.com).
24: *
25: * The hash has been simplified in this release, to avoid the
26: * table overflow problems of previous releases. If the value
27: * at the initial hash location is not right, it is replaced
28: * by the right value. Collisions will cause us to call getpw*
29: * but hey, this is a cache, not the Library of Congress.
30: * This makes the table size independent of the passwd file size.
31: */
32:
1.2 ! downsj 33: #include <sys/types.h>
1.1 downsj 34: #include <stdio.h>
1.2 ! downsj 35: #include <string.h>
1.1 downsj 36: #include <pwd.h>
37:
38: #include "top.local.h"
39: #include "utils.h"
40:
41: struct hash_el {
1.2 ! downsj 42: uid_t uid;
1.1 downsj 43: char name[9];
44: };
45:
1.2 ! downsj 46: static int enter_user __P((uid_t, char *, int));
! 47: static int get_user __P((uid_t));
! 48:
1.1 downsj 49: #define is_empty_hash(x) (hash_table[x].name[0] == 0)
50:
51: /* simple minded hashing function */
52: /* Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for
53: the hash_table. Applied abs() function to fix. 2/16/96 tpugh
54: */
55: #define hashit(i) (abs(i) % Table_size)
56:
57: /* K&R requires that statically declared tables be initialized to zero. */
58: /* We depend on that for hash_table and YOUR compiler had BETTER do it! */
59: struct hash_el hash_table[Table_size];
60:
1.2 ! downsj 61: void init_hash()
1.1 downsj 62:
63: {
64: /*
65: * There used to be some steps we had to take to initialize things.
66: * We don't need to do that anymore, but we will leave this stub in
67: * just in case future changes require initialization steps.
68: */
69: }
70:
71: char *username(uid)
72:
1.2 ! downsj 73: register uid_t uid;
1.1 downsj 74:
75: {
76: register int hashindex;
77:
78: hashindex = hashit(uid);
79: if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid))
80: {
81: /* not here or not right -- get it out of passwd */
82: hashindex = get_user(uid);
83: }
84: return(hash_table[hashindex].name);
85: }
86:
1.2 ! downsj 87: uid_t userid(username)
1.1 downsj 88:
89: char *username;
90:
91: {
92: struct passwd *pwd;
93:
94: /* Eventually we want this to enter everything in the hash table,
95: but for now we just do it simply and remember just the result.
96: */
97:
98: if ((pwd = getpwnam(username)) == NULL)
99: {
100: return(-1);
101: }
102:
103: /* enter the result in the hash table */
104: enter_user(pwd->pw_uid, username, 1);
105:
106: /* return our result */
107: return(pwd->pw_uid);
108: }
109:
1.2 ! downsj 110: static int enter_user(uid, name, wecare)
1.1 downsj 111:
1.2 ! downsj 112: register uid_t uid;
1.1 downsj 113: register char *name;
114: int wecare; /* 1 = enter it always, 0 = nice to have */
115:
116: {
117: register int hashindex;
118:
119: #ifdef DEBUG
120: fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare);
121: #endif
122:
123: hashindex = hashit(uid);
124:
125: if (!is_empty_hash(hashindex))
126: {
127: if (!wecare)
128: return 0; /* Don't clobber a slot for trash */
129: if (hash_table[hashindex].uid == uid)
130: return(hashindex); /* Fortuitous find */
131: }
132:
133: /* empty or wrong slot -- fill it with new value */
134: hash_table[hashindex].uid = uid;
135: (void) strncpy(hash_table[hashindex].name, name, 8);
136: return(hashindex);
137: }
138:
139: /*
140: * Get a userid->name mapping from the system.
141: * If the passwd database is hashed (#define RANDOM_PW), we
142: * just handle this uid. Otherwise we scan the passwd file
143: * and cache any entries we pass over while looking.
144: */
145:
1.2 ! downsj 146: static int get_user(uid)
1.1 downsj 147:
1.2 ! downsj 148: register uid_t uid;
1.1 downsj 149:
150: {
151: struct passwd *pwd;
152:
153: #ifdef RANDOM_PW
154: /* no performance penalty for using getpwuid makes it easy */
155: if ((pwd = getpwuid(uid)) != NULL)
156: {
157: return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
158: }
159: #else
160:
161: int from_start = 0;
162:
163: /*
164: * If we just called getpwuid each time, things would be very slow
165: * since that just iterates through the passwd file each time. So,
166: * we walk through the file instead (using getpwent) and cache each
167: * entry as we go. Once the right record is found, we cache it and
168: * return immediately. The next time we come in, getpwent will get
169: * the next record. In theory, we never have to read the passwd file
170: * a second time (because we cache everything we read). But in
171: * practice, the cache may not be large enough, so if we don't find
172: * it the first time we have to scan the file a second time. This
173: * is not very efficient, but it will do for now.
174: */
175:
176: while (from_start++ < 2)
177: {
178: while ((pwd = getpwent()) != NULL)
179: {
180: if (pwd->pw_uid == uid)
181: {
182: return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
183: }
184: (void) enter_user(pwd->pw_uid, pwd->pw_name, 0);
185: }
186: /* try again */
187: setpwent();
188: }
189: #endif
190: /* if we can't find the name at all, then use the uid as the name */
191: return(enter_user(uid, itoa7(uid), 1));
192: }