Two bugs. First, ttloop() was not reacting to an EOF on the
[unix-history] / usr / src / old / dcheck / dcheck.c
index a236cb1..d814ee5 100644 (file)
@@ -1,17 +1,30 @@
-static char *sccsid = "@(#)dcheck.c    1.4 (Berkeley) %G%";
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
+#ifndef lint
+static char sccsid[] = "@(#)dcheck.c   5.1 (Berkeley) %G%";
+#endif not lint
+
 /*
  * dcheck - check directory consistency
  */
 #define        NB      10
 /*
  * dcheck - check directory consistency
  */
 #define        NB      10
-#define        NDIR(fs)        ((fs)->fs_bsize/sizeof(struct direct))
-#define        MAXNDIR         (MAXBSIZE/sizeof(struct direct))
-#define        MAXNINDIR       (MAXBSIZE/sizeof(daddr_t))
+#define        MAXNINDIR       (MAXBSIZE / sizeof (daddr_t))
 
 
+#include <sys/param.h>
+#include <sys/inode.h>
+#include <sys/fs.h>
+#include <sys/dir.h>
 #include <stdio.h>
 #include <stdio.h>
-#include "../h/param.h"
-#include "../h/inode.h"
-#include "../h/dir.h"
-#include "../h/fs.h"
 
 union {
        struct  fs fs;
 
 union {
        struct  fs fs;
@@ -19,6 +32,12 @@ union {
 } fsun;
 #define        sblock  fsun.fs
 
 } fsun;
 #define        sblock  fsun.fs
 
+struct dirstuff {
+       int loc;
+       struct dinode *ip;
+       char dbuf[MAXBSIZE];
+};
+
 struct dinode  itab[MAXIPG];
 struct dinode  *gip;
 ino_t  ilist[NB];
 struct dinode  itab[MAXIPG];
 struct dinode  *gip;
 ino_t  ilist[NB];
@@ -80,18 +99,13 @@ char *file;
        headpr = 0;
        printf("%s:\n", file);
        sync();
        headpr = 0;
        printf("%s:\n", file);
        sync();
-       bread(SBLOCK, (char *)&sblock, MAXBSIZE);
+       bread(SBLOCK, (char *)&sblock, SBSIZE);
        if (sblock.fs_magic != FS_MAGIC) {
                printf("%s: not a file system\n", file);
                nerror++;
                return;
        }
        nfiles = sblock.fs_ipg * sblock.fs_ncg;
        if (sblock.fs_magic != FS_MAGIC) {
                printf("%s: not a file system\n", file);
                nerror++;
                return;
        }
        nfiles = sblock.fs_ipg * sblock.fs_ncg;
-       if (nfiles > 65535) {
-               printf("%s: preposterous number of files\n", file);
-               nerror++;
-               return;
-       }
        ecount = (ino_t *)malloc((nfiles+1) * sizeof (*ecount));
        if (ecount == 0) {
                printf("%s: not enough core for %d files\n", file, nfiles);
        ecount = (ino_t *)malloc((nfiles+1) * sizeof (*ecount));
        if (ecount == 0) {
                printf("%s: not enough core for %d files\n", file, nfiles);
@@ -101,7 +115,7 @@ char *file;
                ecount[i] = 0;
        ino = 0;
        for (c = 0; c < sblock.fs_ncg; c++) {
                ecount[i] = 0;
        ino = 0;
        for (c = 0; c < sblock.fs_ncg; c++) {
-               bread(fsbtodb(&sblock, cgimin(c, &sblock)), (char *)itab,
+               bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
                    sblock.fs_ipg * sizeof (struct dinode));
                for (j = 0; j < sblock.fs_ipg; j++) {
                        pass1(&itab[j]);
                    sblock.fs_ipg * sizeof (struct dinode));
                for (j = 0; j < sblock.fs_ipg; j++) {
                        pass1(&itab[j]);
@@ -110,7 +124,7 @@ char *file;
        }
        ino = 0;
        for (c = 0; c < sblock.fs_ncg; c++) {
        }
        ino = 0;
        for (c = 0; c < sblock.fs_ncg; c++) {
-               bread(fsbtodb(&sblock, cgimin(c, &sblock)), (char *)itab,
+               bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
                    sblock.fs_ipg * sizeof (struct dinode));
                for (j = 0; j < sblock.fs_ipg; j++) {
                        pass2(&itab[j]);
                    sblock.fs_ipg * sizeof (struct dinode));
                for (j = 0; j < sblock.fs_ipg; j++) {
                        pass2(&itab[j]);
@@ -121,49 +135,33 @@ char *file;
 }
 
 pass1(ip)
 }
 
 pass1(ip)
-register struct dinode *ip;
+       register struct dinode *ip;
 {
 {
-       struct direct dbuf[MAXNDIR];
-       long doff;
-       struct direct *dp;
-       register i, j;
+       register struct direct *dp;
+       struct dirstuff dirp;
        int k;
        int k;
-       daddr_t d;
-       ino_t kno;
 
        if((ip->di_mode&IFMT) != IFDIR)
                return;
 
        if((ip->di_mode&IFMT) != IFDIR)
                return;
+       dirp.loc = 0;
+       dirp.ip = ip;
        gip = ip;
        gip = ip;
-       doff = 0;
-       for(i=0;; i++) {
-               if(doff >= ip->di_size)
-                       break;
-               d = bmap(i);
-               if(d == 0)
-                       break;
-               bread(fsbtodb(&sblock, d), (char *)dbuf, sblock.fs_bsize);
-               for(j=0; j < NDIR(&sblock); j++) {
-                       if(doff >= ip->di_size)
-                               break;
-                       doff += sizeof(struct direct);
-                       dp = &dbuf[j];
-                       kno = dp->d_ino;
-                       if(kno == 0)
-                               continue;
-                       if(kno > nfiles || kno < ROOTINO) {
-                               printf("%d bad; %d/%.*s\n",
-                                   kno, ino, DIRSIZ, dp->d_name);
+       for (dp = readdir(&dirp); dp != NULL; dp = readdir(&dirp)) {
+               if(dp->d_ino == 0)
+                       continue;
+               if(dp->d_ino > nfiles || dp->d_ino < ROOTINO) {
+                       printf("%d bad; %d/%s\n",
+                           dp->d_ino, ino, dp->d_name);
+                       nerror++;
+                       continue;
+               }
+               for (k = 0; ilist[k] != 0; k++)
+                       if (ilist[k] == dp->d_ino) {
+                               printf("%d arg; %d/%s\n",
+                                    dp->d_ino, ino, dp->d_name);
                                nerror++;
                                nerror++;
-                               continue;
                        }
                        }
-                       for (k=0; ilist[k] != 0; k++)
-                               if (ilist[k]==kno) {
-                                       printf("%d arg; %d/%.*s\n",
-                                           kno, ino, DIRSIZ, dp->d_name);
-                                       nerror++;
-                               }
-                       ecount[kno]++;
-               }
+               ecount[dp->d_ino]++;
        }
 }
 
        }
 }
 
@@ -185,6 +183,35 @@ register struct dinode *ip;
            ecount[i], ip->di_nlink);
 }
 
            ecount[i], ip->di_nlink);
 }
 
+/*
+ * get next entry in a directory.
+ */
+struct direct *
+readdir(dirp)
+       register struct dirstuff *dirp;
+{
+       register struct direct *dp;
+       daddr_t lbn, d;
+
+       for(;;) {
+               if (dirp->loc >= dirp->ip->di_size)
+                       return NULL;
+               if ((lbn = lblkno(&sblock, dirp->loc)) == 0) {
+                       d = bmap(lbn);
+                       if(d == 0)
+                               return NULL;
+                       bread(fsbtodb(&sblock, d), dirp->dbuf,
+                           dblksize(&sblock, dirp->ip, lbn));
+               }
+               dp = (struct direct *)
+                   (dirp->dbuf + blkoff(&sblock, dirp->loc));
+               dirp->loc += dp->d_reclen;
+               if (dp->d_ino == 0)
+                       continue;
+               return (dp);
+       }
+}
+
 bread(bno, buf, cnt)
 daddr_t bno;
 char *buf;
 bread(bno, buf, cnt)
 daddr_t bno;
 char *buf;