From 8481248b9fbddc6d5e6ff26eb23505ef13dc85f7 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2012 23:59:41 +0200 Subject: util: unify usage of on_tty() in util.c --- src/shared/pager.c | 2 +- src/shared/util.c | 16 +++++++++++----- src/shared/util.h | 1 + 3 files changed, 13 insertions(+), 6 deletions(-) (limited to 'src/shared') diff --git a/src/shared/pager.c b/src/shared/pager.c index 6799787e85..488a12c763 100644 --- a/src/shared/pager.c +++ b/src/shared/pager.c @@ -57,7 +57,7 @@ int pager_open(void) { if (!*pager || streq(pager, "cat")) return 0; - if (isatty(STDOUT_FILENO) <= 0) + if (!on_tty()) return 0; /* Determine and cache number of columns before we spawn the diff --git a/src/shared/util.c b/src/shared/util.c index 462b541b41..527a5800fe 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -2175,28 +2175,25 @@ int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) { } int ask(char *ret, const char *replies, const char *text, ...) { - bool on_tty; assert(ret); assert(replies); assert(text); - on_tty = isatty(STDOUT_FILENO); - for (;;) { va_list ap; char c; int r; bool need_nl = true; - if (on_tty) + if (on_tty()) fputs(ANSI_HIGHLIGHT_ON, stdout); va_start(ap, text); vprintf(text, ap); va_end(ap); - if (on_tty) + if (on_tty()) fputs(ANSI_HIGHLIGHT_OFF, stdout); fflush(stdout); @@ -3820,6 +3817,15 @@ void columns_cache_reset(int signum) { cached_columns = 0; } +bool on_tty(void) { + static int cached_on_tty = -1; + + if (_unlikely_(cached_on_tty < 0)) + cached_on_tty = isatty(STDOUT_FILENO) > 0; + + return cached_on_tty; +} + int fd_lines(int fd) { struct winsize ws; zero(ws); diff --git a/src/shared/util.h b/src/shared/util.h index 662c3d1f39..e19f76c1ea 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -385,6 +385,7 @@ int status_welcome(void); int fd_columns(int fd); unsigned columns(void); void columns_cache_reset(int _unused_ signum); +bool on_tty(void); int fd_lines(int fd); unsigned lines(void); -- cgit v1.2.3-54-g00ecf