Annotation of researchv9/libc/stdio/getpwent.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <pwd.h>
        !             3: 
        !             4: static char PASSWD[]   = "/etc/passwd";
        !             5: static char EMPTY[] = "";
        !             6: static FILE *pwf = NULL;
        !             7: static char line[BUFSIZ+2];
        !             8: static struct passwd passwd;
        !             9: 
        !            10: setpwent()
        !            11: {
        !            12:        if( pwf == NULL )
        !            13:                pwf = fopen( PASSWD, "r" );
        !            14:        else
        !            15:                rewind( pwf );
        !            16: }
        !            17: 
        !            18: endpwent()
        !            19: {
        !            20:        if( pwf != NULL ){
        !            21:                fclose( pwf );
        !            22:                pwf = NULL;
        !            23:        }
        !            24: }
        !            25: 
        !            26: static char *
        !            27: pwskip(p)
        !            28: register char *p;
        !            29: {
        !            30:        while( *p && *p != ':' && *p != '\n' )
        !            31:                ++p;
        !            32:        if( *p ) *p++ = 0;
        !            33:        else p[1] = 0;
        !            34:        return(p);
        !            35: }
        !            36: 
        !            37: struct passwd *
        !            38: pwdecode(p)
        !            39:        register char *p;
        !            40: {
        !            41:        passwd.pw_name = p;
        !            42:        p = pwskip(p);
        !            43:        passwd.pw_passwd = p;
        !            44:        p = pwskip(p);
        !            45:        passwd.pw_uid = atoi(p);
        !            46:        p = pwskip(p);
        !            47:        passwd.pw_gid = atoi(p);
        !            48:        passwd.pw_quota = 0;
        !            49:        passwd.pw_comment = EMPTY;
        !            50:        p = pwskip(p);
        !            51:        passwd.pw_gecos = p;
        !            52:        p = pwskip(p);
        !            53:        passwd.pw_dir = p;
        !            54:        p = pwskip(p);
        !            55:        passwd.pw_shell = p;
        !            56:        p = pwskip(p);
        !            57:        return(&passwd);
        !            58: }
        !            59: 
        !            60: struct passwd *
        !            61: getpwent()
        !            62: {
        !            63:        register char *p;
        !            64: 
        !            65:        if (pwf == NULL) {
        !            66:                if( (pwf = fopen( PASSWD, "r" )) == NULL )
        !            67:                        return(0);
        !            68:        }
        !            69:        p = fgets(line, BUFSIZ, pwf);
        !            70:        if (p==NULL)
        !            71:                return(0);
        !            72:        return pwdecode (p);
        !            73: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.