Revert back to old way of flushing input buffer

This commit is contained in:
Tiiffi
2024-12-12 22:14:40 +02:00
parent dfd840de1f
commit f0e7e71589

View File

@@ -38,10 +38,10 @@
#include <fcntl.h> #include <fcntl.h>
#include <wchar.h> #include <wchar.h>
#else #else
#include <stdio_ext.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/select.h> #include <sys/select.h>
#include <netdb.h> #include <netdb.h>
#include <termios.h>
#endif #endif
#define VERSION "0.8.0" #define VERSION "0.8.0"
@@ -794,14 +794,17 @@ char *utf8_getline(char *buf, int size, FILE *stream)
} }
#endif #endif
/*
void flush_input(void) void flush_input(void)
{ {
#ifdef _WIN32 #ifdef _WIN32
FlushConsoleInputBuffer(console_handle); // NOTE: Undefined behaviour in C standard but Windows allows it
fflush(stdin);
#else #else
tcflush(STDIN_FILENO, TCIFLUSH); __fpurge(stdin);
#endif #endif
} }
*/
// gets line from stdin and deals with rubbish left in the input buffer // gets line from stdin and deals with rubbish left in the input buffer
int get_line(char *buffer, int bsize) int get_line(char *buffer, int bsize)
@@ -812,8 +815,6 @@ int get_line(char *buffer, int bsize)
char *ret = fgets(buffer, bsize, stdin); char *ret = fgets(buffer, bsize, stdin);
#endif #endif
flush_input();
if (ret == NULL) { if (ret == NULL) {
if (ferror(stdin)) { if (ferror(stdin)) {
log_error("Error %d: %s\n", errno, strerror(errno)); 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 // remove unwanted characters from the buffer
buffer[strcspn(buffer, "\r\n")] = '\0'; 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); int len = strlen(buffer);
if (len == bsize - 1) {
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
}
#endif
return len; return len;
} }