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