Annotation of 43BSD/lib/libc/gen/getpwent.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1984 Regents of the University of California.
        !             3:  * All rights reserved.  The Berkeley software License Agreement
        !             4:  * specifies the terms and conditions for redistribution.
        !             5:  */
        !             6: 
        !             7: #if defined(LIBC_SCCS) && !defined(lint)
        !             8: static char sccsid[] = "@(#)getpwent.c 5.2 (Berkeley) 3/9/86";
        !             9: #endif LIBC_SCCS and not lint
        !            10: 
        !            11: #include <stdio.h>
        !            12: #include <pwd.h>
        !            13: #include <ndbm.h>
        !            14: 
        !            15: static char EMPTY[] = "";
        !            16: static FILE *pwf = NULL;
        !            17: static char line[BUFSIZ+1];
        !            18: static struct passwd passwd;
        !            19: 
        !            20: /*
        !            21:  * The following are shared with getpwnamuid.c
        !            22:  */
        !            23: char   *_pw_file = "/etc/passwd";
        !            24: DBM    *_pw_db;
        !            25: int    _pw_stayopen;
        !            26: 
        !            27: setpwent()
        !            28: {
        !            29:        if (pwf == NULL)
        !            30:                pwf = fopen(_pw_file, "r");
        !            31:        else
        !            32:                rewind(pwf);
        !            33: }
        !            34: 
        !            35: endpwent()
        !            36: {
        !            37:        if (pwf != NULL) {
        !            38:                fclose(pwf);
        !            39:                pwf = NULL;
        !            40:        }
        !            41:        if (_pw_db != (DBM *)0) {
        !            42:                dbm_close(_pw_db);
        !            43:                _pw_db = (DBM *)0;
        !            44:                _pw_stayopen = 0;
        !            45:        }
        !            46: }
        !            47: 
        !            48: static char *
        !            49: pwskip(p)
        !            50: register char *p;
        !            51: {
        !            52:        while (*p && *p != ':' && *p != '\n')
        !            53:                ++p;
        !            54:        if (*p)
        !            55:                *p++ = 0;
        !            56:        return(p);
        !            57: }
        !            58: 
        !            59: struct passwd *
        !            60: getpwent()
        !            61: {
        !            62:        register char *p;
        !            63: 
        !            64:        if (pwf == NULL) {
        !            65:                if ((pwf = fopen( _pw_file, "r" )) == NULL)
        !            66:                        return(0);
        !            67:        }
        !            68:        p = fgets(line, BUFSIZ, pwf);
        !            69:        if (p == NULL)
        !            70:                return(0);
        !            71:        passwd.pw_name = p;
        !            72:        p = pwskip(p);
        !            73:        passwd.pw_passwd = p;
        !            74:        p = pwskip(p);
        !            75:        passwd.pw_uid = atoi(p);
        !            76:        p = pwskip(p);
        !            77:        passwd.pw_gid = atoi(p);
        !            78:        passwd.pw_quota = 0;
        !            79:        passwd.pw_comment = EMPTY;
        !            80:        p = pwskip(p);
        !            81:        passwd.pw_gecos = p;
        !            82:        p = pwskip(p);
        !            83:        passwd.pw_dir = p;
        !            84:        p = pwskip(p);
        !            85:        passwd.pw_shell = p;
        !            86:        while (*p && *p != '\n')
        !            87:                p++;
        !            88:        *p = '\0';
        !            89:        return(&passwd);
        !            90: }
        !            91: 
        !            92: setpwfile(file)
        !            93:        char *file;
        !            94: {
        !            95:        _pw_file = file;
        !            96: }

unix.superglobalmegacorp.com

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