From f0e7e71589212959c66302f268094601f621f445 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Thu, 12 Dec 2024 22:14:40 +0200 Subject: [PATCH] Revert back to old way of flushing input buffer --- mcrcon.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index 68e1dbb..1949f00 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -38,10 +38,10 @@ #include #include #else + #include #include #include #include - #include #endif #define VERSION "0.8.0" @@ -794,14 +794,17 @@ char *utf8_getline(char *buf, int size, FILE *stream) } #endif +/* void flush_input(void) { #ifdef _WIN32 - FlushConsoleInputBuffer(console_handle); + // NOTE: Undefined behaviour in C standard but Windows allows it + fflush(stdin); #else - tcflush(STDIN_FILENO, TCIFLUSH); + __fpurge(stdin); #endif } +*/ // gets line from stdin and deals with rubbish left in the input buffer int get_line(char *buffer, int bsize) @@ -812,8 +815,6 @@ int get_line(char *buffer, int bsize) char *ret = fgets(buffer, bsize, stdin); #endif - flush_input(); - if (ret == NULL) { if (ferror(stdin)) { log_error("Error %d: %s\n", errno, strerror(errno)); @@ -825,7 +826,17 @@ int get_line(char *buffer, int bsize) // remove unwanted characters from the buffer buffer[strcspn(buffer, "\r\n")] = '\0'; + +#ifdef _WIN32 + // NOTE: Undefined behaviour in C standard but Windows allows it + fflush(stdin); +#else int len = strlen(buffer); + if (len == bsize - 1) { + int ch; + while ((ch = getchar()) != '\n' && ch != EOF); + } +#endif return len; }