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

1.1     ! root        1: #include "ps.h"
        !             2: 
        !             3: #define NNAMES 16
        !             4: 
        !             5: char *fspts[3];
        !             6: 
        !             7: struct direct *fsbeg, *fsend, *fsprev;
        !             8: 
        !             9: #ifdef TEST
        !            10: main(argc, argv)
        !            11: char **argv;
        !            12: {
        !            13:        register struct direct *fs; int dev; char *getfs();
        !            14:        getfstab();
        !            15:        if (argc <= 1)
        !            16:                for (fs=fsbeg; fs < fsend; fs++)
        !            17:                        printf("%3d,%3d %.14s\n",
        !            18:                                major(fs->d_ino), minor(fs->d_ino), fs->d_name);
        !            19:        else while ((argc -= 2) > 0) {
        !            20:                dev = atoi(*++argv);
        !            21:                dev = makedev(dev, atoi(*++argv));
        !            22:                printf("%3d,%3d %.14s\n", major(dev), minor(dev), getfs(dev));
        !            23:        }
        !            24:        exit(0);
        !            25: }
        !            26: #endif
        !            27: 
        !            28: char *
        !            29: getfs(dev)
        !            30: register dev;
        !            31: {
        !            32:        register struct direct *fs;
        !            33: 
        !            34:        if ((fs = fsprev) && fs->d_ino == dev)
        !            35:                return fs->d_name;
        !            36:        for (fs=fsbeg; fs < fsend; fs++)
        !            37:                if (fs->d_ino == dev ||
        !            38:                    (major(fs->d_ino) == major(dev) && minor(fs->d_ino) == 255)) {
        !            39:                        fsprev = fs;
        !            40:                        return fs->d_name;
        !            41:                }
        !            42:        return 0;
        !            43: }
        !            44: 
        !            45: getfstab()
        !            46: {
        !            47:        char *buffer, *bufpt, *nlpt, *strchr(), *strrchr(), *sindex();
        !            48:        struct stat fd_st;
        !            49:        register struct direct *fs = 0;
        !            50:        register char *p; int fd;
        !            51: 
        !            52:        if (fsbeg)
        !            53:                return 0;
        !            54: 
        !            55:        if ((fd = open("/etc/fstab", 0)) < 0)
        !            56:                return 1;
        !            57: 
        !            58:        if (fstat(fd, &fd_st) || (buffer = malloc((int)fd_st.st_size+1)) == 0)
        !            59:                return close(fd), 1;
        !            60: 
        !            61:        if (!Read(fd, buffer, (int)fd_st.st_size))
        !            62:                return free(buffer), close(fd), 1;
        !            63:        buffer[fd_st.st_size] = 0;
        !            64: 
        !            65:        for (bufpt=buffer; nlpt=strchr(bufpt, '\n'); bufpt=nlpt) {
        !            66:                *nlpt++ = 0;
        !            67:                if ((p = strchr(bufpt, ':')) == 0)
        !            68:                        continue;
        !            69:                *p = 0;
        !            70:                if (stat(bufpt, &fd_st))
        !            71:                        continue;
        !            72:                if ((fs = Nalloc(struct direct, fspts, NNAMES)) == 0)
        !            73:                        break;
        !            74:                if (p = strrchr(bufpt, '/'))
        !            75:                        ++p;
        !            76:                else
        !            77:                        p = bufpt;
        !            78:                strncpy(fs->d_name, p, DIRSIZ);
        !            79:                fs->d_ino = fd_st.st_rdev;
        !            80:        }
        !            81:        free(buffer), close(fd);
        !            82: 
        !            83:        if ((fd = open("/usr/net/friends", 0)) < 0)
        !            84:                return 1;
        !            85: 
        !            86:        if (fstat(fd, &fd_st) || (buffer = malloc((int)fd_st.st_size+1)) == 0)
        !            87:                return close(fd), 1;
        !            88: 
        !            89:        if (!Read(fd, buffer, (int)fd_st.st_size))
        !            90:                return free(buffer), close(fd), 1;
        !            91:        buffer[fd_st.st_size] = 0;
        !            92: 
        !            93:        for (bufpt=buffer; nlpt=strchr(bufpt, '\n'); bufpt=nlpt) {
        !            94:                *nlpt++ = 0;
        !            95:                if ((bufpt = sindex(bufpt, " \t")) == 0)
        !            96:                        continue;
        !            97:                if ((bufpt = strrchr(bufpt, '/')) == 0)
        !            98:                        continue;
        !            99:                if ((p = sindex(++bufpt, " \t")) == 0)
        !           100:                        continue;
        !           101:                *p = 0;
        !           102:                if ((fs = Nalloc(struct direct, fspts, NNAMES)) == 0)
        !           103:                        break;
        !           104:                strncpy(fs->d_name, bufpt, DIRSIZ);
        !           105:                fs->d_ino = makedev(atoi(++p), 255);
        !           106:        }
        !           107:        free(buffer), close(fd);
        !           108: 
        !           109:        if (stat(bufpt = "/proc", &fd_st) == 0 &&
        !           110:           (fs = Nalloc(struct direct, fspts, NNAMES))) {
        !           111:                strncpy(fs->d_name, bufpt+1, DIRSIZ);
        !           112:                fs->d_ino = fd_st.st_dev;
        !           113:        }
        !           114:        if (fs) {
        !           115:                fsbeg = (struct direct *)fspts[0];
        !           116:                fsend = (struct direct *)fspts[1];
        !           117:        }
        !           118:        return 0;
        !           119: }
        !           120: 
        !           121: char *
        !           122: sindex(s, c)
        !           123: register char *s, *c;
        !           124: {
        !           125:        register char *p = 0, *q;
        !           126:        while (*c) {
        !           127:                if ((q = strchr(s, *c++)) == 0)
        !           128:                        continue;
        !           129:                if (p == 0 || q < p)
        !           130:                        p = q;
        !           131:        }
        !           132:        return p;
        !           133: }

unix.superglobalmegacorp.com

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