Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flushing stdin doesn't seem to work #199

Open
lazka opened this issue Feb 17, 2024 · 3 comments
Open

Flushing stdin doesn't seem to work #199

lazka opened this issue Feb 17, 2024 · 3 comments

Comments

@lazka
Copy link
Member

lazka commented Feb 17, 2024

This is an example extracted from pacman https://gitlab.archlinux.org/pacman/pacman/-/blob/2180e4d127af2ce0722b831a7131283604a7bdf1/src/pacman/util.c#L185

pacman flushes stdin before asking a question and doesn't have and takes any non-"y" as not, leading to user confusion if for example enter is pressed during the waiting before the question is asked, leading to surprise aborts of the process, example: msys2/MSYS2-packages#3532

This example allows you to input text, then flushes as pacman does, then reads stdin. Flushing currently doesn't have an effect.

#include <stdio.h>
#include <unistd.h>
#include <termios.h>

int main() {
    printf("type something in the next 5 secs\n");
    for (int i; 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;
}
@lazka
Copy link
Member Author

lazka commented Feb 17, 2024

Confirmed also with cygwin.

It happens with mintty, but not when running in Windows Terminal. Disabling conpty doesn't make a difference.

The question now is if this is a mintty or cygwin issue.

edit: the issue also exists with xterm under cygwin

edit: the issue doesn't exist with cmd.exe

@lazka
Copy link
Member Author

lazka commented Feb 17, 2024

on IRC it was pointed out that the issue existing in xterm might indicate that it's a cygwin problem

@lazka
Copy link
Member Author

lazka commented Feb 20, 2024

next: ask on the cygwin ML for help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant