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>
39 #include "top.local.h"
44 char name
[UT_NAMESIZE
+ 1];
47 #define is_empty_hash(x) (hash_table[x].name[0] == 0)
49 /* simple minded hashing function */
50 /* Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for
51 the hash_table. Applied abs() function to fix. 2/16/96 tpugh
53 #define hashit(i) (abs(i) % Table_size)
55 /* K&R requires that statically declared tables be initialized to zero. */
56 /* We depend on that for hash_table and YOUR compiler had BETTER do it! */
57 struct hash_el hash_table
[Table_size
];
63 * There used to be some steps we had to take to initialize things.
64 * We don't need to do that anymore, but we will leave this stub in
65 * just in case future changes require initialization steps.
74 register int hashindex
;
76 hashindex
= hashit(uid
);
77 if (is_empty_hash(hashindex
) || (hash_table
[hashindex
].uid
!= uid
))
79 /* not here or not right -- get it out of passwd */
80 hashindex
= get_user(uid
);
82 return(hash_table
[hashindex
].name
);
92 /* Eventually we want this to enter everything in the hash table,
93 but for now we just do it simply and remember just the result.
96 if ((pwd
= getpwnam(username
)) == NULL
)
101 /* enter the result in the hash table */
102 enter_user(pwd
->pw_uid
, username
, 1);
104 /* return our result */
108 int enter_user(uid
, name
, wecare
)
112 int wecare
; /* 1 = enter it always, 0 = nice to have */
115 register int hashindex
;
118 fprintf(stderr
, "enter_hash(%d, %s, %d)\n", uid
, name
, wecare
);
121 hashindex
= hashit(uid
);
123 if (!is_empty_hash(hashindex
))
126 return 0; /* Don't clobber a slot for trash */
127 if (hash_table
[hashindex
].uid
== uid
)
128 return(hashindex
); /* Fortuitous find */
131 /* empty or wrong slot -- fill it with new value */
132 hash_table
[hashindex
].uid
= uid
;
133 (void) strncpy(hash_table
[hashindex
].name
, name
, UT_NAMESIZE
);
138 * Get a userid->name mapping from the system.
139 * If the passwd database is hashed (#define RANDOM_PW), we
140 * just handle this uid. Otherwise we scan the passwd file
141 * and cache any entries we pass over while looking.
152 /* no performance penalty for using getpwuid makes it easy */
153 if ((pwd
= getpwuid(uid
)) != NULL
)
155 return(enter_user(pwd
->pw_uid
, pwd
->pw_name
, 1));
162 * If we just called getpwuid each time, things would be very slow
163 * since that just iterates through the passwd file each time. So,
164 * we walk through the file instead (using getpwent) and cache each
165 * entry as we go. Once the right record is found, we cache it and
166 * return immediately. The next time we come in, getpwent will get
167 * the next record. In theory, we never have to read the passwd file
168 * a second time (because we cache everything we read). But in
169 * practice, the cache may not be large enough, so if we don't find
170 * it the first time we have to scan the file a second time. This
171 * is not very efficient, but it will do for now.
174 while (from_start
++ < 2)
176 while ((pwd
= getpwent()) != NULL
)
178 if (pwd
->pw_uid
== uid
)
180 return(enter_user(pwd
->pw_uid
, pwd
->pw_name
, 1));
182 (void) enter_user(pwd
->pw_uid
, pwd
->pw_name
, 0);
188 /* if we can't find the name at all, then use the uid as the name */
189 return(enter_user(uid
, itoa7(uid
), 1));