1: /*
   2:  * Modified version of getpwnam which doesn't use stdio.
   3:  * This is done to keep it (and hence csh) small at a small
   4:  * cost in speed.
   5:  */
   6: #include <pwd.h>
   7: #include <whoami.h>
   8: 
   9: #ifdef UCB_PWHASH
  10: #include <sys/pwdtbl.h>
  11: #include <sys/stat.h>
  12: #define pwf _pw_file
  13: 
  14: static char NTBL[] = "/etc/nam_pw_map"; /* name of name table */
  15: static int  nf = -1;            /* name table */
  16: static int  nusers;             /* number of users */
  17: static off_t    nameloc ();         /* finds name from table */
  18: struct stat sbuf;               /* stat buffer */
  19: #endif
  20: 
  21: extern int  pwf;                /* password file */
  22: 
  23: struct passwd *
  24: getpwnam(name)
  25: char *name;
  26: {
  27:     register struct passwd *p;
  28:     struct passwd *getpwent();
  29: 
  30: #ifdef UCB_PWHASH
  31:     off_t loc;
  32: #endif
  33: 
  34:     if (pwf == -1)
  35:         setpwent();
  36: #ifdef UCB_PWHASH
  37:     loc = nameloc (name);
  38:     if (loc >= 0)  {
  39:         lseek (pwf,loc,0);
  40:         p = getpwent();
  41:         if (strcmp(p->pw_name,name) == 0)
  42:             return (p);
  43:     }
  44: #endif
  45: 
  46:                         /* begin linear search */
  47:     setpwent();
  48:     while( (p = getpwent()) && strcmp(name,p->pw_name) );
  49:     endpwent();
  50:     return(p);
  51: }
  52: 
  53: #ifdef UCB_PWHASH
  54: static off_t
  55: nameloc (name)              /* finds name from name->loc table */
  56: char    *name;
  57: {
  58:     off_t       high;           /* high pointer to file */
  59:     off_t       low;            /* low pointer to file */
  60:     off_t       test;           /* current pointer to file */
  61:     struct pwloc    p;          /* table entry from file */
  62:     int     n;          /* temporary variable */
  63: 
  64:                         /* get status of name table
  65: 						 * file and open it */
  66:     if (nf == -1)  {
  67:         if (stat (NTBL,&sbuf) || (nf = open (NTBL,0)) == -1)
  68:             return ((off_t) -1);
  69: 
  70:                         /* compute number of entries */
  71:         nusers = sbuf.st_size/sizeof(struct pwloc);
  72:     }
  73: 
  74:     low = 0;                /* initialize pointer */
  75:     high = nusers;
  76: 
  77: compare:
  78:     test = (high+low+1)/2;          /* find midpoint */
  79:     if (test == high)           /* no valid midpoint */
  80:         return ((off_t) -1);
  81: 
  82:                         /* read table entry */
  83:     lseek (nf,test*sizeof (struct pwloc),0);
  84:     if (read(nf, &p, sizeof (struct pwloc)) != sizeof (struct pwloc))
  85:         return ((off_t) -1);
  86: 
  87:                         /* return if location found */
  88:     if ((n = strcmp8 (p.pwl_name,name)) == 0)
  89:         return (p.pwl_loc);
  90: 
  91:     if (n < 0)              /* adjust pointers */
  92:         low = test;
  93:     else
  94:         high = test;
  95:     goto compare;
  96: }
  97: 
  98: strcmp8 (a,b)
  99: register char   *a, *b;
 100: {
 101:     register char   *l;
 102:     register int    n;
 103: 
 104:     l = a+8;
 105:     while (a < l && (n = (*a++)-(*b++)) == 0);
 106:     return (n);
 107: }
 108: #endif

Defined functions

getpwnam defined in line 23; used 2 times
nameloc defined in line 54; used 2 times
strcmp8 defined in line 98; used 1 times
  • in line 88

Defined variables

NTBL defined in line 14; used 2 times
  • in line 67(2)
nf defined in line 15; used 4 times
nusers defined in line 16; used 2 times
sbuf defined in line 18; used 2 times

Defined macros

pwf defined in line 12; used 2 times
Last modified: 1980-09-12
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 748
Valid CSS Valid XHTML 1.0 Strict