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 34 #include <pwd.h> 35 #include <stdbool.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 #define hashit(i) (abs(i) % Table_size) 52 53 /* K&R requires that statically declared tables be initialized to zero. */ 54 /* We depend on that for hash_table and YOUR compiler had BETTER do it! */ 55 static struct hash_el hash_table[Table_size]; 56 57 58 char *username(int uid) 59 { 60 int hashindex; 61 62 hashindex = hashit(uid); 63 if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid)) 64 { 65 /* not here or not right -- get it out of passwd */ 66 hashindex = get_user(uid); 67 } 68 return(hash_table[hashindex].name); 69 } 70 71 int userid(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 */ 92 int enter_user(int uid, char name[], bool wecare) 93 { 94 int hashindex; 95 96 #ifdef DEBUG 97 fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare); 98 #endif 99 100 hashindex = hashit(uid); 101 102 if (!is_empty_hash(hashindex)) 103 { 104 if (!wecare) 105 return (0); /* Don't clobber a slot for trash */ 106 if (hash_table[hashindex].uid == uid) 107 return(hashindex); /* Fortuitous find */ 108 } 109 110 /* empty or wrong slot -- fill it with new value */ 111 hash_table[hashindex].uid = uid; 112 (void) strncpy(hash_table[hashindex].name, name, MAXLOGNAME - 1); 113 return(hashindex); 114 } 115 116 /* 117 * Get a userid->name mapping from the system. 118 */ 119 120 int 121 get_user(int uid) 122 { 123 struct passwd *pwd; 124 125 /* no performance penalty for using getpwuid makes it easy */ 126 if ((pwd = getpwuid(uid)) != NULL) 127 { 128 return(enter_user(pwd->pw_uid, pwd->pw_name, 1)); 129 } 130 131 /* if we can't find the name at all, then use the uid as the name */ 132 return(enter_user(uid, itoa7(uid), 1)); 133 } 134