1: From James.Gosling@CMU-VLSI@CMU-10A  Thu Sep 10 07:13:52 1981
   2: Date: 10 Sep 1981 10:07:48-EDT
   3: From: James.Gosling at CMU-VLSI at CMU-10A
   4: Reply-To: James.Gosling at CMU-10A
   5: To: IngVAX.eric@Berkeley
   6: Subject: getpwwho.c
   7: Status: R
   8: 
   9: /*
  10:  **********************************************************************
  11:  * HISTORY
  12:  * 11-Dec-80  Mike Accetta(mja) at Carnegie-Mellon University
  13:  *	Changed to convert all separator characters to spaces when
  14:  *	copying name into internal buffer so that dots in names with
  15:  *	more than two parts will match password file entries.
  16:  *
  17:  * 03-Dec-80  Mike Accetta (mja) at Carnegie-Mellon University
  18:  *	Changed to treat everything before last blank in name as first
  19:  *	name and modified to close password file when called with null
  20:  *	pointer.
  21:  *
  22:  * 29-Nov-80  Mike Accetta (mja) at Carnegie-Mellon University
  23:  *	Changed to allow '.' as separator in names.
  24:  *
  25:  * 07-Nov-80  Mike Accetta (mja) at Carnegie-Mellon University
  26:  *	Removed reference to (extinct) alias name.
  27:  *
  28:  **********************************************************************
  29:  */
  30: 
  31: #include <stdio.h>
  32: #include <ctype.h>
  33: #include <pwd.h>
  34: #include <gecos.h>
  35: 
  36: #define dotisspace(c)   (isspace(c) || (c) == '.')
  37: 
  38: struct gecos *parsgecos();
  39: 
  40: static char PASSWD[]    = "/etc/passwd";
  41: static char EMPTY[] = "";
  42: static FILE *pwf = NULL;
  43: static char line[BUFSIZ+1], auxline[BUFSIZ+1];
  44: static struct passwd passwd, auxpasswd;
  45: static struct gecos auxgecos;
  46: static int ambigstate;
  47: 
  48: static setpwent()
  49: {
  50:     if( pwf == NULL )
  51:         pwf = fopen( PASSWD, "r" );
  52:     else
  53:         rewind( pwf );
  54: }
  55: 
  56: static endpwent()
  57: {
  58:     if( pwf != NULL ){
  59:         fclose( pwf );
  60:         pwf = NULL;
  61:     }
  62: }
  63: 
  64: static char *
  65: pwskip(p)
  66: register char *p;
  67: {
  68:     while( *p && *p != ':' )
  69:         ++p;
  70:     if( *p ) *p++ = 0;
  71:     return(p);
  72: }
  73: 
  74: static struct passwd *getpwent(passwd,line)
  75: register struct passwd *passwd;
  76: char * line;
  77: {
  78:     register char *p;
  79: 
  80:     if (pwf == NULL) {
  81:         if( (pwf = fopen( PASSWD, "r" )) == NULL )
  82:             return(0);
  83:     }
  84:     p = fgets(line, BUFSIZ, pwf);
  85:     if (p==NULL)
  86:         return(0);
  87:     passwd->pw_name = p;
  88:     p = pwskip(p);
  89:     passwd->pw_passwd = p;
  90:     p = pwskip(p);
  91:     passwd->pw_uid = atoi(p);
  92:     p = pwskip(p);
  93:     passwd->pw_gid = atoi(p);
  94:     passwd->pw_quota = 0;
  95:     passwd->pw_comment = EMPTY;
  96:     p = pwskip(p);
  97:     passwd->pw_gecos = p;
  98:     p = pwskip(p);
  99:     passwd->pw_dir = p;
 100:     p = pwskip(p);
 101:     passwd->pw_shell = p;
 102:     while(*p && *p != '\n') p++;
 103:     *p = '\0';
 104:     return(passwd);
 105: }
 106: 
 107: struct gecos *_gecos;
 108: 
 109: static char name1[100], name2[100];
 110: static int name1l, name2l;
 111: 
 112: static scanpw (pw, buf)
 113: register struct passwd *pw;
 114: char   *buf;
 115: {
 116:     register struct gecos  *ge;
 117:     register char  *cp;
 118: 
 119:     while (getpwent (pw, buf)) {
 120:     _gecos = ge = parsgecos (pw -> pw_gecos);
 121:     if (*name1 == '\0' && strcmp (name2, pw -> pw_name) == 0)
 122:         return 1;
 123:     if (IsName (ge -> pw_who))
 124:         return 2;
 125:     }
 126:     return 0;
 127: }
 128: 
 129: static  IsName (name)
 130: char   *name; {
 131:     register char  *p1,
 132:                    *p2;
 133:     if (!FoldedEQ (name, name1, name1l))
 134:     return (0);
 135:     p1 = p2 = name;
 136:     while (*p2){
 137:     if (dotisspace (*p2))
 138:         p1 = p2+1;
 139:     p2++;
 140:     }
 141:     return (FoldedEQ (p1, name2, name2l));
 142: }
 143: 
 144: struct passwd  *
 145: getpwwho (name)
 146: char   *name; {
 147:     register char *s, *d;
 148:     ambigstate = -1;
 149:     if (name == NULL)
 150:     {
 151:     endpwent();
 152:     return(0);
 153:     }
 154:     s = d = name2;
 155:     name1[0] = '\0';
 156:     strcpy(name2, name);
 157:     for(; *d; d++)
 158:     if (dotisspace(*d)) {
 159:         *d = ' ';
 160:         s = d;
 161:     }
 162:     if(dotisspace(*s)){
 163:     *s++ = 0;
 164:     strcpy (name1, name2);
 165:     strcpy (name2, s);
 166:     }
 167:     name1l = strlen (name1);
 168:     name2l = strlen (name2);
 169:     setpwent ();
 170:     switch (scanpw (&passwd, line)) {
 171:     case 0:
 172:         return (0);
 173:     case 1:
 174:         return (&passwd);
 175:     default: ;
 176:     }
 177:     auxgecos = *_gecos;
 178:     while (1) {
 179:     switch (scanpw (&auxpasswd, auxline)) {
 180:         case 0:
 181:         if (ambigstate == -1) {
 182:             _gecos = &auxgecos;
 183:             return & passwd;
 184:         }
 185:         return (struct passwd  *) - 1;
 186:         case 1:
 187:         return & auxpasswd;
 188:         case 2:
 189:         ambigstate = 0;
 190:         break;
 191:     }
 192:     }
 193:     /*  NOTREACHED */
 194: }
 195: 
 196: struct passwd  *getpwambig () {
 197:     if (ambigstate < 0)
 198:     return 0;
 199:     if (ambigstate++ == 0)
 200:     setpwent();
 201:     if (!scanpw (&passwd, line)) {
 202:     ambigstate = -1;
 203:     return 0;
 204:     }
 205:     return & passwd;
 206: }
 207: 
 208: static FoldedEQ(s1,s2,len)
 209: register char *s1,*s2;
 210: register len; {
 211:     while(--len>=0 && *s1 && (isupper(*s1) ? tolower(*s1) : *s1)
 212:                   == (isupper(*s2) ? tolower(*s2) : *s2))
 213:         s1++, s2++;
 214:     return len<0 || *s1==0 && *s2==0;
 215: }

Defined functions

FoldedEQ defined in line 208; used 2 times
IsName defined in line 129; used 1 times
endpwent defined in line 56; used 1 times
getpwambig defined in line 196; never used
getpwent defined in line 74; used 1 times
getpwwho defined in line 144; used 1 times
  • in line 6
pwskip defined in line 64; used 6 times
scanpw defined in line 112; used 3 times
setpwent defined in line 48; used 2 times

Defined variables

EMPTY defined in line 41; used 1 times
  • in line 95
PASSWD defined in line 40; used 2 times
_gecos defined in line 107; used 3 times
ambigstate defined in line 46; used 6 times
auxgecos defined in line 45; used 2 times
auxline defined in line 43; used 1 times
auxpasswd defined in line 44; used 2 times
line defined in line 43; used 5 times
name1 defined in line 109; used 5 times
name1l defined in line 110; used 2 times
name2 defined in line 109; used 7 times
name2l defined in line 110; used 2 times
passwd defined in line 44; used 17 times

Defined macros

dotisspace defined in line 36; used 3 times
Last modified: 1983-12-09
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1301
Valid CSS Valid XHTML 1.0 Strict