# HG changeset patch # User Rob Landley # Date 1354955132 21600 # Node ID 075eaff297f88b629e39848ca104546365ca5bb1 # Parent dc4a38a1327084fc6041b3462e8dd3bdc4363d4e 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.) diff -r dc4a38a13270 -r 075eaff297f8 lib/lib.c --- a/lib/lib.c Fri Dec 07 06:26:21 2012 -0600 +++ b/lib/lib.c Sat Dec 08 02:25:32 2012 -0600 @@ -958,24 +958,17 @@ } } -// 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(stderr, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N'); + fflush(stderr); + while (fread(&buf, 1, 1, stdin)) { + int new; - 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; - - break; + if (isspace(buf)) break; + if (-1 != (new = stridx("ny", tolower(buf)))) def = new; } return def;