From db8eb323b3b8ded0eb75c0872563784ae4356e99 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 8 Dec 2012 02:25:32 -0600 Subject: Make yesno() always read from stdin and write to stderr. (If we need to find our tty, open /dev/tty, but existing users don't.) --- lib/lib.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 36f78ceb..f452bf3c 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -958,24 +958,17 @@ void terminal_size(unsigned *x, unsigned *y) } } -// This should use a raw tty, fixit later. int yesno(char *prompt, int def) { - FILE *fps[] = {stdin, stdout, stderr}; - int i; char buf; - for (i=0; i<3; i++) if (isatty(i)) break; - if (i == 3) return 1; - - fprintf(fps[i], "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N'); - fflush(fps[i]); - while (fread(&buf, 1, 1, fps[i])) { - if (tolower(buf) == 'y') def = 1; - if (tolower(buf) == 'n') def = 0; - else if (!isspace(buf)) continue; + fprintf(stderr, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N'); + fflush(stderr); + while (fread(&buf, 1, 1, stdin)) { + int new; - break; + if (isspace(buf)) break; + if (-1 != (new = stridx("ny", tolower(buf)))) def = new; } return def; -- cgit v1.2.3