1: /* @(#)getpwent.c 2.3 SCCS id keyword */ 2: #include <whoami.h> 3: #include <stdio.h> 4: #include <pwd.h> 5: 6: #ifdef UCB_PWHASH 7: #include <sys/types.h> 8: #include <pwtable.h> 9: #define NAMSIZ 8 10: #endif 11: 12: static char PASSWD[] = "/etc/passwd"; 13: static char EMPTY[] = ""; 14: static FILE *pwf = NULL; 15: static char line[BUFSIZ+1]; 16: static struct passwd passwd; 17: 18: setpwent() 19: { 20: if( pwf == NULL ) 21: pwf = fopen( PASSWD, "r" ); 22: else 23: rewind( pwf ); 24: } 25: 26: endpwent() 27: { 28: if( pwf != NULL ){ 29: fclose( pwf ); 30: pwf = NULL; 31: } 32: #ifdef UCB_PWHASH 33: endmapent(); 34: #endif 35: } 36: 37: static char * 38: pwskip(p) 39: register char *p; 40: { 41: while( *p && *p != ':' ) 42: ++p; 43: if( *p ) *p++ = 0; 44: return(p); 45: } 46: 47: struct passwd * 48: getpwent() 49: { 50: register char *p; 51: 52: if (pwf == NULL) { 53: if( (pwf = fopen( PASSWD, "r" )) == NULL ) 54: return(0); 55: } 56: p = fgets(line, BUFSIZ, pwf); 57: if (p==NULL) 58: return(0); 59: passwd.pw_name = p; 60: p = pwskip(p); 61: passwd.pw_passwd = p; 62: p = pwskip(p); 63: passwd.pw_uid = atoi(p); 64: p = pwskip(p); 65: passwd.pw_gid = atoi(p); 66: passwd.pw_quota = 0; 67: passwd.pw_comment = EMPTY; 68: p = pwskip(p); 69: passwd.pw_gecos = p; 70: p = pwskip(p); 71: passwd.pw_dir = p; 72: p = pwskip(p); 73: passwd.pw_shell = p; 74: while(*p && *p != '\n') p++; 75: *p = '\0'; 76: #ifdef UCB_SHELL 77: if (p==passwd.pw_shell) 78: passwd.pw_shell = UCB_SHELL; 79: #endif 80: return(&passwd); 81: } 82: 83: #ifdef UCB_PWHASH 84: struct passwd * 85: getpwmap(pwt) 86: struct pwtable *pwt; 87: { 88: struct passwd *p; 89: 90: if( pwt == NULL ) 91: return(NULL); 92: if( pwf == NULL ) 93: if( (pwf = fopen( PASSWD, "r" )) == NULL ) 94: return(NULL); 95: if( pwt->pwt_loc == 0L ) 96: rewind( pwf ); 97: else { 98: fseek( pwf, pwt->pwt_loc - 1L, 0 ); 99: if( getc(pwf) != '\n' ) 100: return(NULL); 101: } 102: if( (p = getpwent()) == NULL ) 103: return(NULL); 104: if( p->pw_uid != pwt->pwt_uid ) 105: return(NULL); 106: if( strncmp(p->pw_name, pwt->pwt_name, NAMSIZ) ) 107: return(NULL); 108: return(p); 109: } 110: #endif