aboutsummaryrefslogtreecommitdiff
path: root/lib/lib.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-03-20 11:10:54 -0500
committerRob Landley <rob@landley.net>2012-03-20 11:10:54 -0500
commitf78c63a7035ceabb0dcb907cb5764b18da495f82 (patch)
tree9a216750279035c47b34d6087935957cd73f7bae /lib/lib.c
parentcf6bcb27fb3dc6d196adce31d774229ea5d56dec (diff)
downloadtoybox-f78c63a7035ceabb0dcb907cb5764b18da495f82.tar.gz
Using /dev/tty for yesno() is wrong because yes 'n' | cp -ial needs to work.
Diffstat (limited to 'lib/lib.c')
-rw-r--r--lib/lib.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/lib/lib.c b/lib/lib.c
index f4b81f5e..4190b065 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -794,20 +794,22 @@ void terminal_size(unsigned *x, unsigned *y)
// This should use a raw tty, fixit later.
int yesno(char *prompt, int def)
{
- FILE *fp = fopen("/dev/tty", "rw");
+ FILE *fps[] = {stdin, stdout, stderr};
+ int i;
char buf;
- if (!fp) return 1;
+ for (i=0; i<3; i++) if (isatty(i)) break;
+ if (i == 3) return 1;
- fprintf(fp, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N');
- while (fread(&buf, 1, 1, fp)) {
+ 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;
}
- fclose(fp);
return def;
}