Annotation of researchv8dc/cmd/df/odf.C, revision 1.1

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

unix.superglobalmegacorp.com

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