From f793d5347f08bdb742e8ad8a2cc5a7d30e6bd623 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 27 Feb 2012 21:56:49 -0600 Subject: Upgrade yesno() and make cp -i use it. --- lib/lib.c | 15 +++++++-------- lib/lib.h | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/lib.c b/lib/lib.c index 3f04cb61..cc441f9d 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -792,20 +792,19 @@ void terminal_size(unsigned *x, unsigned *y) } // This should use a raw tty, fixit later. -int yesno(int def) +int yesno(char *prompt, int def) { - char buf[16]; + char buf; int i; for (i=0; i<3 && !isatty(i); i++); if (i == 3) return 1; - sprintf(buf, "(%c/%c):", def ? 'Y' : 'y', def ? 'n' : 'N'); - write(i, buf, 6); - while (read(i, buf, 1)) { - if (isspace(*buf)) break; - if (tolower(*buf) == 'y') return 1; - if (tolower(*buf) == 'n') return 0; + fdprintf(i, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N'); + while (read(i, &buf, 1)) { + if (isspace(buf)) break; + if (tolower(buf) == 'y') return 1; + if (tolower(buf) == 'n') return 0; } return def; } diff --git a/lib/lib.h b/lib/lib.h index a6959730..ab10eb73 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -109,7 +109,7 @@ void delete_tempfile(int fdin, int fdout, char **tempname); void replace_tempfile(int fdin, int fdout, char **tempname); void crc_init(unsigned int *crc_table, int little_endian); void terminal_size(unsigned *x, unsigned *y); -int yesno(int def); +int yesno(char *prompt, int def); void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid)); -- cgit v1.2.3