Annotation of 41BSD/cmd/df.c, revision 1.1

1.1     ! root        1: static char *sccsid = "@(#)df.c        4.3 (Berkeley) 10/15/80";
        !             2: #include <stdio.h>
        !             3: #include <fstab.h>
        !             4: #include <sys/param.h>
        !             5: #include <sys/filsys.h>
        !             6: #include <sys/fblk.h>
        !             7: #include <sys/stat.h>
        !             8: /*
        !             9:  * df
        !            10:  */
        !            11: 
        !            12: #define NFS    20      /* Max number of filesystems */
        !            13: 
        !            14: struct {
        !            15:        char path[32];
        !            16:        char spec[32];
        !            17: } mtab[NFS];
        !            18: char root[32];
        !            19: 
        !            20: char *mpath();
        !            21: 
        !            22: daddr_t        blkno   = 1;
        !            23: 
        !            24: int    lflag;
        !            25: int    iflag;
        !            26: 
        !            27: struct filsys sblock;
        !            28: 
        !            29: int    fi;
        !            30: daddr_t        alloc();
        !            31: 
        !            32: main(argc, argv)
        !            33: char **argv;
        !            34: {
        !            35:        int i;
        !            36:        char buf[128];
        !            37: 
        !            38:        while (argc >= 1 && argv[1][0]=='-') {
        !            39:                switch(argv[1][1]) {
        !            40: 
        !            41:                case 'l':
        !            42:                        lflag++;
        !            43:                        break;
        !            44: 
        !            45:                case 'i':
        !            46:                        iflag++;
        !            47:                        break;
        !            48: 
        !            49:                default:
        !            50:                        fprintf(stderr, "usage: df [ -il ] [ filsys... ]\n");
        !            51:                        exit(0);
        !            52:                }
        !            53:                argc--, argv++;
        !            54:        }
        !            55: 
        !            56:        if ((i=open("/etc/mtab", 0)) >= 0) {
        !            57:                read(i, mtab, sizeof mtab);     /* Probably returns short */
        !            58:                close(i);
        !            59:        }
        !            60:        printf("Filesystem  Mounted on  blocks\t  used\t  free");
        !            61:        if (lflag)
        !            62:                printf("\thardway");
        !            63:        printf("\t%% used");
        !            64:        if (iflag)
        !            65:                printf("\tiused\tifree\t%%iused");
        !            66:        putchar('\n');
        !            67:        if(argc <= 1) {
        !            68:                struct  fstab   *fsp;
        !            69:                if (setfsent() == 0)
        !            70:                        perror(FSTAB), exit(1);
        !            71:                while( (fsp = getfsent()) != 0){
        !            72:                        if (  (strcmp(fsp->fs_type, FSTAB_RW) != 0)
        !            73:                            &&(strcmp(fsp->fs_type, FSTAB_RO) != 0) )
        !            74:                                continue;
        !            75:                        if (root[0] == 0)
        !            76:                                strcpy(root, fsp->fs_spec);
        !            77:                        dfree(fsp->fs_spec);
        !            78:                }
        !            79:                endfsent();
        !            80:                exit(0);
        !            81:        }
        !            82: 
        !            83:        for(i=1; i<argc; i++) {
        !            84:                dfree(argv[i]);
        !            85:        }
        !            86: }
        !            87: 
        !            88: dfree(file)
        !            89: char *file;
        !            90: {
        !            91:        daddr_t i;
        !            92:        long    blocks;
        !            93:        long    free;
        !            94:        long    used;
        !            95:        long    hardway;
        !            96:        char    *mp;
        !            97:        struct  stat stbuf;
        !            98: 
        !            99:        if(stat(file, &stbuf) == 0 && (stbuf.st_mode&S_IFMT) != S_IFCHR
        !           100:          && (stbuf.st_mode&S_IFMT) != S_IFBLK) {
        !           101:                int mt = open("/etc/mtab", 0), len;
        !           102:                char *str = "/dev/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        !           103:                char mtab[32];
        !           104:                struct stat mstbuf;
        !           105:                while((len = read(mt, mtab, 32)) == 32) {
        !           106:                        read(mt, &str[5], 32);
        !           107:                        if(stat(str, &mstbuf) == 0 && mstbuf.st_rdev == stbuf.st_dev) {
        !           108:                                file = str;
        !           109:                                break;
        !           110:                        }
        !           111:                }
        !           112:                close(mt);
        !           113:                if(len == 0) {
        !           114:                        fprintf(stderr, "%s: mounted on unknown device\n", file);
        !           115:                        return;
        !           116:                }
        !           117:        }
        !           118:        fi = open(file, 0);
        !           119:        if(fi < 0) {
        !           120:                fprintf(stderr,"cannot open %s\n", file);
        !           121:                return;
        !           122:        }
        !           123:        sync();
        !           124:        bread(1L, (char *)&sblock, sizeof(sblock));
        !           125:        printf("%-12.12s%s", file, mp = mpath(file));
        !           126:        if (strlen(mp) < 4)
        !           127:                putchar('\t');
        !           128: 
        !           129:        blocks = (long) sblock.s_fsize - (long)sblock.s_isize;
        !           130:        free = sblock.s_tfree;
        !           131:        used = blocks - free;
        !           132: 
        !           133:        printf("\t%6ld", blocks);
        !           134:        printf("\t%6ld", used);
        !           135:        printf("\t%6ld", free);
        !           136:        if (lflag) {
        !           137:                hardway = 0;
        !           138:                while(alloc())
        !           139:                        hardway++;
        !           140:                printf("\t%6ld", free=hardway);
        !           141:        }
        !           142:        printf("\t%5.0f%%", (double) used / (double)blocks * 100.0);
        !           143:        if (iflag) {
        !           144:                int inodes = (sblock.s_isize - 2) * INOPB;
        !           145:                used = inodes - sblock.s_tinode;
        !           146:                printf("\t%5ld\t%5ld\t%5.0f%%", used, sblock.s_tinode, (double)used/(double)inodes*100.0);
        !           147:        }
        !           148:        printf("\n");
        !           149:        close(fi);
        !           150: }
        !           151: 
        !           152: daddr_t
        !           153: alloc()
        !           154: {
        !           155:        int i;
        !           156:        daddr_t b;
        !           157:        struct fblk buf;
        !           158: 
        !           159:        i = --sblock.s_nfree;
        !           160:        if(i<0 || i>=NICFREE) {
        !           161:                printf("bad free count, b=%D\n", blkno);
        !           162:                return(0);
        !           163:        }
        !           164:        b = sblock.s_free[i];
        !           165:        if(b == 0)
        !           166:                return(0);
        !           167:        if(b<sblock.s_isize || b>=sblock.s_fsize) {
        !           168:                printf("bad free block (%D)\n", b);
        !           169:                return(0);
        !           170:        }
        !           171:        if(sblock.s_nfree <= 0) {
        !           172:                bread(b, (char *)&buf, sizeof(buf));
        !           173:                blkno = b;
        !           174:                sblock.s_nfree = buf.df_nfree;
        !           175:                for(i=0; i<NICFREE; i++)
        !           176:                        sblock.s_free[i] = buf.df_free[i];
        !           177:        }
        !           178:        return(b);
        !           179: }
        !           180: 
        !           181: bread(bno, buf, cnt)
        !           182: daddr_t bno;
        !           183: char *buf;
        !           184: {
        !           185:        int n;
        !           186:        extern errno;
        !           187: 
        !           188:        lseek(fi, bno<<BSHIFT, 0);
        !           189:        if((n=read(fi, buf, cnt)) != cnt) {
        !           190:                printf("\nread error bno = %ld\n", bno);
        !           191:                printf("count = %d; errno = %d\n", n, errno);
        !           192:                exit(0);
        !           193:        }
        !           194: }
        !           195: 
        !           196: /*
        !           197:  * Given a name like /dev/rrp0h, returns the mounted path, like /usr.
        !           198:  */
        !           199: char *mpath(file)
        !           200: char *file;
        !           201: {
        !           202:        register int i;
        !           203: 
        !           204:        if (eq(file, root))
        !           205:                return "/";
        !           206:        for (i=0; i<NFS; i++)
        !           207:                if (eq(file, mtab[i].spec))
        !           208:                        return mtab[i].path;
        !           209:        return "";
        !           210: }
        !           211: 
        !           212: eq(f1, f2)
        !           213: char *f1, *f2;
        !           214: {
        !           215:        if (strncmp(f1, "/dev/", 5) == 0)
        !           216:                f1 += 5;
        !           217:        if (strncmp(f2, "/dev/", 5) == 0)
        !           218:                f2 += 5;
        !           219:        if (strcmp(f1, f2) == 0)
        !           220:                return 1;
        !           221:        if (*f1 == 'r' && strcmp(f1+1, f2) == 0)
        !           222:                return 1;
        !           223:        if (*f2 == 'r' && strcmp(f1, f2+1) == 0)
        !           224:                return 1;
        !           225:        if (*f1 == 'r' && *f2 == 'r' && strcmp(f1+1, f2+1) == 0)
        !           226:                return 1;
        !           227:        return 0;
        !           228: }

unix.superglobalmegacorp.com

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