Official request: Please make GNU grep the default

Dag-Erling Smørgrav des at des.no
Mon Aug 23 15:18:51 UTC 2010


Dag-Erling Smørgrav <des at des.no> writes:
> No idea what causes it, but a quick grep (hah!) for qflag turns up the
> following horror:
>
>         /* Find out the correct return value according to the
>            results and the command line option. */
>         exit(c ? (notfound ? (qflag ? 0 : 2) : 0) : (notfound ? 2 : 1));
>
> which shows that -q *does* affect the exit code, but my brain refuses to
> try to understand that code.

My brain is in need of a break from $REALJOB.  POSIX says

EXIT STATUS

    The following exit values shall be returned:

     0
        One or more lines were selected.
     1
        No lines were selected.
    >1
        An error occurred.

CONSEQUENCES OF ERRORS

    If the -q option is specified, the exit status shall be zero if an
    input line is selected, even if an error was detected. Otherwise,
    default actions shall be performed.

I suppose c is supposed to indicate, in some manner, whether an error
occurred, but it's hard to tell; the code seems almost deliberately
obfuscated.  The name gives no clue whatsoever as to its meaning.  It is
incremented like a counter, but tested like a boolean.  Its value is
derived from the value returned by procfile(), but that value is also
named "c", and is derived from values returned by other functions which
I could not be bothered to track down.  In any case -

    c           notfound    qflag       result
    true        true        true        0
    true        true        false       2
    true        false       true        0
    true        false       false       0
    false       true        true        2
    false       true        false       2
    false       false       true        1
    false       false       false       1

By this point, my brain is tied into the shape of a pretzel, but it
looks like c might actually be a count of matching lines and notfound
might be an error flag.  I give it -10 for calling the count "c" instead
of "count" or "matches" (I'm being generous because "c" is the first
letter of "count"), another -10 for testing it as a boolean instead of
comparing it to 0, -1,000 for calling the error flag "notfound", and
-1,000,000 for writing code so convoluted that, even with the source
code in front of me, I had to reverse-engineer it to figure out what it
does.  I think that adds up to -1,001,020.

DES
-- 
Dag-Erling Smørgrav - des at des.no


More information about the freebsd-current mailing list