Annotation of researchv10dc/cmd/ncheck.c, revision 1.1

1.1     ! root        1: static char *sccsid = "@(#)ncheck.c    4.1 (Berkeley) 10/1/80";
        !             2: /*
        !             3:  * ncheck -- obtain file names from reading filesystem
        !             4:  */
        !             5: 
        !             6: #define        NI      4
        !             7: #define        NB      1000
        !             8: #define        HSIZE   8209
        !             9: 
        !            10: #include <stdio.h>
        !            11: #include <sys/param.h>
        !            12: #include <sys/inode.h>
        !            13: #include <sys/ino.h>
        !            14: #include <sys/dir.h>
        !            15: #include <sys/filsys.h>
        !            16: #include <sys/fblk.h>
        !            17: #include <sys/stat.h>
        !            18: 
        !            19: #define        BITFSBIT        64      /* should be in param.h */
        !            20: #define        BIGINOPB        INOPB(BITFSBIT)
        !            21: #define        BIGBSIZE        BSIZE(BITFSBIT)
        !            22: #define        BIGNINDIR       NINDIR(BITFSBIT)
        !            23: #define        BIGNDIR (BIGBSIZE/sizeof(struct direct))
        !            24: 
        !            25: #define        NDIR(dev)       (BSIZE(dev)/sizeof(struct direct))
        !            26: 
        !            27: struct filsys  sblock;
        !            28: struct stat    status;
        !            29: #define        dev     status.st_rdev
        !            30: struct dinode  itab[BIGINOPB*NI];
        !            31: daddr_t        iaddr[NADDR];
        !            32: ino_t  ilist[NB];
        !            33: unsigned short imodes[NB];
        !            34: struct htab
        !            35: {
        !            36:        ino_t   h_ino;
        !            37:        ino_t   h_pino;
        !            38:        char    h_name[DIRSIZ];
        !            39: } htab[HSIZE];
        !            40: 
        !            41: int    aflg;
        !            42: int    sflg;
        !            43: int    fi;
        !            44: long ino;
        !            45: int    nhent;
        !            46: int    nxfile;
        !            47: int    bigflag;
        !            48: 
        !            49: int    nerror;
        !            50: daddr_t        bmap();
        !            51: long   atol();
        !            52: struct htab *lookup();
        !            53: 
        !            54: main(argc, argv)
        !            55: char *argv[];
        !            56: {
        !            57:        register i;
        !            58:        long n;
        !            59: 
        !            60:        setbuf(stdout, NULL);
        !            61:        nochk(1,0);     /* secure unix */
        !            62:        nochk(2,0);
        !            63:        while (--argc) {
        !            64:                argv++;
        !            65:                if (**argv=='-')
        !            66:                switch ((*argv)[1]) {
        !            67: 
        !            68:                case 'a':
        !            69:                        aflg++;
        !            70:                        continue;
        !            71: 
        !            72:                case 'i':
        !            73:                        for(i=0; i<NB; i++) {
        !            74:                                n = atol(argv[1]);
        !            75:                                if(n == 0)
        !            76:                                        break;
        !            77:                                ilist[i] = n;
        !            78:                                argv++;
        !            79:                                argc--;
        !            80:                        }
        !            81:                        nxfile = i;
        !            82:                        ilist[nxfile] = 0;
        !            83:                        continue;
        !            84: 
        !            85:                case 's':
        !            86:                        sflg++;
        !            87:                        continue;
        !            88: 
        !            89:                case 'B':
        !            90:                        bigflag = BITFSBIT;
        !            91:                        continue;
        !            92: 
        !            93:                default:
        !            94:                        fprintf(stderr, "ncheck: bad flag %c\n", (*argv)[1]);
        !            95:                        nerror++;
        !            96:                }
        !            97:                check(*argv);
        !            98:                if(sflg) {
        !            99:                        nxfile = 0;
        !           100:                        ilist[nxfile] = 0;
        !           101:                }
        !           102:        }
        !           103:        return(nerror);
        !           104: }
        !           105: 
        !           106: check(file)
        !           107: char *file;
        !           108: {
        !           109:        register i, j;
        !           110:        long mino;
        !           111:        register nchunk;
        !           112: 
        !           113:        fi = open(file, 0);
        !           114:        if(fi < 0) {
        !           115:                fprintf(stderr, "ncheck: cannot open %s\n", file);
        !           116:                nerror++;
        !           117:                return;
        !           118:        }
        !           119:        if (fstat(fi, &status) < 0) {
        !           120:                fprintf(stderr, "ncheck: cannot stat %s\n", file);
        !           121:                nerror++;
        !           122:                close(fi);
        !           123:                return;
        !           124:        }
        !           125:        if ((status.st_mode & S_IFMT) == S_IFREG)
        !           126:                dev = makedev(0, bigflag);
        !           127:        nhent = 0;
        !           128:        printf("%s:\n", file);
        !           129:        sync();
        !           130:        bread((daddr_t)1, (char *)&sblock, BSIZE(dev));
        !           131:        mino = (sblock.s_isize-2) * INOPB(dev);
        !           132:        nchunk = INOPB(dev)*NI;
        !           133:        ino = 0;
        !           134:        for(i=2;; i+=NI) {
        !           135:                if(ino >= mino)
        !           136:                        break;
        !           137:                bread((daddr_t)i, (char *)itab, BSIZE(dev)*NI);
        !           138:                for(j=0; j<nchunk; j++) {
        !           139:                        if(ino >= mino)
        !           140:                                break;
        !           141:                        ino++;
        !           142:                        pass1(&itab[j]);
        !           143:                }
        !           144:        }
        !           145:        ilist[nxfile] = 0;
        !           146:        ino = 0;
        !           147:        for(i=2;; i+=NI) {
        !           148:                if(ino >= mino)
        !           149:                        break;
        !           150:                bread((daddr_t)i, (char *)itab, BSIZE(dev)*NI);
        !           151:                for(j=0; j<nchunk; j++) {
        !           152:                        if(ino >= mino)
        !           153:                                break;
        !           154:                        ino++;
        !           155:                        pass2(&itab[j]);
        !           156:                }
        !           157:        }
        !           158:        ino = 0;
        !           159:        for(i=2;; i+=NI) {
        !           160:                if(ino >= mino)
        !           161:                        break;
        !           162:                bread((daddr_t)i, (char *)itab, BSIZE(dev)*NI);
        !           163:                for(j=0; j<nchunk; j++) {
        !           164:                        if(ino >= mino)
        !           165:                                break;
        !           166:                        ino++;
        !           167:                        pass3(&itab[j]);
        !           168:                }
        !           169:        }
        !           170: }
        !           171: 
        !           172: pass1(ip)
        !           173: register struct dinode *ip;
        !           174: {
        !           175:        register struct htab *  hp;
        !           176:        int filtyp = ip->di_mode & IFMT;
        !           177: 
        !           178:        if(filtyp != IFDIR) {
        !           179:                if (sflg==0 || nxfile>=NB)
        !           180:                        return;
        !           181:                if(filtyp==IFBLK || filtyp==IFCHR || 
        !           182:                   ip->di_mode&(ISUID|ISGID) && !(ip->di_mode&ISYNC) 
        !           183: #ifdef lb_t
        !           184:                   || ip->di_label.lb_t /* secure unix */
        !           185: #endif
        !           186:                  ) {
        !           187:                        ilist[nxfile] = ino;
        !           188:                        imodes[nxfile++] = ip->di_mode;
        !           189:                }
        !           190:                return;
        !           191:        }
        !           192:        lookup(ino, 1);
        !           193: }
        !           194: 
        !           195: pass2(ip)
        !           196: register struct dinode *ip;
        !           197: {
        !           198:        struct direct dbuf[BIGNDIR];
        !           199:        long doff;
        !           200:        struct direct *dp;
        !           201:        register i, j;
        !           202:        int k;
        !           203:        struct htab *hp;
        !           204:        daddr_t d;
        !           205:        ino_t kno;
        !           206: 
        !           207:        if((ip->di_mode&IFMT) != IFDIR)
        !           208:                return;
        !           209:        l3tol(iaddr, ip->di_addr, NADDR);
        !           210:        doff = 0;
        !           211:        for(i=0;; i++) {
        !           212:                if(doff >= ip->di_size)
        !           213:                        break;
        !           214:                d = bmap(i);
        !           215:                if(d == 0)
        !           216:                        break;
        !           217:                bread(d, (char *)dbuf, BSIZE(dev));
        !           218:                for(j=0; j<NDIR(dev); j++) {
        !           219:                        if(doff >= ip->di_size)
        !           220:                                break;
        !           221:                        doff += sizeof(struct direct);
        !           222:                        dp = dbuf+j;
        !           223:                        kno = dp->d_ino;
        !           224:                        if(kno == 0)
        !           225:                                continue;
        !           226:                        hp = lookup(kno, 0);
        !           227:                        if(hp == 0)
        !           228:                                continue;
        !           229:                        if(dotname(dp))
        !           230:                                continue;
        !           231:                        hp->h_pino = ino;
        !           232:                        for(k=0; k<DIRSIZ; k++)
        !           233:                                hp->h_name[k] = dp->d_name[k];
        !           234:                }
        !           235:        }
        !           236: }
        !           237: 
        !           238: pass3(ip)
        !           239: register struct dinode *ip;
        !           240: {
        !           241:        struct direct dbuf[BIGNDIR];
        !           242:        long doff;
        !           243:        register struct direct *dp;
        !           244:        register i, j;
        !           245:        int k;
        !           246:        daddr_t d;
        !           247:        ino_t kno;
        !           248:        struct htab *   hp;
        !           249: 
        !           250:        if((ip->di_mode&IFMT) != IFDIR)
        !           251:                return;
        !           252:        l3tol(iaddr, ip->di_addr, NADDR);
        !           253:        doff = 0;
        !           254:        for(i=0;; i++) {
        !           255:                if(doff >= ip->di_size)
        !           256:                        break;
        !           257:                d = bmap(i);
        !           258:                if(d == 0)
        !           259:                        break;
        !           260:                bread(d, (char *)dbuf, BSIZE(dev));
        !           261:                for(j=0; j<NDIR(dev); j++) {
        !           262:                        if(doff >= ip->di_size)
        !           263:                                break;
        !           264:                        doff += sizeof(struct direct);
        !           265:                        dp = dbuf+j;
        !           266:                        kno = dp->d_ino;
        !           267:                        if(kno == 0)
        !           268:                                continue;
        !           269:                        if(aflg==0 && dotname(dp))
        !           270:                                continue;
        !           271:                        if(ilist[0] || sflg) {
        !           272:                                for(k=0; ilist[k] != 0; k++)
        !           273:                                        if(ilist[k] == kno)
        !           274:                                                goto pr;
        !           275:                                continue;
        !           276:                        }
        !           277:                pr:
        !           278:                        hp = lookup(kno, 0);
        !           279:                        printf("%u\t", kno);
        !           280:                        if (sflg && kno == ilist[k])
        !           281:                                printf("%o\t", imodes[k]);
        !           282:                        pname(ino, 0);
        !           283:                        printf("/%.14s", dp->d_name);
        !           284:                        if (hp)
        !           285:                                printf("/.");
        !           286:                        printf("\n");
        !           287:                }
        !           288:        }
        !           289: }
        !           290: 
        !           291: dotname(dp)
        !           292: register struct direct *dp;
        !           293: {
        !           294: 
        !           295:        if (dp->d_name[0]=='.')
        !           296:                if (dp->d_name[1]==0 || (dp->d_name[1]=='.' && dp->d_name[2]==0))
        !           297:                        return(1);
        !           298:        return(0);
        !           299: }
        !           300: 
        !           301: pname(i, lev)
        !           302: ino_t i;
        !           303: {
        !           304:        register struct htab *hp;
        !           305: 
        !           306:        if (i==ROOTINO)
        !           307:                return;
        !           308:        if ((hp = lookup(i, 0)) == 0) {
        !           309:                printf("???");
        !           310:                return;
        !           311:        }
        !           312:        if (lev > 10) {
        !           313:                printf("...");
        !           314:                return;
        !           315:        }
        !           316:        pname(hp->h_pino, ++lev);
        !           317:        printf("/%.14s", hp->h_name);
        !           318: }
        !           319: 
        !           320: struct htab *
        !           321: lookup(i, ef)
        !           322: ino_t i;
        !           323: {
        !           324:        register struct htab *hp;
        !           325: 
        !           326:        for (hp = &htab[i%HSIZE]; hp->h_ino;) {
        !           327:                if (hp->h_ino==i)
        !           328:                        return(hp);
        !           329:                if (++hp >= &htab[HSIZE])
        !           330:                        hp = htab;
        !           331:        }
        !           332:        if (ef==0)
        !           333:                return(0);
        !           334:        if (++nhent >= HSIZE) {
        !           335:                fprintf(stderr, "ncheck: out of core-- increase HSIZE\n");
        !           336:                exit(1);
        !           337:        }
        !           338:        hp->h_ino = i;
        !           339:        return(hp);
        !           340: }
        !           341: 
        !           342: bread(bno, buf, cnt)
        !           343: daddr_t bno;
        !           344: char *buf;
        !           345: {
        !           346:        register i;
        !           347: 
        !           348:        lseek(fi, bno*BSIZE(dev), 0);
        !           349:        if (read(fi, buf, cnt) != cnt) {
        !           350:                fprintf(stderr, "ncheck: read error %d\n", bno);
        !           351:                for(i=0; i<BSIZE(dev); i++)
        !           352:                        buf[i] = 0;
        !           353:        }
        !           354: }
        !           355: 
        !           356: daddr_t
        !           357: bmap(i)
        !           358: {
        !           359:        daddr_t ibuf[BIGNINDIR];
        !           360: 
        !           361:        if(i < NADDR-3)
        !           362:                return(iaddr[i]);
        !           363:        i -= NADDR-3;
        !           364:        if(i > NINDIR(dev)) {
        !           365:                fprintf(stderr, "ncheck: %u - huge directory\n", ino);
        !           366:                return((daddr_t)0);
        !           367:        }
        !           368:        bread(iaddr[NADDR-3], (char *)ibuf, BSIZE(dev));
        !           369:        return(ibuf[i]);
        !           370: }

unix.superglobalmegacorp.com

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