>From cac59bcc85ee168b56365655f9f27a95a9d56e0e Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Fri, 18 Mar 2011 11:03:28 -0400 Subject: [PATCH] pacman/util: flush terminal input before reading response Addresses FS#20538 Signed-off-by: Dave Reisner --- src/pacman/util.c | 17 +++++++++++++++++ src/pacman/util.h | 1 + 2 files changed, 18 insertions(+), 0 deletions(-) diff --git a/src/pacman/util.c b/src/pacman/util.c index 558d6f8..7fb4562 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -37,6 +37,7 @@ #include #include #include +#include /* tcflush */ #include #include @@ -100,6 +101,16 @@ int needs_root(void) } } +/* discard unhandled input on the terminal's input buffer */ +int flush_term_input(void) { + if(isatty(fileno(stdin))) { + return(tcflush(fileno(stdin), TCIFLUSH)); + } + + /* fail silently */ + return(0); +} + /* gets the current screen column width */ int getcols(void) { @@ -812,6 +823,8 @@ int multiselect_question(char *array, int count) break; } + flush_term_input(); + if(fgets(response, sizeof(response), stdin)) { strtrim(response); if(strlen(response) > 0) { @@ -849,6 +862,8 @@ int select_question(int count) break; } + flush_term_input(); + if(fgets(response, sizeof(response), stdin)) { strtrim(response); if(strlen(response) > 0) { @@ -891,6 +906,8 @@ static int question(short preset, char *fmt, va_list args) return preset; } + flush_term_input(); + if(fgets(response, sizeof(response), stdin)) { strtrim(response); if(strlen(response) == 0) { diff --git a/src/pacman/util.h b/src/pacman/util.h index 234a631..27cbf46 100644 --- a/src/pacman/util.h +++ b/src/pacman/util.h @@ -59,6 +59,7 @@ int str_cmp(const void *s1, const void *s2); void display_new_optdepends(pmpkg_t *oldpkg, pmpkg_t *newpkg); void display_optdepends(pmpkg_t *pkg); void print_packages(const alpm_list_t *packages); +int flush_term_input(void); void select_display(const alpm_list_t *pkglist); int select_question(int count); int multiselect_question(char *array, int count); -- 1.7.4.1