AMD64 - Refactor the 'top' program.
[dragonfly.git] / contrib / top / username.c
1 /*
2  *  Top users/processes display for Unix
3  *  Version 3
4  *
5  *  This program may be freely redistributed,
6  *  but this entire comment MUST remain intact.
7  *
8  *  Copyright (c) 1984, 1989, William LeFebvre, Rice University
9  *  Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
10  *
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 $
13  */
14
15 /*
16  *  Username translation code for top.
17  *
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.
23  *
24  *  Changes to these have been provided by John Gilmore (gnu@toad.com).
25  *
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.
32  */
33
34 #include <sys/types.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <pwd.h>
39 #include <utmp.h>
40
41 #include "top.local.h"
42 #include "utils.h"
43 #include "username.h"
44
45 struct hash_el {
46     int  uid;
47     char name[UT_NAMESIZE + 1];
48 };
49
50 static int get_user(int uid);
51
52 #define    is_empty_hash(x)     (hash_table[x].name[0] == 0)
53
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
57 */
58 #define    hashit(i)    (abs(i) % Table_size)
59
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];
63
64 void
65 init_hash(void)
66 {
67     /*
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.
71      */
72 }
73
74 char *
75 username(long uid)
76 {
77     int hashindex;
78
79     hashindex = hashit(uid);
80     if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid))
81     {
82         /* not here or not right -- get it out of passwd */
83         hashindex = get_user(uid);
84     }
85     return(hash_table[hashindex].name);
86 }
87
88 int
89 userid(char *xusername)
90 {
91     struct passwd *pwd;
92
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.
95      */
96
97     if ((pwd = getpwnam(xusername)) == NULL)
98     {
99         return(-1);
100     }
101
102     /* enter the result in the hash table */
103     enter_user(pwd->pw_uid, xusername, 1);
104
105     /* return our result */
106     return(pwd->pw_uid);
107 }
108
109 int
110 enter_user(int uid, char *name, int wecare)
111 {
112     int hashindex;
113
114 #ifdef DEBUG
115     fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare);
116 #endif
117
118     hashindex = hashit(uid);
119
120     if (!is_empty_hash(hashindex))
121     {
122         if (!wecare)
123             return 0;           /* Don't clobber a slot for trash */
124         if (hash_table[hashindex].uid == uid)
125             return(hashindex);  /* Fortuitous find */
126     }
127
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);
131     return(hashindex);
132 }
133
134 /*
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.
139  */
140
141 static int
142 get_user(int uid)
143 {
144     struct passwd *pwd;
145
146 #ifdef RANDOM_PW
147     /* no performance penalty for using getpwuid makes it easy */
148     if ((pwd = getpwuid(uid)) != NULL)
149     {
150         return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
151     }
152 #else
153
154     int from_start = 0;
155
156     /*
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.
167      */
168
169     while (from_start++ < 2)
170     {
171         while ((pwd = getpwent()) != NULL)
172         {
173             if (pwd->pw_uid == uid)
174             {
175                 return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
176             }
177             (void) enter_user(pwd->pw_uid, pwd->pw_name, 0);
178         }
179         /* try again */
180         setpwent();
181     }
182 #endif
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));
185 }