]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/top/username.c
top(1): set max username length based on system constant
[FreeBSD/FreeBSD.git] / usr.bin / 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$
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
33 #include <sys/param.h>
34 #include <sys/types.h>
35
36 #include <pwd.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.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[MAXLOGNAME];
48 };
49
50 #define    is_empty_hash(x)     (hash_table[x].name[0] == 0)
51
52 /* simple minded hashing function */
53 /* Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for
54    the hash_table.  Applied abs() function to fix. 2/16/96 tpugh
55 */
56 #define    hashit(i)    (abs(i) % Table_size)
57
58 /* K&R requires that statically declared tables be initialized to zero. */
59 /* We depend on that for hash_table and YOUR compiler had BETTER do it! */
60 struct hash_el hash_table[Table_size];
61
62
63 char *username(uid)
64
65 int uid;
66
67 {
68     int hashindex;
69
70     hashindex = hashit(uid);
71     if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid))
72     {
73         /* not here or not right -- get it out of passwd */
74         hashindex = get_user(uid);
75     }
76     return(hash_table[hashindex].name);
77 }
78
79 int userid(username)
80
81 char *username;
82
83 {
84     struct passwd *pwd;
85
86     /* Eventually we want this to enter everything in the hash table,
87        but for now we just do it simply and remember just the result.
88      */
89
90     if ((pwd = getpwnam(username)) == NULL)
91     {
92         return(-1);
93     }
94
95     /* enter the result in the hash table */
96     enter_user(pwd->pw_uid, username, 1);
97
98     /* return our result */
99     return(pwd->pw_uid);
100 }
101
102 int enter_user(uid, name, wecare)
103
104 int  uid;
105 char *name;
106 int wecare;             /* 1 = enter it always, 0 = nice to have */
107
108 {
109     int hashindex;
110
111 #ifdef DEBUG
112     fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare);
113 #endif
114
115     hashindex = hashit(uid);
116
117     if (!is_empty_hash(hashindex))
118     {
119         if (!wecare)
120             return 0;           /* Don't clobber a slot for trash */
121         if (hash_table[hashindex].uid == uid)
122             return(hashindex);  /* Fortuitous find */
123     }
124
125     /* empty or wrong slot -- fill it with new value */
126     hash_table[hashindex].uid = uid;
127     (void) strncpy(hash_table[hashindex].name, name, MAXLOGNAME - 1);
128     return(hashindex);
129 }
130
131 /*
132  * Get a userid->name mapping from the system.
133  * If the passwd database is hashed (#define RANDOM_PW), we
134  * just handle this uid.  Otherwise we scan the passwd file
135  * and cache any entries we pass over while looking.
136  */
137
138 int get_user(uid)
139
140 int uid;
141
142 {
143     struct passwd *pwd;
144
145 #ifdef RANDOM_PW
146     /* no performance penalty for using getpwuid makes it easy */
147     if ((pwd = getpwuid(uid)) != NULL)
148     {
149         return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
150     }
151 #else
152
153     int from_start = 0;
154
155     /*
156      *  If we just called getpwuid each time, things would be very slow
157      *  since that just iterates through the passwd file each time.  So,
158      *  we walk through the file instead (using getpwent) and cache each
159      *  entry as we go.  Once the right record is found, we cache it and
160      *  return immediately.  The next time we come in, getpwent will get
161      *  the next record.  In theory, we never have to read the passwd file
162      *  a second time (because we cache everything we read).  But in
163      *  practice, the cache may not be large enough, so if we don't find
164      *  it the first time we have to scan the file a second time.  This
165      *  is not very efficient, but it will do for now.
166      */
167
168     while (from_start++ < 2)
169     {
170         while ((pwd = getpwent()) != NULL)
171         {
172             if (pwd->pw_uid == uid)
173             {
174                 return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
175             }
176             (void) enter_user(pwd->pw_uid, pwd->pw_name, 0);
177         }
178         /* try again */
179         setpwent();
180     }
181 #endif
182     /* if we can't find the name at all, then use the uid as the name */
183     return(enter_user(uid, itoa7(uid), 1));
184 }