[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#20837: Code for --color=auto

From: Paul J. Lucas
Subject: bug#20837: Code for --color=auto
Date: Wed, 17 Jun 2015 07:08:23 -0700

Hello -

I have my own C program where I also use SGR to produce color just like grep 
does.  I pulled the grep source (2.21) to see how —color=auto was implemented.  
In grep.c, lines 2440-2441, there is the code:

        if (color_option == 2)
          color_option = isatty (STDOUT_FILENO) && should_colorize ();

The thing about using isatty() is that it doesn’t work for the common case of 
piping the output of grep to less.  What about using fstat(STDOUT_FILENO) 
instead?  From testing (where “C” is an C program that outputs color using 
SGR), I’ve gotten:

        COMMAND  Should? isatty ISCHR ISFIFO ISREG
        ======== ======= ====== ===== ====== =====
        C          T      T      T     F      F
        C > file   F      F      F     F    >>T<<---- The interesting case.
        C | less   T      F      F     T      F

Hence, it would seem that we want to output color except when ISREG on the stat 
struct on stdout is T.  Wouldn’t this make better logic for —color=auto ?

- Paul

reply via email to

[Prev in Thread] Current Thread [Next in Thread]