2 * Top users/processes display for Unix
5 * This program may be freely redistributed,
6 * but this entire comment MUST remain intact.
8 * Copyright (c) 1984, 1989, William LeFebvre, Rice University
9 * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
11 * $FreeBSD: src/contrib/top/username.c,v 1.2.8.1 2002/08/11 17:09:25 dwmalone Exp $
12 * $DragonFly: src/contrib/top/username.c,v 1.2 2003/06/17 04:24:07 dillon Exp $
16 * Username translation code for top.
18 * These routines handle uid to username mapping.
19 * They use a hashing table scheme to reduce reading overhead.
20 * For the time being, these are very straightforward hashing routines.
21 * Maybe someday I'll put in something better. But with the advent of
22 * "random access" password files, it might not be worth the effort.
24 * Changes to these have been provided by John Gilmore (gnu@toad.com).
26 * The hash has been simplified in this release, to avoid the
27 * table overflow problems of previous releases. If the value
28 * at the initial hash location is not right, it is replaced
29 * by the right value. Collisions will cause us to call getpw*
30 * but hey, this is a cache, not the Library of Congress.
31 * This makes the table size independent of the passwd file size.
34 #include <sys/types.h>
41 #include "top.local.h"
47 char name[UT_NAMESIZE + 1];
50 static int get_user(int uid);
52 #define is_empty_hash(x) (hash_table[x].name[0] == 0)
54 /* simple minded hashing function */
55 /* Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for
56 the hash_table. Applied abs() function to fix. 2/16/96 tpugh
58 #define hashit(i) (abs(i) % Table_size)
60 /* K&R requires that statically declared tables be initialized to zero. */
61 /* We depend on that for hash_table and YOUR compiler had BETTER do it! */
62 struct hash_el hash_table[Table_size];
68 * There used to be some steps we had to take to initialize things.
69 * We don't need to do that anymore, but we will leave this stub in
70 * just in case future changes require initialization steps.
79 hashindex = hashit(uid);
80 if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid))
82 /* not here or not right -- get it out of passwd */
83 hashindex = get_user(uid);
85 return(hash_table[hashindex].name);
89 userid(char *xusername)
93 /* Eventually we want this to enter everything in the hash table,
94 but for now we just do it simply and remember just the result.
97 if ((pwd = getpwnam(xusername)) == NULL)
102 /* enter the result in the hash table */
103 enter_user(pwd->pw_uid, xusername, 1);
105 /* return our result */
110 enter_user(int uid, char *name, int wecare)
115 fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare);
118 hashindex = hashit(uid);
120 if (!is_empty_hash(hashindex))
123 return 0; /* Don't clobber a slot for trash */
124 if (hash_table[hashindex].uid == uid)
125 return(hashindex); /* Fortuitous find */
128 /* empty or wrong slot -- fill it with new value */
129 hash_table[hashindex].uid = uid;
130 (void) strncpy(hash_table[hashindex].name, name, UT_NAMESIZE);
135 * Get a userid->name mapping from the system.
136 * If the passwd database is hashed (#define RANDOM_PW), we
137 * just handle this uid. Otherwise we scan the passwd file
138 * and cache any entries we pass over while looking.
147 /* no performance penalty for using getpwuid makes it easy */
148 if ((pwd = getpwuid(uid)) != NULL)
150 return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
157 * If we just called getpwuid each time, things would be very slow
158 * since that just iterates through the passwd file each time. So,
159 * we walk through the file instead (using getpwent) and cache each
160 * entry as we go. Once the right record is found, we cache it and
161 * return immediately. The next time we come in, getpwent will get
162 * the next record. In theory, we never have to read the passwd file
163 * a second time (because we cache everything we read). But in
164 * practice, the cache may not be large enough, so if we don't find
165 * it the first time we have to scan the file a second time. This
166 * is not very efficient, but it will do for now.
169 while (from_start++ < 2)
171 while ((pwd = getpwent()) != NULL)
173 if (pwd->pw_uid == uid)
175 return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
177 (void) enter_user(pwd->pw_uid, pwd->pw_name, 0);
183 /* if we can't find the name at all, then use the uid as the name */
184 return(enter_user(uid, ltoa7(uid), 1));