]> git.ipfire.org Git - thirdparty/wireguard-tools.git/commitdiff
terminal: specialize color_mode to stdout only
authorJason A. Donenfeld <Jason@zx2c4.com>
Tue, 21 Apr 2020 04:52:35 +0000 (22:52 -0600)
committerJason A. Donenfeld <Jason@zx2c4.com>
Tue, 21 Apr 2020 04:52:35 +0000 (22:52 -0600)
By specializing this to stdout, we can cache the isatty result.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
src/terminal.c
src/terminal.h

index ba885978b16839eb94c0c72248b8036ad451bee9..bea27ba4734bc3f8372ccee932810196a969dc34 100644 (file)
@@ -12,7 +12,7 @@
 #include <stdbool.h>
 #include <unistd.h>
 
-static bool color_mode(FILE *file)
+static bool color_mode(void)
 {
        static int mode = -1;
        const char *var;
@@ -25,17 +25,17 @@ static bool color_mode(FILE *file)
        else if (var && !strcmp(var, "never"))
                mode = false;
        else
-               return isatty(fileno(file));
+               mode = isatty(fileno(stdout));
        return mode;
 }
 
-static void filter_ansi(FILE *file, const char *fmt, va_list args)
+static void filter_ansi(const char *fmt, va_list args)
 {
        char *str = NULL;
        size_t len, i, j;
 
-       if (color_mode(file)) {
-               vfprintf(file, fmt, args);
+       if (color_mode()) {
+               vfprintf(stdout, fmt, args);
                return;
        }
 
@@ -55,7 +55,7 @@ static void filter_ansi(FILE *file, const char *fmt, va_list args)
                }
        }
        for (i = 0; i < len; i = j) {
-               fputs(&str[i], file);
+               fputs(&str[i], stdout);
                for (j = i + strlen(&str[i]); j < len; ++j) {
                        if (str[j] != '\0')
                                break;
@@ -70,15 +70,6 @@ void terminal_printf(const char *fmt, ...)
        va_list args;
 
        va_start(args, fmt);
-       filter_ansi(stdout, fmt, args);
-       va_end(args);
-}
-
-void terminal_fprintf(FILE *file, const char *fmt, ...)
-{
-       va_list args;
-
-       va_start(args, fmt);
-       filter_ansi(file, fmt, args);
+       filter_ansi(fmt, args);
        va_end(args);
 }
index e8cb5709b062c149c160449bfe4b10e9d1a2e132..58697fa86549c193ed85c2790413648b1e6c782c 100644 (file)
@@ -47,6 +47,5 @@
 #define TERMINAL_CLEAR_ALL     "\x1b[2J"
 
 void terminal_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
-void terminal_fprintf(FILE *file, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
 
 #endif