Author: kevans
Date: Thu Jun  7 18:27:58 2018
New Revision: 334806
URL: https://svnweb.freebsd.org/changeset/base/334806

Log:
  bsdgrep(1): Do some less dirty things with return types
  
  Neither procfile nor grep_tree return anything meaningful to their callers.
  None of the callers actually care about how many lines were matched in all
  of the files they processed; it's all about "did anything match?"
  
  This is generally just a light refactoring to remind me of what actually
  matters as I'm rewriting these bits to care less about 'stuff'.

Modified:
  head/usr.bin/grep/grep.c
  head/usr.bin/grep/grep.h
  head/usr.bin/grep/util.c

Modified: head/usr.bin/grep/grep.c
==============================================================================
--- head/usr.bin/grep/grep.c    Thu Jun  7 18:24:25 2018        (r334805)
+++ head/usr.bin/grep/grep.c    Thu Jun  7 18:27:58 2018        (r334806)
@@ -339,6 +339,7 @@ main(int argc, char *argv[])
        long long l;
        unsigned int aargc, eargc, i;
        int c, lastc, needpattern, newarg, prevoptind;
+       bool matched;
 
        setlocale(LC_ALL, "");
 
@@ -725,15 +726,16 @@ main(int argc, char *argv[])
                exit(!procfile("-"));
 
        if (dirbehave == DIR_RECURSE)
-               c = grep_tree(aargv);
+               matched = grep_tree(aargv);
        else
-               for (c = 0; aargc--; ++aargv) {
+               for (matched = false; aargc--; ++aargv) {
                        if ((finclude || fexclude) && !file_matching(*aargv))
                                continue;
-                       c+= procfile(*aargv);
+                       if (procfile(*aargv))
+                               matched = true;
                }
 
        /* Find out the correct return value according to the
           results and the command line option. */
-       exit(c ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
+       exit(matched ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
 }

Modified: head/usr.bin/grep/grep.h
==============================================================================
--- head/usr.bin/grep/grep.h    Thu Jun  7 18:24:25 2018        (r334805)
+++ head/usr.bin/grep/grep.h    Thu Jun  7 18:27:58 2018        (r334806)
@@ -125,8 +125,8 @@ extern char  re_error[RE_ERROR_BUF + 1];    /* Seems big 
 
 /* util.c */
 bool    file_matching(const char *fname);
-int     procfile(const char *fn);
-int     grep_tree(char **argv);
+bool    procfile(const char *fn);
+bool    grep_tree(char **argv);
 void   *grep_malloc(size_t size);
 void   *grep_calloc(size_t nmemb, size_t size);
 void   *grep_realloc(void *ptr, size_t size);

Modified: head/usr.bin/grep/util.c
==============================================================================
--- head/usr.bin/grep/util.c    Thu Jun  7 18:24:25 2018        (r334805)
+++ head/usr.bin/grep/util.c    Thu Jun  7 18:27:58 2018        (r334806)
@@ -139,16 +139,17 @@ dir_matching(const char *dname)
  * Processes a directory when a recursive search is performed with
  * the -R option.  Each appropriate file is passed to procfile().
  */
-int
+bool
 grep_tree(char **argv)
 {
        FTS *fts;
        FTSENT *p;
        int c, fts_flags;
-       bool ok;
+       bool matched, ok;
        const char *wd[] = { ".", NULL };
 
        c = fts_flags = 0;
+       matched = false;
 
        switch(linkbehave) {
        case LINK_EXPLICIT:
@@ -195,14 +196,14 @@ grep_tree(char **argv)
                        if (fexclude || finclude)
                                ok &= file_matching(p->fts_path);
 
-                       if (ok)
-                               c += procfile(p->fts_path);
+                       if (ok && procfile(p->fts_path))
+                               matched = true;
                        break;
                }
        }
 
        fts_close(fts);
-       return (c);
+       return (matched);
 }
 
 static void
@@ -288,7 +289,7 @@ procmatches(struct mprintc *mc, struct parsec *pc, boo
  * Opens a file and processes it.  Each file is processed line-by-line
  * passing the lines to procline().
  */
-int
+bool
 procfile(const char *fn)
 {
        struct parsec pc;
@@ -296,7 +297,7 @@ procfile(const char *fn)
        struct file *f;
        struct stat sb;
        mode_t s;
-       int c, t;
+       int lines, t;
 
        if (strcmp(fn, "-") == 0) {
                fn = label != NULL ? label : errstr[1];
@@ -306,10 +307,10 @@ procfile(const char *fn)
                        /* Check if we need to process the file */
                        s = sb.st_mode & S_IFMT;
                        if (dirbehave == DIR_SKIP && s == S_IFDIR)
-                               return (0);
+                               return (false);
                        if (devbehave == DEV_SKIP && (s == S_IFIFO ||
                            s == S_IFCHR || s == S_IFBLK || s == S_IFSOCK))
-                               return (0);
+                               return (false);
                }
                f = grep_open(fn);
        }
@@ -317,7 +318,7 @@ procfile(const char *fn)
                file_err = true;
                if (!sflag)
                        warn("%s", fn);
-               return (0);
+               return (false);
        }
 
        pc.ln.file = grep_strdup(fn);
@@ -335,7 +336,7 @@ procfile(const char *fn)
                mc.doctx = true;
        mcount = mlimit;
 
-       for (c = 0;  c == 0 || !(lflag || qflag); ) {
+       for (lines = 0; lines == 0 || !(lflag || qflag); ) {
                /*
                 * XXX TODO: We need to revisit this in a chunking world. We're
                 * not going to be doing per-line statistics because of the
@@ -365,7 +366,7 @@ procfile(const char *fn)
                }
 
                if ((t = procline(&pc)) == 0)
-                       ++c;
+                       ++lines;
 
                /* Halt processing if we hit our match limit */
                if (!procmatches(&mc, &pc, t == 0))
@@ -378,19 +379,19 @@ procfile(const char *fn)
        if (cflag) {
                if (!hflag)
                        printf("%s:", pc.ln.file);
-               printf("%u\n", c);
+               printf("%u\n", lines);
        }
-       if (lflag && !qflag && c != 0)
+       if (lflag && !qflag && lines != 0)
                printf("%s%c", fn, nullflag ? 0 : '\n');
-       if (Lflag && !qflag && c == 0)
+       if (Lflag && !qflag && lines == 0)
                printf("%s%c", fn, nullflag ? 0 : '\n');
-       if (c && !cflag && !lflag && !Lflag &&
+       if (lines != 0 && !cflag && !lflag && !Lflag &&
            binbehave == BINFILE_BIN && f->binary && !qflag)
                printf(errstr[7], fn);
 
        free(pc.ln.file);
        free(f);
-       return (c);
+       return (lines != 0);
 }
 
 #ifdef WITH_INTERNAL_NOSPEC
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to