X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=527a5800fec407d6cc096f66d22dc22cbfa6d7cf;hp=462b541b41d6d7560b9ec0b792eb1f801633079b;hb=8481248b9fbddc6d5e6ff26eb23505ef13dc85f7;hpb=28917d7dc711746795f7e6468c06c1983a5cdf53 diff --git a/src/shared/util.c b/src/shared/util.c index 462b541b4..527a5800f 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);