added type-rename operator
[unix-history] / usr / src / old / dcheck / dcheck.c
index 999d0f2..8604eab 100644 (file)
@@ -1,17 +1,22 @@
-static char *sccsid = "@(#)dcheck.c    1.5 (Berkeley) %G%";
+static char *sccsid = "@(#)dcheck.c    2.3 (Berkeley) %G%";
 /*
  * 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 <stdio.h>
+#ifndef SIMFS
+#include <sys/param.h>
+#include <sys/inode.h>
+#include <sys/fs.h>
+#include <dir.h>
+#else
 #include "../h/param.h"
 #include "../h/inode.h"
 #include "../h/param.h"
 #include "../h/inode.h"
-#include "../h/dir.h"
 #include "../h/fs.h"
 #include "../h/fs.h"
+#include "../h/ndir.h"
+#endif
+#include <stdio.h>
 
 union {
        struct  fs fs;
 
 union {
        struct  fs fs;
@@ -19,6 +24,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];
@@ -87,11 +98,6 @@ char *file;
                return;
        }
        nfiles = sblock.fs_ipg * sblock.fs_ncg;
                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 +107,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 +116,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 +127,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 +175,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;