home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume27 / top-3.2 / part01 / username.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  5KB  |  183 lines

  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.  
  12. /*
  13.  *  Username translation code for top.
  14.  *
  15.  *  These routines handle uid to username mapping.
  16.  *  They use a hashing table scheme to reduce reading overhead.
  17.  *  For the time being, these are very straightforward hashing routines.
  18.  *  Maybe someday I'll put in something better.  But with the advent of
  19.  *  "random access" password files, it might not be worth the effort.
  20.  *
  21.  *  Changes to these have been provided by John Gilmore (gnu@toad.com).
  22.  *
  23.  *  The hash has been simplified in this release, to avoid the
  24.  *  table overflow problems of previous releases.  If the value
  25.  *  at the initial hash location is not right, it is replaced
  26.  *  by the right value.  Collisions will cause us to call getpw*
  27.  *  but hey, this is a cache, not the Library of Congress.
  28.  *  This makes the table size independent of the passwd file size.
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <pwd.h>
  33.  
  34. #include "top.local.h"
  35. #include "utils.h"
  36.  
  37. struct hash_el {
  38.     int  uid;
  39.     char name[9];
  40. };
  41.  
  42. #define    is_empty_hash(x)    (hash_table[x].name[0] == 0)
  43.  
  44. /* simple minded hashing function */
  45. #define    hashit(i)    ((i) % Table_size)
  46.  
  47. /* K&R requires that statically declared tables be initialized to zero. */
  48. /* We depend on that for hash_table and YOUR compiler had BETTER do it! */
  49. struct hash_el hash_table[Table_size];
  50.  
  51. init_hash()
  52.  
  53. {
  54.     /*
  55.      *  There used to be some steps we had to take to initialize things.
  56.      *  We don't need to do that anymore, but we will leave this stub in
  57.      *  just in case future changes require initialization steps.
  58.      */
  59. }
  60.  
  61. char *username(uid)
  62.  
  63. register int uid;
  64.  
  65. {
  66.     register int hashindex;
  67.  
  68.     hashindex = hashit(uid);
  69.     if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid))
  70.     {
  71.     /* not here or not right -- get it out of passwd */
  72.     hashindex = get_user(uid);
  73.     }
  74.     return(hash_table[hashindex].name);
  75. }
  76.  
  77. int userid(username)
  78.  
  79. char *username;
  80.  
  81. {
  82.     struct passwd *pwd;
  83.  
  84.     /* Eventually we want this to enter everything in the hash table,
  85.        but for now we just do it simply and remember just the result.
  86.      */
  87.  
  88.     if ((pwd = getpwnam(username)) == NULL)
  89.     {
  90.     return(-1);
  91.     }
  92.  
  93.     /* enter the result in the hash table */
  94.     enter_user(pwd->pw_uid, username, 1);
  95.  
  96.     /* return our result */
  97.     return(pwd->pw_uid);
  98. }
  99.  
  100. int enter_user(uid, name, wecare)
  101.  
  102. register int  uid;
  103. register char *name;
  104. int wecare;        /* 1 = enter it always, 0 = nice to have */
  105.  
  106. {
  107.     register int hashindex;
  108.  
  109. #ifdef DEBUG
  110.     fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare);
  111. #endif
  112.  
  113.     hashindex = hashit(uid);
  114.  
  115.     if (!is_empty_hash(hashindex))
  116.     {
  117.     if (!wecare)
  118.         return 0;        /* Don't clobber a slot for trash */
  119.     if (hash_table[hashindex].uid == uid)
  120.         return(hashindex);    /* Fortuitous find */
  121.     }
  122.  
  123.     /* empty or wrong slot -- fill it with new value */
  124.     hash_table[hashindex].uid = uid;
  125.     (void) strncpy(hash_table[hashindex].name, name, 8);
  126.     return(hashindex);
  127. }
  128.  
  129. /*
  130.  * Get a userid->name mapping from the system.
  131.  * If the passwd database is hashed (#define RANDOM_PW), we
  132.  * just handle this uid.  Otherwise we scan the passwd file
  133.  * and cache any entries we pass over while looking.
  134.  */
  135.  
  136. int get_user(uid)
  137.  
  138. register int uid;
  139.  
  140. {
  141.     struct passwd *pwd;
  142.  
  143. #ifdef RANDOM_PW
  144.     /* no performance penalty for using getpwuid makes it easy */
  145.     if ((pwd = getpwuid(uid)) != NULL)
  146.     {
  147.     return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
  148.     }
  149. #else
  150.  
  151.     int from_start = 0;
  152.  
  153.     /*
  154.      *  If we just called getpwuid each time, things would be very slow
  155.      *  since that just iterates through the passwd file each time.  So,
  156.      *  we walk through the file instead (using getpwent) and cache each
  157.      *  entry as we go.  Once the right record is found, we cache it and
  158.      *  return immediately.  The next time we come in, getpwent will get
  159.      *  the next record.  In theory, we never have to read the passwd file
  160.      *  a second time (because we cache everything we read).  But in
  161.      *  practice, the cache may not be large enough, so if we don't find
  162.      *  it the first time we have to scan the file a second time.  This
  163.      *  is not very efficient, but it will do for now.
  164.      */
  165.  
  166.     while (from_start++ < 2)
  167.     {
  168.     while ((pwd = getpwent()) != NULL)
  169.     {
  170.         if (pwd->pw_uid == uid)
  171.         {
  172.         return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
  173.         }
  174.         (void) enter_user(pwd->pw_uid, pwd->pw_name, 0);
  175.     }
  176.     /* try again */
  177.     setpwent();
  178.     }
  179. #endif
  180.     /* if we can't find the name at all, then use the uid as the name */
  181.     return(enter_user(uid, itoa7(uid), 1));
  182. }
  183.