From: David Ahern Subject: Re: [iproute PATCH 4/4] lib: Enable colored output only for TTYs Date: Wed, 15 Aug 2018 08:40:20 -0600 Message-ID: <8f48ba0a-ba36-67f8-8dee-f0d86b72e8e0@gmail.com> References: <20180815090610.16646-1-phil@nwl.cc> <20180815090610.16646-5-phil@nwl.cc> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Cc: netdev@vger.kernel.org, Till Maas To: Phil Sutter , Stephen Hemminger Return-path: Received: from mail-pg1-f194.google.com ([209.85.215.194]:45438 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729300AbeHORct (ORCPT ); Wed, 15 Aug 2018 13:32:49 -0400 Received: by mail-pg1-f194.google.com with SMTP id f1-v6so589783pgq.12 for ; Wed, 15 Aug 2018 07:40:24 -0700 (PDT) In-Reply-To: <20180815090610.16646-5-phil@nwl.cc> Content-Language: en-US Sender: netdev-owner@vger.kernel.org List-ID: On 8/15/18 3:06 AM, Phil Sutter wrote: > Add an additional prerequisite to check_enable_color() to make sure > stdout actually points to an open TTY device. Otherwise calls like > > | ip -color a s >/tmp/foo > > will print color escape sequences into that file. > > Signed-off-by: Phil Sutter > --- > lib/color.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/lib/color.c b/lib/color.c > index edf96e5c6ecd7..500ba09682697 100644 > --- a/lib/color.c > +++ b/lib/color.c > @@ -3,6 +3,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -79,7 +80,7 @@ void enable_color(void) > > int check_enable_color(int color, int json) > { > - if (color && !json) { > + if (color && !json && isatty(fileno(stdout))) { > enable_color(); > return 0; > } > This also disables color sequence when the output is piped to a pager such as less which with the -R argument can handle it just fine. ie., the user needs to remove the color arg when that output is not wanted.