Bell 32V development
[unix-history] / usr / src / cmd / fgrep.c
index 826c097..1c8eba9 100644 (file)
@@ -24,7 +24,7 @@ int   bflag, cflag, fflag, lflag, nflag, vflag, xflag;
 int    hflag   = 1;
 int    sflag;
 int    nfile;
 int    hflag   = 1;
 int    sflag;
 int    nfile;
-long   blkno;
+int    blkno;
 int    nsucc;
 long   tln;
 FILE   *wordf;
 int    nsucc;
 long   tln;
 FILE   *wordf;
@@ -117,7 +117,7 @@ char *file;
        register ccount;
        char buf[1024];
        int f;
        register ccount;
        char buf[1024];
        int f;
-       int failed;
+       int failed, ecnt;
        char *nlp;
        if (file) {
                if ((f = open(file, 0)) < 0) {
        char *nlp;
        if (file) {
                if ((f = open(file, 0)) < 0) {
@@ -130,7 +130,7 @@ char *file;
        failed = 0;
        lnum = 1;
        tln = 0;
        failed = 0;
        lnum = 1;
        tln = 0;
-       blkno = 0;
+       blkno = -1;
        p = buf;
        nlp = p;
        c = w;
        p = buf;
        nlp = p;
        c = w;
@@ -141,7 +141,7 @@ char *file;
                                if ((ccount = read(f, p, &buf[1024] - p)) <= 0) break;
                        }
                        else if ((ccount = read(f, p, 512)) <= 0) break;
                                if ((ccount = read(f, p, &buf[1024] - p)) <= 0) break;
                        }
                        else if ((ccount = read(f, p, 512)) <= 0) break;
-                       blkno += ccount;
+                       blkno++;
                }
                nstate:
                        if (c->inp == *p) {
                }
                nstate:
                        if (c->inp == *p) {
@@ -168,38 +168,42 @@ char *file;
                                else goto nstate;
                        }
                if (c->out) {
                                else goto nstate;
                        }
                if (c->out) {
+                       ecnt = 0;
                        while (*p++ != '\n') {
                        while (*p++ != '\n') {
-                               if (--ccount <= 0) {
+                               ecnt++;
+                                  if (--ccount <= 0) {
                                        if (p == &buf[1024]) p = buf;
                                        if (p > &buf[512]) {
                                                if ((ccount = read(f, p, &buf[1024] - p)) <= 0) break;
                                        }
                                        else if ((ccount = read(f, p, 512)) <= 0) break;
                                        if (p == &buf[1024]) p = buf;
                                        if (p > &buf[512]) {
                                                if ((ccount = read(f, p, &buf[1024] - p)) <= 0) break;
                                        }
                                        else if ((ccount = read(f, p, 512)) <= 0) break;
-                                       blkno += ccount;
-                               }
-                       }
-                       if ( (vflag && (failed == 0 || xflag == 0)) || (vflag == 0 && xflag && failed) )
-                               goto nomatch;
-       succeed:        nsucc = 1;
-                       if (cflag) tln++;
-                       else if (sflag)
-                               ;       /* ugh */
-                       else if (lflag) {
-                               printf("%s\n", file);
-                               close(f);
-                               return;
+                                       blkno++;
+                                  }
                        }
                        }
-                       else {
-                               if (nfile > 1 && hflag) printf("%s:", file);
-                               if (bflag) printf("%ld:", (blkno-ccount-1)/512);
-                               if (nflag) printf("%ld:", lnum);
-                               if (p <= nlp) {
-                                       while (nlp < &buf[1024]) putchar(*nlp++);
-                                       nlp = buf;
+                       if (vflag == 0) {
+                               if (xflag)
+                                       if (failed || ecnt > 1) goto nogood;
+               succeed:        nsucc = 1;
+                               if (cflag) tln++;
+                               else if (sflag)
+                                       ;       /* ugh */
+                               else if (lflag) {
+                                       printf("%s\n", file);
+                                       close(f);
+                                       return;
+                               }
+                               else {
+                                       if (nfile > 1 && hflag) printf("%s:", file);
+                                       if (bflag) printf("%d:", blkno);
+                                       if (nflag) printf("%ld:", lnum);
+                                       if (p <= nlp) {
+                                               while (nlp < &buf[1024]) putchar(*nlp++);
+                                               nlp = buf;
+                                       }
+                                       while (nlp < p) putchar(*nlp++);
                                }
                                }
-                               while (nlp < p) putchar(*nlp++);
                        }
                        }
-       nomatch:        lnum++;
+               nogood: lnum++;
                        nlp = p;
                        c = w;
                        failed = 0;
                        nlp = p;
                        c = w;
                        failed = 0;