Changeset View
Changeset View
Standalone View
Standalone View
username.c
Show All 26 Lines | |||||
* at the initial hash location is not right, it is replaced | * at the initial hash location is not right, it is replaced | ||||
* by the right value. Collisions will cause us to call getpw* | * by the right value. Collisions will cause us to call getpw* | ||||
* but hey, this is a cache, not the Library of Congress. | * but hey, this is a cache, not the Library of Congress. | ||||
* This makes the table size independent of the passwd file size. | * This makes the table size independent of the passwd file size. | ||||
*/ | */ | ||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <stdio.h> | |||||
#include <pwd.h> | #include <pwd.h> | ||||
#include <stdio.h> | |||||
#include <stdlib.h> | |||||
#include <string.h> | |||||
#include "top.local.h" | #include "top.local.h" | ||||
#include "utils.h" | #include "utils.h" | ||||
#include "username.h" | |||||
struct hash_el { | struct hash_el { | ||||
int uid; | int uid; | ||||
char name[MAXLOGNAME]; | char name[MAXLOGNAME]; | ||||
}; | }; | ||||
#define is_empty_hash(x) (hash_table[x].name[0] == 0) | #define is_empty_hash(x) (hash_table[x].name[0] == 0) | ||||
/* simple minded hashing function */ | /* simple minded hashing function */ | ||||
/* Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for | /* Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for | ||||
the hash_table. Applied abs() function to fix. 2/16/96 tpugh | the hash_table. Applied abs() function to fix. 2/16/96 tpugh | ||||
*/ | */ | ||||
#define hashit(i) (abs(i) % Table_size) | #define hashit(i) (abs(i) % Table_size) | ||||
/* K&R requires that statically declared tables be initialized to zero. */ | /* K&R requires that statically declared tables be initialized to zero. */ | ||||
/* We depend on that for hash_table and YOUR compiler had BETTER do it! */ | /* We depend on that for hash_table and YOUR compiler had BETTER do it! */ | ||||
struct hash_el hash_table[Table_size]; | struct hash_el hash_table[Table_size]; | ||||
void | |||||
init_hash() | init_hash() | ||||
{ | { | ||||
/* | /* | ||||
* There used to be some steps we had to take to initialize things. | * There used to be some steps we had to take to initialize things. | ||||
* We don't need to do that anymore, but we will leave this stub in | * We don't need to do that anymore, but we will leave this stub in | ||||
* just in case future changes require initialization steps. | * just in case future changes require initialization steps. | ||||
*/ | */ | ||||
} | } | ||||
char *username(uid) | char *username(uid) | ||||
register int uid; | int uid; | ||||
{ | { | ||||
register int hashindex; | register int hashindex; | ||||
hashindex = hashit(uid); | hashindex = hashit(uid); | ||||
if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid)) | if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid)) | ||||
{ | { | ||||
/* not here or not right -- get it out of passwd */ | /* not here or not right -- get it out of passwd */ | ||||
Show All 22 Lines | return(-1); | ||||
enter_user(pwd->pw_uid, username, 1); | enter_user(pwd->pw_uid, username, 1); | ||||
/* return our result */ | /* return our result */ | ||||
return(pwd->pw_uid); | return(pwd->pw_uid); | ||||
} | } | ||||
int enter_user(uid, name, wecare) | int enter_user(uid, name, wecare) | ||||
register int uid; | int uid; | ||||
register char *name; | char *name; | ||||
int wecare; /* 1 = enter it always, 0 = nice to have */ | int wecare; /* 1 = enter it always, 0 = nice to have */ | ||||
{ | { | ||||
register int hashindex; | register int hashindex; | ||||
#ifdef DEBUG | #ifdef DEBUG | ||||
fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare); | fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare); | ||||
#endif | #endif | ||||
Show All 18 Lines | |||||
* Get a userid->name mapping from the system. | * Get a userid->name mapping from the system. | ||||
* If the passwd database is hashed (#define RANDOM_PW), we | * If the passwd database is hashed (#define RANDOM_PW), we | ||||
* just handle this uid. Otherwise we scan the passwd file | * just handle this uid. Otherwise we scan the passwd file | ||||
* and cache any entries we pass over while looking. | * and cache any entries we pass over while looking. | ||||
*/ | */ | ||||
int get_user(uid) | int get_user(uid) | ||||
register int uid; | int uid; | ||||
{ | { | ||||
struct passwd *pwd; | struct passwd *pwd; | ||||
#ifdef RANDOM_PW | #ifdef RANDOM_PW | ||||
/* no performance penalty for using getpwuid makes it easy */ | /* no performance penalty for using getpwuid makes it easy */ | ||||
if ((pwd = getpwuid(uid)) != NULL) | if ((pwd = getpwuid(uid)) != NULL) | ||||
{ | { | ||||
Show All 36 Lines |