From 9477abc3591905a20acbfe7b8ce7832617d72701 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Fri, 18 Mar 2011 11:03:28 -0400 Subject: pacman/util: flush terminal input before reading response Addresses FS#20538 Signed-off-by: Dave Reisner Signed-off-by: Dan McGee --- configure.ac | 5 +++-- src/pacman/util.c | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index 87330afc..32993029 100644 --- a/configure.ac +++ b/configure.ac @@ -154,7 +154,8 @@ AC_CHECK_LIB([gpgme], [gpgme_check_version], , # Checks for header files. AC_CHECK_HEADERS([fcntl.h glob.h libintl.h locale.h mntent.h string.h \ sys/ioctl.h sys/mount.h sys/param.h sys/statvfs.h \ - sys/time.h sys/types.h sys/ucred.h syslog.h wchar.h]) + sys/time.h sys/types.h sys/ucred.h syslog.h termios.h \ + wchar.h]) # Checks for typedefs, structures, and compiler characteristics. AC_C_INLINE @@ -173,7 +174,7 @@ AC_FUNC_GETMNTENT AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK AC_FUNC_MKTIME AC_CHECK_FUNCS([geteuid getmntinfo realpath regcomp strcasecmp \ - strndup strrchr strsep swprintf \ + strndup strrchr strsep swprintf tcflush \ wcwidth uname]) # For the diskspace code FS_STATS_TYPE diff --git a/src/pacman/util.c b/src/pacman/util.c index 558d6f8f..51bb0526 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -37,6 +37,9 @@ #include #include #include +#ifdef HAVE_TERMIOS_H +#include /* tcflush */ +#endif #include #include @@ -100,6 +103,18 @@ int needs_root(void) } } +/* discard unhandled input on the terminal's input buffer */ +static int flush_term_input(void) { +#ifdef HAVE_TCFLUSH + if(isatty(fileno(stdin))) { + return(tcflush(fileno(stdin), TCIFLUSH)); + } +#endif + + /* fail silently */ + return 0; +} + /* gets the current screen column width */ int getcols(void) { @@ -812,6 +827,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 +866,8 @@ int select_question(int count) break; } + flush_term_input(); + if(fgets(response, sizeof(response), stdin)) { strtrim(response); if(strlen(response) > 0) { @@ -891,6 +910,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) { -- cgit v1.2.3-55-g3dc8