Annotation of researchv10no/cmd/chuck/main.c, revision 1.1

1.1     ! root        1: #include "fs.h"
        !             2: #include "sys/ttyio.h"
        !             3: #include "ctype.h"
        !             4: 
        !             5: char *options = "ivwI:B:b:M:";
        !             6: int bsize = 4096;
        !             7: main(argc, argv)
        !             8: char **argv;
        !             9: {      int c;
        !            10:        extern char *optarg;
        !            11:        extern int optind;
        !            12: 
        !            13:        timer(0);
        !            14:        while((c = getopt(argc, argv, options)) != -1)
        !            15:        switch(c) {
        !            16:        default:
        !            17:                flags[c]++;
        !            18:                continue;
        !            19:        case '?':
        !            20:                fatal("unknown arg %c (%s)\n", c, options);
        !            21:        case 'b':
        !            22:                bsize = atoi(optarg);
        !            23:                continue;
        !            24:        case 'M':
        !            25:                flags[c]++;
        !            26:                pblk = atoi(optarg);
        !            27:                continue;
        !            28:        case 'I':
        !            29:                for(optind--; optind < argc && isdigit(argv[optind][0]); optind++)
        !            30:                        addi(atoi(argv[optind]));
        !            31:                continue;
        !            32:        case 'B':
        !            33:                for(optind--; optind < argc && isdigit(argv[optind][0]); optind++)
        !            34:                        addb(atoi(argv[optind]));
        !            35:                continue;
        !            36:        }
        !            37:        if(optind >= argc)
        !            38:                fatal("no file?\n");
        !            39:        if(optind < argc-1)
        !            40:                fatal("too many files %s\n", argv[optind]);
        !            41:        if(flags['i'])
        !            42:                flags['w']++;
        !            43:        file = argv[optind];
        !            44:        if(flags['M'])
        !            45:                mknew();
        !            46:        doit(file);
        !            47: }
        !            48: 
        !            49: doit(file)
        !            50: {
        !            51:        fd = -1;
        !            52:        if(flags['w']) {
        !            53:                fd = open(file, 2);
        !            54:                if(fd < 0) {
        !            55:                        pmesg("no write\n");
        !            56:                        flags['w'] = 0;
        !            57:                }
        !            58:        }
        !            59:        if(fd < 0)
        !            60:                fd = open(file, 0);
        !            61:        if(fd < 0) {
        !            62:                perror(file);
        !            63:                fatal("can't open file system\n");
        !            64:        }
        !            65:        firstsuper();   /* read it, sanity check, allocate imap, bmap */
        !            66:        timer("read");
        !            67:        scaninodes();   /* read all the inode blocks */
        !            68:        timer("inodes");
        !            69:        if(blkcnts[Ind3])
        !            70:                indblocks(Ind3, Ind2);
        !            71:        if(blkcnts[Ind2])       /* cope with indirect blocks */
        !            72:                indblocks(Ind2, Ind);
        !            73:        if(blkcnts[Ind])
        !            74:                indblocks(Ind, Data);
        !            75:        timer("indir");
        !            76:        dirblocks();    /* read all the directory blocks */
        !            77:        timer("dirs");
        !            78:        dochecks();     /* tree structure etc */
        !            79:        checksuper();   /* is the super block ok? */
        !            80:        repairs();
        !            81:        report();       /* say what happened somewhere */
        !            82:        timer("done");
        !            83:        if (exitcode)
        !            84:                pmesg("exit %d\n", exitcode);
        !            85:        exit(exitcode);
        !            86: }
        !            87: 
        !            88: pmesg(s, a, b, c, d, e, f, g, h, i)
        !            89: char *s;
        !            90: {      static char *p;
        !            91:        if(!p)
        !            92:                p = mbuf;
        !            93:        if(p == mbuf && file) {
        !            94:                sprintf(mbuf, "%s: ", file);
        !            95:                p = mbuf + strlen(mbuf);
        !            96:        }
        !            97:        sprintf(p, s, a, b, c, d, e, f, g, h, i);
        !            98:        write(2, mbuf, strlen(mbuf));
        !            99: }
        !           100: 
        !           101: fatal(s, a, b, c, d, e, f, g, h, i)
        !           102: char *s;
        !           103: {
        !           104:        pmesg(s, a, b, c, d, e, f, g, h, i);
        !           105:        exit(1);
        !           106: }
        !           107: 
        !           108: bread(loc, buf, cnt)
        !           109: char *buf;
        !           110: {      int i;
        !           111:        i = bsize *cnt;
        !           112:        if(lseek(fd, bsize*loc, 0) < 0 || read(fd, buf, i) != i)
        !           113:                return(-1);
        !           114:        return(0);
        !           115: }
        !           116: 
        !           117: bwrite(loc, buf, cnt)
        !           118: char *buf;
        !           119: {      int i = bsize * cnt;
        !           120:        if(lseek(fd, bsize*loc, 0) < 0 || write(fd, buf, i) != i)
        !           121:                return(-1);
        !           122:        return(0);
        !           123: }
        !           124: 
        !           125: char *
        !           126: itype(n)
        !           127: {
        !           128:        switch(n) {
        !           129:        default:
        !           130:                sprintf(xbuf, "?%d?", n);
        !           131:                return(xbuf);
        !           132:        case Unalloc:   return("Unalloc");
        !           133:        case Dir:       return("Dir");
        !           134:        case Lnk:       return("Lnk");
        !           135:        case Chr:       return("Chr");
        !           136:        case Blk:       return("Blk");
        !           137:        case Reg:       return("Reg");
        !           138:        case Weird:     return("Weird");
        !           139:        }
        !           140: }
        !           141: 
        !           142: char *
        !           143: btype(n)
        !           144: {
        !           145:        switch(n) {
        !           146:        default:
        !           147:                sprintf(xbuf, "?%d?", n);
        !           148:                return(xbuf);
        !           149:        case Unk:       return("Unk");
        !           150:        case Sblock:    return("Sblock");
        !           151:        case Inode:     return("Inode");
        !           152:        case Free:      return("Free");
        !           153:        case Data:      return("Data");
        !           154:        case Ind:       return("Ind");
        !           155:        case Ind2:      return("Ind2");
        !           156:        case Ind3:      return("Ind3");
        !           157:        case First:     return("First");
        !           158:        case Other:     return("Otherdir");
        !           159:        case Bits:      return("Bits");
        !           160:        case Ioerr:     return("Ioerr");
        !           161:        case Boot:      return("Boot");
        !           162:        case Bad:       return("Bad");
        !           163:        }
        !           164: }
        !           165: 
        !           166: addi(n)
        !           167: {
        !           168:        if(!ilen)
        !           169:                iarg = (int *) malloc((ilen = 10) * sizeof(int));
        !           170:        else if(iptr >= ilen) {
        !           171:                ilen *= 3;
        !           172:                iarg = (int *) realloc((char *)iarg, ilen * sizeof(int));
        !           173:        }
        !           174:        if(!iarg) {
        !           175:                pmesg("can't alloc iarg %d\n", n);
        !           176:                return;
        !           177:        }
        !           178:        iarg[iptr++] = n;
        !           179: }
        !           180: 
        !           181: addb(n)
        !           182: {
        !           183:        if(!blen)
        !           184:                barg = (int *) malloc((blen = 10) * sizeof(int));
        !           185:        else if(bptr >= blen) {
        !           186:                blen *= 3;
        !           187:                barg = (int *) realloc((char *)barg, blen * sizeof(int));
        !           188:        }
        !           189:        if(!barg) {
        !           190:                pmesg("can't alloc barg %d\n", n);
        !           191:                return;
        !           192:        }
        !           193:        barg[bptr++] = n;
        !           194: }
        !           195: 
        !           196: /* how do we flush stdin? */
        !           197: qry(s)
        !           198: char *s;
        !           199: {      int n;
        !           200:        pmesg(s);
        !           201:        ioctl(0, TIOCFLUSH, 0);
        !           202:        n = read(0, mbuf+40, 128-40);
        !           203:        if(n <= 0)
        !           204:                return(n);
        !           205:        return(mbuf[40]);
        !           206: }
        !           207: #include "sys/times.h"
        !           208: long ot, nt;
        !           209: struct tms otm, ntm;
        !           210: timer(s)
        !           211: char *s;
        !           212: {
        !           213:        if(ot == nt && nt == 0 || !flags['v']) {
        !           214:                (void) time(&ot);
        !           215:                times(&otm);
        !           216:                return;
        !           217:        }
        !           218:        (void) time(&nt);
        !           219:        times(&ntm);
        !           220:        pmesg("(time)%s %d %.3fu %.3fs\n", s, nt-ot,
        !           221:                (ntm.tms_utime-otm.tms_utime)/60.,
        !           222:                (ntm.tms_stime-otm.tms_stime)/60.);
        !           223:        ot=nt;
        !           224:        otm = ntm;
        !           225: }

unix.superglobalmegacorp.com

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