xref: /freebsd/usr.bin/top/username.c (revision 4928135658a9d0eaee37003df6137ab363fcb0b4)
1 /*
2  *  Top users/processes display for Unix
3  *
4  *  This program may be freely redistributed,
5  *  but this entire comment MUST remain intact.
6  *
7  *  Copyright (c) 1984, 1989, William LeFebvre, Rice University
8  *  Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
9  *
10  * $FreeBSD$
11  */
12 
13 /*
14  *  Username translation code for top.
15  *
16  *  These routines handle uid to username mapping.
17  *  They use a hashing table scheme to reduce reading overhead.
18  *  For the time being, these are very straightforward hashing routines.
19  *  Maybe someday I'll put in something better.  But with the advent of
20  *  "random access" password files, it might not be worth the effort.
21  *
22  *  Changes to these have been provided by John Gilmore (gnu@toad.com).
23  *
24  *  The hash has been simplified in this release, to avoid the
25  *  table overflow problems of previous releases.  If the value
26  *  at the initial hash location is not right, it is replaced
27  *  by the right value.  Collisions will cause us to call getpw*
28  *  but hey, this is a cache, not the Library of Congress.
29  *  This makes the table size independent of the passwd file size.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 
35 #include <pwd.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "utils.h"
41 #include "username.h"
42 
43 struct hash_el {
44     int  uid;
45     char name[MAXLOGNAME];
46 };
47 
48 #define    is_empty_hash(x)	(hash_table[x].name[0] == 0)
49 
50 /* simple minded hashing function */
51 /* Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for
52    the hash_table.  Applied abs() function to fix. 2/16/96 tpugh
53 */
54 #define    hashit(i)	(abs(i) % Table_size)
55 
56 /* K&R requires that statically declared tables be initialized to zero. */
57 /* We depend on that for hash_table and YOUR compiler had BETTER do it! */
58 static struct hash_el hash_table[Table_size];
59 
60 
61 char *username(uid)
62 
63 int uid;
64 
65 {
66     int hashindex;
67 
68     hashindex = hashit(uid);
69     if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid))
70     {
71 	/* not here or not right -- get it out of passwd */
72 	hashindex = get_user(uid);
73     }
74     return(hash_table[hashindex].name);
75 }
76 
77 int userid(username)
78 
79 char *username;
80 
81 {
82     struct passwd *pwd;
83 
84     /* Eventually we want this to enter everything in the hash table,
85        but for now we just do it simply and remember just the result.
86      */
87 
88     if ((pwd = getpwnam(username)) == NULL)
89     {
90 	return(-1);
91     }
92 
93     /* enter the result in the hash table */
94     enter_user(pwd->pw_uid, username, 1);
95 
96     /* return our result */
97     return(pwd->pw_uid);
98 }
99 
100 int enter_user(uid, name, wecare)
101 
102 int  uid;
103 char *name;
104 int wecare;		/* 1 = enter it always, 0 = nice to have */
105 
106 {
107     int hashindex;
108 
109 #ifdef DEBUG
110     fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare);
111 #endif
112 
113     hashindex = hashit(uid);
114 
115     if (!is_empty_hash(hashindex))
116     {
117 	if (!wecare)
118 	    return 0;		/* Don't clobber a slot for trash */
119 	if (hash_table[hashindex].uid == uid)
120 	    return(hashindex);	/* Fortuitous find */
121     }
122 
123     /* empty or wrong slot -- fill it with new value */
124     hash_table[hashindex].uid = uid;
125     (void) strncpy(hash_table[hashindex].name, name, MAXLOGNAME - 1);
126     return(hashindex);
127 }
128 
129 /*
130  * Get a userid->name mapping from the system.
131  * If the passwd database is hashed (#define RANDOM_PW), we
132  * just handle this uid.
133  */
134 
135 int
136 get_user(int uid)
137 {
138     struct passwd *pwd;
139 
140     /* no performance penalty for using getpwuid makes it easy */
141     if ((pwd = getpwuid(uid)) != NULL)
142     {
143 	return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
144     }
145 
146     /* if we can't find the name at all, then use the uid as the name */
147     return(enter_user(uid, itoa7(uid), 1));
148 }
149