Netdev List
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: Stephen Hemminger <stephen@networkplumber.org>
Cc: netdev@vger.kernel.org, Till Maas <opensource@till.name>
Subject: [iproute PATCH 4/4] lib: Enable colored output only for TTYs
Date: Wed, 15 Aug 2018 11:06:10 +0200	[thread overview]
Message-ID: <20180815090610.16646-5-phil@nwl.cc> (raw)
In-Reply-To: <20180815090610.16646-1-phil@nwl.cc>

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 <phil@nwl.cc>
---
 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 <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <linux/if.h>
@@ -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;
 	}
-- 
2.18.0

  parent reply	other threads:[~2018-08-15 11:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-15  9:06 [iproute PATCH 0/4] A bunch of fixes regarding colored output Phil Sutter
2018-08-15  9:06 ` [iproute PATCH 1/4] tc: Fix typo in check for " Phil Sutter
2018-08-15  9:06 ` [iproute PATCH 2/4] bridge: Fix " Phil Sutter
2018-08-15  9:06 ` [iproute PATCH 3/4] Merge common code for conditionally " Phil Sutter
2018-08-15  9:06 ` Phil Sutter [this message]
2018-08-15 14:40   ` [iproute PATCH 4/4] lib: Enable colored output only for TTYs David Ahern
2018-08-15 15:04     ` Stephen Hemminger
2018-08-15 15:34       ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180815090610.16646-5-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=netdev@vger.kernel.org \
    --cc=opensource@till.name \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox