changeset 738:075eaff297f8

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.)
author Rob Landley <rob@landley.net>
date Sat, 08 Dec 2012 02:25:32 -0600
parents dc4a38a13270
children 451d7e91232e
files lib/lib.c
diffstat 1 files changed, 6 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- 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;