Problems with flushing stdin in mintty

Christoph Reiter reiter.christoph@gmail.com
Sun Jul 13 09:40:10 GMT 2025


I'm having the problem that under cygwin flushing stdin for some reason doesn't
work when running in mintty. It works when running cygwin in the Windows
terminal though. In my case the program using this is pacman, where it asks
various questions and if you hit some keys in-between questions they leak into
the next one, resulting in invalid input.

Example program showing the problem. Type some text in the first 5 seconds, and
see the input leak into the next input. Any ideas/workarounds welcome.

```c
// gcc -o flush_stdin flush_stdin.c
#include <stdio.h>
#include <unistd.h>
#include <termios.h>

int main() {
    printf("type something in the next 5 secs\n");
    for (int i=0; i < 5; i++) {
        sleep(1);
        printf("%d\n", i);
    }

    // Flush stdin
    int fd = fileno(stdin);
    if (fd != -1) {
        printf("flushing stdin\n");
        if (tcflush(fd, TCIFLUSH) == -1) {  // this doesn't seem to do anything
            perror("Error flushing stdin");
            return 1;
        }
    } else {
        perror("Error getting file descriptor for stdin");
        return 1;
    }

    char buffer[100];
    printf("Enter some text:\n");
    fgets(buffer, sizeof(buffer), stdin);
    printf("You entered: %s\n", buffer);

    return 0;
}
```


More information about the Cygwin mailing list