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

unix.superglobalmegacorp.com

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