date and time created 83/02/18 00:41:40 by mckusick
[unix-history] / usr / src / sbin / ncheck / ncheck.c
index 570288a..81a0be1 100644 (file)
@@ -1,33 +1,52 @@
-static char *sccsid = "@(#)ncheck.c    1.6 (Berkeley) %G%";
+static char *sccsid = "@(#)ncheck.c    2.2 (Berkeley) %G%";
 /*
  * ncheck -- obtain file names from reading filesystem
  */
 
 #define        NB              500
 #define        HSIZE           2503
 /*
  * ncheck -- obtain file names from reading filesystem
  */
 
 #define        NB              500
 #define        HSIZE           2503
-#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>
+#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"
+#endif
+#include <stdio.h>
+#include <dir.h>
 
 struct fs      sblock;
 struct dinode  itab[MAXIPG];
 struct         dinode  *gip;
 
 struct fs      sblock;
 struct dinode  itab[MAXIPG];
 struct         dinode  *gip;
-ino_t  ilist[NB];
+struct ilist {
+       ino_t   ino;
+       u_short mode;
+       short   uid;
+       short   gid;
+} ilist[NB];
 struct htab
 {
        ino_t   h_ino;
        ino_t   h_pino;
 struct htab
 {
        ino_t   h_ino;
        ino_t   h_pino;
-       char    h_name[DIRSIZ];
+       char    *h_name;
 } htab[HSIZE];
 } htab[HSIZE];
+char strngtab[30 * HSIZE];
+int strngloc;
+
+struct dirstuff {
+       int loc;
+       struct dinode *ip;
+       char dbuf[MAXBSIZE];
+};
 
 int    aflg;
 int    sflg;
 
 int    aflg;
 int    sflg;
+int    iflg; /* number of inodes being searched for */
+int    mflg;
 int    fi;
 ino_t  ino;
 int    nhent;
 int    fi;
 ino_t  ino;
 int    nhent;
@@ -55,17 +74,21 @@ main(argc, argv)
                        continue;
 
                case 'i':
                        continue;
 
                case 'i':
-                       for(i=0; i<NB; i++) {
+                       for(iflg=0; iflg<NB; iflg++) {
                                n = atol(argv[1]);
                                if(n == 0)
                                        break;
                                n = atol(argv[1]);
                                if(n == 0)
                                        break;
-                               ilist[i] = n;
-                               nxfile = i;
+                               ilist[iflg].ino = n;
+                               nxfile = iflg;
                                argv++;
                                argc--;
                        }
                        continue;
 
                                argv++;
                                argc--;
                        }
                        continue;
 
+               case 'm':
+                       mflg++;
+                       continue;
+
                case 's':
                        sflg++;
                        continue;
                case 's':
                        sflg++;
                        continue;
@@ -100,29 +123,24 @@ check(file)
                nerror++;
                return;
        }
                nerror++;
                return;
        }
-       nfiles = sblock.fs_ipg * sblock.fs_ncg;
-       if (nfiles > 65535) {
-               printf("%s: %d is a preposterous number of files\n",
-                   file, nfiles);
-               nerror++;
-               return;
-       }
        ino = 0;
        for (c = 0; c < sblock.fs_ncg; c++) {
                bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
                    sblock.fs_ipg * sizeof (struct dinode));
        ino = 0;
        for (c = 0; c < sblock.fs_ncg; c++) {
                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]);
+               for(j = 0; j < sblock.fs_ipg; j++) {
+                       if (itab[j].di_mode != 0)
+                               pass1(&itab[j]);
                        ino++;
                }
        }
                        ino++;
                }
        }
-       ilist[nxfile+1] = 0;
+       ilist[nxfile+1].ino = 0;
        ino = 0;
        for (c = 0; c < sblock.fs_ncg; c++) {
                bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
                    sblock.fs_ipg * sizeof (struct dinode));
        ino = 0;
        for (c = 0; c < sblock.fs_ncg; c++) {
                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]);
+               for(j = 0; j < sblock.fs_ipg; j++) {
+                       if (itab[j].di_mode != 0)
+                               pass2(&itab[j]);
                        ino++;
                }
        }
                        ino++;
                }
        }
@@ -130,23 +148,43 @@ check(file)
        for (c = 0; c < sblock.fs_ncg; c++) {
                bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
                    sblock.fs_ipg * sizeof (struct dinode));
        for (c = 0; c < sblock.fs_ncg; c++) {
                bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
                    sblock.fs_ipg * sizeof (struct dinode));
-               for(j=0; j<sblock.fs_ipg; j++) {
-                       pass3(&itab[j]);
+               for(j = 0; j < sblock.fs_ipg; j++) {
+                       if (itab[j].di_mode != 0)
+                               pass3(&itab[j]);
                        ino++;
                }
        }
                        ino++;
                }
        }
+       close(fi);
+       for (i = 0; i < HSIZE; i++)
+               htab[i].h_ino = 0;
+       for (i = iflg; i < NB; i++)
+               ilist[i].ino = 0;
+       nxfile = iflg;
 }
 
 pass1(ip)
        register struct dinode *ip;
 {
 }
 
 pass1(ip)
        register struct dinode *ip;
 {
-       if((ip->di_mode & IFMT) != IFDIR) {
+       int i;
+
+       if (mflg)
+               for (i = 0; i < iflg; i++)
+                       if (ino == ilist[i].ino) {
+                               ilist[i].mode = ip->di_mode;
+                               ilist[i].uid = ip->di_uid;
+                               ilist[i].gid = ip->di_gid;
+                       }
+       if ((ip->di_mode & IFMT) != IFDIR) {
                if (sflg==0 || nxfile>=NB)
                        return;
                if ((ip->di_mode&IFMT)==IFBLK || (ip->di_mode&IFMT)==IFCHR
                if (sflg==0 || nxfile>=NB)
                        return;
                if ((ip->di_mode&IFMT)==IFBLK || (ip->di_mode&IFMT)==IFCHR
-                 || ip->di_mode&(ISUID|ISGID))
-                       ilist[nxfile++] = ino;
+                 || ip->di_mode&(ISUID|ISGID)) {
+                       ilist[nxfile].ino = ino;
+                       ilist[nxfile].mode = ip->di_mode;
+                       ilist[nxfile].uid = ip->di_uid;
+                       ilist[nxfile++].gid = ip->di_gid;
                        return;
                        return;
+               }
        }
        lookup(ino, 1);
 }
        }
        lookup(ino, 1);
 }
@@ -154,92 +192,91 @@ pass1(ip)
 pass2(ip)
        register struct dinode *ip;
 {
 pass2(ip)
        register struct dinode *ip;
 {
-       struct direct dbuf[MAXNDIR];
-       long doff;
-       struct direct *dp;
-       register i, j;
-       int k;
+       register struct direct *dp;
+       struct dirstuff dirp;
        struct htab *hp;
        struct htab *hp;
-       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, sizeof(dbuf));
-               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;
-                       hp = lookup(kno, 0);
-                       if(hp == 0)
-                               continue;
-                       if(dotname(dp))
-                               continue;
-                       hp->h_pino = ino;
-                       for(k=0; k<DIRSIZ; k++)
-                               hp->h_name[k] = dp->d_name[k];
-               }
+       for (dp = readdir(&dirp); dp != NULL; dp = readdir(&dirp)) {
+               if(dp->d_ino == 0)
+                       continue;
+               hp = lookup(dp->d_ino, 0);
+               if(hp == 0)
+                       continue;
+               if(dotname(dp))
+                       continue;
+               hp->h_pino = ino;
+               hp->h_name = &strngtab[strngloc];
+               strngloc += strlen(dp->d_name) + 1;
+               strcpy(hp->h_name, dp->d_name);
        }
 }
 
 pass3(ip)
        register struct dinode *ip;
 {
        }
 }
 
 pass3(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, sizeof(dbuf));
-               for(j=0; j < NDIR(&sblock); j++) {
-                       if(doff >= ip->di_size)
+       for(dp = readdir(&dirp); dp != NULL; dp = readdir(&dirp)) {
+               if(aflg==0 && dotname(dp))
+                       continue;
+               if(sflg == 0 && iflg == 0)
+                       goto pr;
+               for(k = 0; ilist[k].ino != 0; k++)
+                       if(ilist[k].ino == dp->d_ino)
                                break;
                                break;
-                       doff += sizeof(struct direct);
-                       dp = dbuf+j;
-                       kno = dp->d_ino;
-                       if(kno == 0)
-                               continue;
-                       if(aflg==0 && dotname(dp))
-                               continue;
-                       if(ilist[0] == 0)
-                               goto pr;
-                       for(k=0; ilist[k] != 0; k++)
-                               if(ilist[k] == kno)
-                                       goto pr;
+               if (ilist[k].ino == 0)
                        continue;
                        continue;
-               pr:
-                       printf("%u      ", kno);
-                       pname(ino, 0);
-                       printf("/%.14s", dp->d_name);
-                       if (lookup(kno, 0))
-                               printf("/.");
-                       printf("\n");
+               if (mflg)
+                       printf("mode %-6o uid %-5d gid %-5d ino ",
+                           ilist[k].mode, ilist[k].uid, ilist[k].gid);
+       pr:
+               printf("%-5u\t", dp->d_ino);
+               pname(ino, 0);
+               printf("/%s", dp->d_name);
+               if (lookup(dp->d_ino, 0))
+                       printf("/.");
+               printf("\n");
+       }
+}
+
+/*
+ * 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);
        }
 }
 
        }
 }
 
@@ -248,7 +285,8 @@ dotname(dp)
 {
 
        if (dp->d_name[0]=='.')
 {
 
        if (dp->d_name[0]=='.')
-               if (dp->d_name[1]==0 || (dp->d_name[1]=='.' && dp->d_name[2]==0))
+               if (dp->d_name[1]==0 ||
+                  (dp->d_name[1]=='.' && dp->d_name[2]==0))
                        return(1);
        return(0);
 }
                        return(1);
        return(0);
 }
@@ -270,7 +308,7 @@ pname(i, lev)
                return;
        }
        pname(hp->h_pino, ++lev);
                return;
        }
        pname(hp->h_pino, ++lev);
-       printf("/%.14s", hp->h_name);
+       printf("/%s", hp->h_name);
 }
 
 struct htab *
 }
 
 struct htab *