Annotation of researchv9/cmd/ps/getuname.c, revision 1.1

1.1     ! root        1: #include "ps.h"
        !             2: 
        !             3: #define GETC(fd, n, p, buffer) \
        !             4:                (--n > 0 || \
        !             5:                (n = read(fd, p = buffer, sizeof buffer)) > 0 ? *p++ : -1)
        !             6: 
        !             7: #define NNAMES 100
        !             8: 
        !             9: char *unpts[3];
        !            10: 
        !            11: enum States { START, NAME, SKCOL, UID, SKNL };
        !            12: 
        !            13: struct direct *unbeg, *unend, *unprev;
        !            14: 
        !            15: #ifdef TEST
        !            16: main(argc, argv)
        !            17: char **argv;
        !            18: {
        !            19:        register struct direct *up; int uid; char *getuname();
        !            20:        getlogins();
        !            21:        if (argc <= 1)
        !            22:                for (up=unbeg; up < unend; up++)
        !            23:                        printf("%5d %.14s\n", up->d_ino, up->d_name);
        !            24:        else while (--argc > 0) {
        !            25:                uid = atoi(*++argv);
        !            26:                printf("%5d %.14s\n", uid, getuname(uid));
        !            27:        }
        !            28:        exit(0);
        !            29: }
        !            30: #endif
        !            31: 
        !            32: char *
        !            33: getuname(uid)
        !            34: register uid;
        !            35: {
        !            36:        register struct direct *up;
        !            37: 
        !            38:        if ((up = unprev) && up->d_ino == uid)
        !            39:                return up->d_name;
        !            40:        for (up=unbeg; up < unend; up++)
        !            41:                if (up->d_ino == uid) {
        !            42:                        unprev = up;
        !            43:                        return up->d_name;
        !            44:                }
        !            45:        return "?";
        !            46: }
        !            47: 
        !            48: getlogins()
        !            49: {
        !            50:        char buffer[1024];
        !            51:        register fd, c, state, n; register struct direct *up = 0;
        !            52:        register char *p; int nbuf = 0;
        !            53: 
        !            54:        if (unbeg)
        !            55:                return 0;
        !            56: 
        !            57:        if ((fd = open("/etc/passwd", 0)) < 0)
        !            58:                return 1;
        !            59: 
        !            60:        state = (int)START;
        !            61:        while ((c = GETC(fd, nbuf, p, buffer)) > 0) switch (state) {
        !            62:        case START:
        !            63:                if ((up = Nalloc(struct direct, unpts, NNAMES)) == 0)
        !            64:                        goto out;
        !            65:                n = 0;
        !            66:                up->d_ino = 0;
        !            67:                state = (int)NAME;
        !            68:                /* fall through */
        !            69:        case NAME:
        !            70:                if (c == ':') {
        !            71:                        if (n < DIRSIZ)
        !            72:                                up->d_name[n++] = 0;
        !            73:                        state = (int)SKCOL;
        !            74:                } else if (n < DIRSIZ)
        !            75:                        up->d_name[n++] = c;
        !            76:                break;
        !            77:        case SKCOL:
        !            78:                if (c == ':')
        !            79:                        state = (int)UID;
        !            80:                break;
        !            81:        case UID:
        !            82:                if (c >= '0' && c <= '9')
        !            83:                        up->d_ino = 10*up->d_ino + c - '0';
        !            84:                else
        !            85:                        state = (int)SKNL;
        !            86:                break;
        !            87:        case SKNL:
        !            88:                if (c == '\n')
        !            89:                        state = (int)START;
        !            90:                break;
        !            91:        }
        !            92: out:
        !            93:        if (up) {
        !            94:                unbeg = (struct direct *)unpts[0];
        !            95:                unend = (struct direct *)unpts[1];
        !            96:        }
        !            97:        close(fd);
        !            98:        return 0;
        !            99: }

unix.superglobalmegacorp.com

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