Annotation of 42BSD/bin/df.c, revision 1.1

1.1     ! root        1: #ifndef lint
        !             2: static char *sccsid = "@(#)df.c        4.17 6/19/83";
        !             3: #endif
        !             4: 
        !             5: #include <sys/param.h>
        !             6: #include <sys/fs.h>
        !             7: #include <sys/stat.h>
        !             8: 
        !             9: #include <stdio.h>
        !            10: #include <fstab.h>
        !            11: #include <mtab.h>
        !            12: 
        !            13: /*
        !            14:  * df
        !            15:  */
        !            16: struct mtab mtab[NMOUNT];
        !            17: char   root[32];
        !            18: char   *mpath();
        !            19: 
        !            20: int    iflag;
        !            21: 
        !            22: union {
        !            23:        struct fs iu_fs;
        !            24:        char dummy[SBSIZE];
        !            25: } sb;
        !            26: #define sblock sb.iu_fs
        !            27: 
        !            28: int    fi;
        !            29: daddr_t        alloc();
        !            30: char   *strcpy();
        !            31: 
        !            32: main(argc, argv)
        !            33:        int argc;
        !            34:        char **argv;
        !            35: {
        !            36:        int i;
        !            37: 
        !            38:        while (argc > 1 && argv[1][0]=='-') {
        !            39:                switch (argv[1][1]) {
        !            40: 
        !            41:                case 'i':
        !            42:                        iflag++;
        !            43:                        break;
        !            44: 
        !            45:                default:
        !            46:                        fprintf(stderr, "usage: df [ -i ] [ filsys... ]\n");
        !            47:                        exit(0);
        !            48:                }
        !            49:                argc--, argv++;
        !            50:        }
        !            51:        i = open("/etc/mtab", 0);
        !            52:        if (i >= 0) {
        !            53:                (void) read(i, (char *)mtab, sizeof (mtab));
        !            54:                (void) close(i);
        !            55:        }
        !            56:        sync();
        !            57:        printf("Filesystem    kbytes    used   avail capacity");
        !            58:        if (iflag)
        !            59:                printf(" iused   ifree  %%iused");
        !            60:        printf("  Mounted on\n");
        !            61:        if (argc <= 1) {
        !            62:                struct fstab *fsp;
        !            63: 
        !            64:                if (setfsent() == 0)
        !            65:                        perror(FSTAB), exit(1);
        !            66:                while (fsp = getfsent()) {
        !            67:                        if (strcmp(fsp->fs_type, FSTAB_RW) &&
        !            68:                            strcmp(fsp->fs_type, FSTAB_RO) &&
        !            69:                            strcmp(fsp->fs_type, FSTAB_RQ))
        !            70:                                continue;
        !            71:                        if (root[0] == 0)
        !            72:                                (void) strcpy(root, fsp->fs_spec);
        !            73:                        dfree(fsp->fs_spec, 1);
        !            74:                }
        !            75:                endfsent();
        !            76:                exit(0);
        !            77:        }
        !            78:        for (i=1; i<argc; i++)
        !            79:                dfree(argv[i], 0);
        !            80: }
        !            81: 
        !            82: dfree(file, infsent)
        !            83:        char *file;
        !            84:        int infsent;
        !            85: {
        !            86:        long totalblks, availblks, avail, free, used;
        !            87:        struct stat stbuf;
        !            88:        struct fstab *fsp;
        !            89: 
        !            90:        if (stat(file, &stbuf) == 0 &&
        !            91:            (stbuf.st_mode&S_IFMT) != S_IFCHR &&
        !            92:            (stbuf.st_mode&S_IFMT) != S_IFBLK) {
        !            93:                if (infsent) {
        !            94:                        fprintf(stderr, "%s: screwy /etc/fstab entry\n", file);
        !            95:                        return;
        !            96:                }
        !            97:                setfsent();
        !            98:                while (fsp = getfsent()) {
        !            99:                        struct stat stb;
        !           100: 
        !           101:                        if (stat(fsp->fs_spec, &stb) == 0 &&
        !           102:                            stb.st_rdev == stbuf.st_dev) {
        !           103:                                file = fsp->fs_spec;
        !           104:                                endfsent();
        !           105:                                goto found;
        !           106:                        }
        !           107:                }
        !           108:                endfsent();
        !           109:                fprintf(stderr, "%s: mounted on unknown device\n", file);
        !           110:                return;
        !           111:        }
        !           112: found:
        !           113:        fi = open(file, 0);
        !           114:        if (fi < 0) {
        !           115:                perror(file);
        !           116:                return;
        !           117:        }
        !           118:        bread(SBLOCK, (char *)&sblock, SBSIZE);
        !           119:        printf("%-12.12s", file);
        !           120:        totalblks = sblock.fs_dsize;
        !           121:        free = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
        !           122:            sblock.fs_cstotal.cs_nffree;
        !           123:        used = totalblks - free;
        !           124:        availblks = totalblks * (100 - sblock.fs_minfree) / 100;
        !           125:        avail = availblks > used ? availblks - used : 0;
        !           126:        printf("%8d%8d%8d", totalblks * sblock.fs_fsize / 1024,
        !           127:            used * sblock.fs_fsize / 1024, avail * sblock.fs_fsize / 1024);
        !           128:        printf("%6.0f%%",
        !           129:            availblks == 0 ? 0.0 : (double) used / (double) availblks * 100.0);
        !           130:        if (iflag) {
        !           131:                int inodes = sblock.fs_ncg * sblock.fs_ipg;
        !           132:                used = inodes - sblock.fs_cstotal.cs_nifree;
        !           133:                printf("%8ld%8ld%6.0f%% ", used, sblock.fs_cstotal.cs_nifree,
        !           134:                    inodes == 0 ? 0.0 : (double)used / (double)inodes * 100.0);
        !           135:        } else 
        !           136:                printf("  ");
        !           137:        printf("  %s\n", mpath(file));
        !           138:        (void) close(fi);
        !           139: }
        !           140: 
        !           141: long lseek();
        !           142: 
        !           143: bread(bno, buf, cnt)
        !           144:        daddr_t bno;
        !           145:        char *buf;
        !           146: {
        !           147:        int n;
        !           148:        extern errno;
        !           149: 
        !           150:        (void) lseek(fi, (long)(bno * DEV_BSIZE), 0);
        !           151:        if ((n=read(fi, buf, cnt)) != cnt) {
        !           152:                printf("\nread error bno = %ld\n", bno);
        !           153:                printf("count = %d; errno = %d\n", n, errno);
        !           154:                exit(0);
        !           155:        }
        !           156: }
        !           157: 
        !           158: /*
        !           159:  * Given a name like /dev/rrp0h, returns the mounted path, like /usr.
        !           160:  */
        !           161: char *
        !           162: mpath(file)
        !           163:        char *file;
        !           164: {
        !           165:        register struct mtab *mp;
        !           166: 
        !           167:        if (eq(file, root))
        !           168:                return ("/");
        !           169:        for (mp = mtab; mp < mtab + NMOUNT; mp++)
        !           170:                if (eq(file, mp->m_dname))
        !           171:                        return (mp->m_path);
        !           172:        return "";
        !           173: }
        !           174: 
        !           175: eq(f1, f2)
        !           176:        char *f1, *f2;
        !           177: {
        !           178: 
        !           179:        if (strncmp(f1, "/dev/", 5) == 0)
        !           180:                f1 += 5;
        !           181:        if (strncmp(f2, "/dev/", 5) == 0)
        !           182:                f2 += 5;
        !           183:        if (!strcmp(f1, f2))
        !           184:                return (1);
        !           185:        if (*f1 == 'r' && !strcmp(f1+1, f2))
        !           186:                return (1);
        !           187:        if (*f2 == 'r' && !strcmp(f1, f2+1))
        !           188:                return (1);
        !           189:        if (*f1 == 'r' && *f2 == 'r' && strcmp(f1+1, f2+1) == 0)
        !           190:                return (1);
        !           191:        return (0);
        !           192: }

unix.superglobalmegacorp.com

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