Mercurial > hg > toybox
comparison lib/lib.c @ 554:997e016fbdf0
Using /dev/tty for yesno() is wrong because yes 'n' | cp -ial needs to work.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 20 Mar 2012 11:10:54 -0500 |
parents | 75da5d793fc8 |
children | 44abf4d901f3 |
comparison
equal
deleted
inserted
replaced
553:75da5d793fc8 | 554:997e016fbdf0 |
---|---|
792 } | 792 } |
793 | 793 |
794 // This should use a raw tty, fixit later. | 794 // This should use a raw tty, fixit later. |
795 int yesno(char *prompt, int def) | 795 int yesno(char *prompt, int def) |
796 { | 796 { |
797 FILE *fp = fopen("/dev/tty", "rw"); | 797 FILE *fps[] = {stdin, stdout, stderr}; |
798 int i; | |
798 char buf; | 799 char buf; |
799 | 800 |
800 if (!fp) return 1; | 801 for (i=0; i<3; i++) if (isatty(i)) break; |
801 | 802 if (i == 3) return 1; |
802 fprintf(fp, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N'); | 803 |
803 while (fread(&buf, 1, 1, fp)) { | 804 fprintf(fps[i], "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N'); |
805 fflush(fps[i]); | |
806 while (fread(&buf, 1, 1, fps[i])) { | |
804 if (tolower(buf) == 'y') def = 1; | 807 if (tolower(buf) == 'y') def = 1; |
805 if (tolower(buf) == 'n') def = 0; | 808 if (tolower(buf) == 'n') def = 0; |
806 else if (!isspace(buf)) continue; | 809 else if (!isspace(buf)) continue; |
807 | 810 |
808 break; | 811 break; |
809 } | 812 } |
810 fclose(fp); | |
811 | 813 |
812 return def; | 814 return def; |
813 } | 815 } |
814 | 816 |
815 // Execute a callback for each PID that matches a process name from a list. | 817 // Execute a callback for each PID that matches a process name from a list. |