chiark / gitweb /
util: tighten on_tty() check a bit, also check stderr
authorLennart Poettering <lennart@poettering.net>
Tue, 5 Jun 2018 19:53:54 +0000 (21:53 +0200)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +0200)
Let's detect output redirection a bit better, cover both stdout and
stderr.

Fixes: #9192
src/basic/terminal-util.c

index 578fa4a57757f3fa3f3dd52d5813e0d225578941..f6dff6d3562404fb038eb9c94e019de1791191a3 100644 (file)
@@ -897,8 +897,17 @@ void reset_terminal_feature_caches(void) {
 }
 
 bool on_tty(void) {
+
+        /* We check both stdout and stderr, so that situations where pipes on the shell are used are reliably
+         * recognized, regardless if only the output or the errors are piped to some place. Since on_tty() is generally
+         * used to default to a safer, non-interactive, non-color mode of operation it's probably good to be defensive
+         * here, and check for both. Note that we don't check for STDIN_FILENO, because it should fine to use fancy
+         * terminal functionality when outputting stuff, even if the input is piped to us. */
+
         if (cached_on_tty < 0)
-                cached_on_tty = isatty(STDOUT_FILENO) > 0;
+                cached_on_tty =
+                        isatty(STDOUT_FILENO) > 0 &&
+                        isatty(STDERR_FILENO) > 0;
 
         return cached_on_tty;
 }