Annotation of src/usr.bin/top/username.c, Revision 1.1
1.1 ! downsj 1: /* $OpenBSD$ */
! 2:
! 3: /*
! 4: * Top users/processes display for Unix
! 5: * Version 3
! 6: *
! 7: * This program may be freely redistributed,
! 8: * but this entire comment MUST remain intact.
! 9: *
! 10: * Copyright (c) 1984, 1989, William LeFebvre, Rice University
! 11: * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
! 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 <stdio.h>
! 34: #include <pwd.h>
! 35:
! 36: #include "top.local.h"
! 37: #include "utils.h"
! 38:
! 39: struct hash_el {
! 40: int uid;
! 41: char name[9];
! 42: };
! 43:
! 44: #define is_empty_hash(x) (hash_table[x].name[0] == 0)
! 45:
! 46: /* simple minded hashing function */
! 47: /* Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for
! 48: the hash_table. Applied abs() function to fix. 2/16/96 tpugh
! 49: */
! 50: #define hashit(i) (abs(i) % Table_size)
! 51:
! 52: /* K&R requires that statically declared tables be initialized to zero. */
! 53: /* We depend on that for hash_table and YOUR compiler had BETTER do it! */
! 54: struct hash_el hash_table[Table_size];
! 55:
! 56: init_hash()
! 57:
! 58: {
! 59: /*
! 60: * There used to be some steps we had to take to initialize things.
! 61: * We don't need to do that anymore, but we will leave this stub in
! 62: * just in case future changes require initialization steps.
! 63: */
! 64: }
! 65:
! 66: char *username(uid)
! 67:
! 68: register int uid;
! 69:
! 70: {
! 71: register int hashindex;
! 72:
! 73: hashindex = hashit(uid);
! 74: if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid))
! 75: {
! 76: /* not here or not right -- get it out of passwd */
! 77: hashindex = get_user(uid);
! 78: }
! 79: return(hash_table[hashindex].name);
! 80: }
! 81:
! 82: int userid(username)
! 83:
! 84: char *username;
! 85:
! 86: {
! 87: struct passwd *pwd;
! 88:
! 89: /* Eventually we want this to enter everything in the hash table,
! 90: but for now we just do it simply and remember just the result.
! 91: */
! 92:
! 93: if ((pwd = getpwnam(username)) == NULL)
! 94: {
! 95: return(-1);
! 96: }
! 97:
! 98: /* enter the result in the hash table */
! 99: enter_user(pwd->pw_uid, username, 1);
! 100:
! 101: /* return our result */
! 102: return(pwd->pw_uid);
! 103: }
! 104:
! 105: int enter_user(uid, name, wecare)
! 106:
! 107: register int uid;
! 108: register char *name;
! 109: int wecare; /* 1 = enter it always, 0 = nice to have */
! 110:
! 111: {
! 112: register 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, 8);
! 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: int get_user(uid)
! 142:
! 143: register int uid;
! 144:
! 145: {
! 146: struct passwd *pwd;
! 147:
! 148: #ifdef RANDOM_PW
! 149: /* no performance penalty for using getpwuid makes it easy */
! 150: if ((pwd = getpwuid(uid)) != NULL)
! 151: {
! 152: return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
! 153: }
! 154: #else
! 155:
! 156: int from_start = 0;
! 157:
! 158: /*
! 159: * If we just called getpwuid each time, things would be very slow
! 160: * since that just iterates through the passwd file each time. So,
! 161: * we walk through the file instead (using getpwent) and cache each
! 162: * entry as we go. Once the right record is found, we cache it and
! 163: * return immediately. The next time we come in, getpwent will get
! 164: * the next record. In theory, we never have to read the passwd file
! 165: * a second time (because we cache everything we read). But in
! 166: * practice, the cache may not be large enough, so if we don't find
! 167: * it the first time we have to scan the file a second time. This
! 168: * is not very efficient, but it will do for now.
! 169: */
! 170:
! 171: while (from_start++ < 2)
! 172: {
! 173: while ((pwd = getpwent()) != NULL)
! 174: {
! 175: if (pwd->pw_uid == uid)
! 176: {
! 177: return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
! 178: }
! 179: (void) enter_user(pwd->pw_uid, pwd->pw_name, 0);
! 180: }
! 181: /* try again */
! 182: setpwent();
! 183: }
! 184: #endif
! 185: /* if we can't find the name at all, then use the uid as the name */
! 186: return(enter_user(uid, itoa7(uid), 1));
! 187: }