Add trunc and truncf.
[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 <pwd.h>
37 #include <utmp.h>
38
39 #include "top.local.h"
40 #include "utils.h"
41
42 struct hash_el {
43     int  uid;
44     char name[UT_NAMESIZE + 1];
45 };
46
47 #define    is_empty_hash(x)     (hash_table[x].name[0] == 0)
48
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
52 */
53 #define    hashit(i)    (abs(i) % Table_size)
54
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];
58
59 init_hash()
60
61 {
62     /*
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.
66      */
67 }
68
69 char *username(uid)
70
71 register int uid;
72
73 {
74     register int hashindex;
75
76     hashindex = hashit(uid);
77     if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid))
78     {
79         /* not here or not right -- get it out of passwd */
80         hashindex = get_user(uid);
81     }
82     return(hash_table[hashindex].name);
83 }
84
85 int userid(username)
86
87 char *username;
88
89 {
90     struct passwd *pwd;
91
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.
94      */
95
96     if ((pwd = getpwnam(username)) == NULL)
97     {
98         return(-1);
99     }
100
101     /* enter the result in the hash table */
102     enter_user(pwd->pw_uid, username, 1);
103
104     /* return our result */
105     return(pwd->pw_uid);
106 }
107
108 int enter_user(uid, name, wecare)
109
110 register int  uid;
111 register char *name;
112 int wecare;             /* 1 = enter it always, 0 = nice to have */
113
114 {
115     register int hashindex;
116
117 #ifdef DEBUG
118     fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare);
119 #endif
120
121     hashindex = hashit(uid);
122
123     if (!is_empty_hash(hashindex))
124     {
125         if (!wecare)
126             return 0;           /* Don't clobber a slot for trash */
127         if (hash_table[hashindex].uid == uid)
128             return(hashindex);  /* Fortuitous find */
129     }
130
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);
134     return(hashindex);
135 }
136
137 /*
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.
142  */
143
144 int get_user(uid)
145
146 register int uid;
147
148 {
149     struct passwd *pwd;
150
151 #ifdef RANDOM_PW
152     /* no performance penalty for using getpwuid makes it easy */
153     if ((pwd = getpwuid(uid)) != NULL)
154     {
155         return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
156     }
157 #else
158
159     int from_start = 0;
160
161     /*
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.
172      */
173
174     while (from_start++ < 2)
175     {
176         while ((pwd = getpwent()) != NULL)
177         {
178             if (pwd->pw_uid == uid)
179             {
180                 return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
181             }
182             (void) enter_user(pwd->pw_uid, pwd->pw_name, 0);
183         }
184         /* try again */
185         setpwent();
186     }
187 #endif
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));
190 }