1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-23 00:08:50 -05:00

pacman/util: flush terminal input before reading response

Addresses FS#20538

Conflicts:

	src/pacman/util.c

Signed-off-by: Dave Reisner <d@falconindy.com>
Signed-off-by: Dan McGee <dan@archlinux.org>
(cherry picked from commit 9477abc359)
This commit is contained in:
Dave Reisner 2011-03-18 11:03:28 -04:00 committed by Dan McGee
parent 07996bfac7
commit 6b57118c15
2 changed files with 24 additions and 2 deletions

View File

@ -171,7 +171,8 @@ AM_CONDITIONAL([HAVE_LIBFETCH], [test "x$ac_cv_lib_fetch_fetchParseURL" = "xyes"
# Checks for header files. # Checks for header files.
AC_CHECK_HEADERS([fcntl.h glob.h libintl.h locale.h mntent.h string.h \ 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/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. # Checks for typedefs, structures, and compiler characteristics.
AC_C_INLINE AC_C_INLINE
@ -190,7 +191,7 @@ AC_FUNC_GETMNTENT
AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
AC_FUNC_MKTIME AC_FUNC_MKTIME
AC_CHECK_FUNCS([geteuid getmntinfo realpath regcomp strcasecmp \ AC_CHECK_FUNCS([geteuid getmntinfo realpath regcomp strcasecmp \
strndup strrchr strsep swprintf \ strndup strrchr strsep swprintf tcflush \
wcwidth uname]) wcwidth uname])
# For the diskspace code # For the diskspace code
FS_STATS_TYPE FS_STATS_TYPE

View File

@ -37,6 +37,9 @@
#include <unistd.h> #include <unistd.h>
#include <limits.h> #include <limits.h>
#include <wchar.h> #include <wchar.h>
#ifdef HAVE_TERMIOS_H
#include <termios.h> /* tcflush */
#endif
#include <alpm.h> #include <alpm.h>
#include <alpm_list.h> #include <alpm_list.h>
@ -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 */ /* gets the current screen column width */
int getcols(int def) int getcols(int def)
{ {
@ -828,6 +843,8 @@ int multiselect_question(char *array, int count)
break; break;
} }
flush_term_input();
if(fgets(response, response_len, stdin)) { if(fgets(response, response_len, stdin)) {
const size_t response_incr = 64; const size_t response_incr = 64;
/* handle buffer not being large enough to read full line case */ /* handle buffer not being large enough to read full line case */
@ -887,6 +904,8 @@ int select_question(int count)
break; break;
} }
flush_term_input();
if(fgets(response, sizeof(response), stdin)) { if(fgets(response, sizeof(response), stdin)) {
strtrim(response); strtrim(response);
if(strlen(response) > 0) { if(strlen(response) > 0) {
@ -934,6 +953,8 @@ static int question(short preset, char *fmt, va_list args)
} }
fflush(stream); fflush(stream);
flush_term_input();
if(fgets(response, sizeof(response), stdin)) { if(fgets(response, sizeof(response), stdin)) {
strtrim(response); strtrim(response);
if(strlen(response) == 0) { if(strlen(response) == 0) {