Re: scanf failling with ICANON mode when in daemon mode (screen -dmS)

Jeffrey Walton <[email protected]> Fri, 25 Apr 2025 17:29:26 -0400
Newsgroups gmane.comp.gnu.screen.user
Message-ID <CAH8yC8n69hx8JmAGWE=X4fvht_fJwwdjjCMdJnyoAt0c53ibBw@mail.gmail.com>
On Fri, Apr 25, 2025 at 5:11=E2=80=AFPM Presseau, Michael
<[email protected]> wrote:
>
> Hi,
>
>
>
> I have done my own application console with scanf(). When I run in the sc=
reen (screen -mS test ./test), it work as expected with each characters pri=
nted. But when I run it in screen as daemon, it's failing in loop:
>
> $ screen -dmS test ./test
>
> $ screen -r test
>
>
>
> stdin_thread scanf failed! ret: -1
>
> stdin_thread scanf failed! ret: -1
>
>
>
> Do you have any clue on how I can make this work in deamon mode?
>
>
>
> Here is my sample code:
>
> ```
>
> #include <stdio.h>
>
> #include <errno.h>
>
> #include <termios.h>
>
> #include <unistd.h>
>
>
>
> void enableRawMode()
>
> {
>
> #ifdef __linux__
>
>                 struct termios raw;
>
>
>
>                 // Get current terminal attributes
>
>                 tcgetattr(STDIN_FILENO, &raw);
>
>
>
>                 // Modify terminal attributes for raw mode
>
>                 raw.c_lflag &=3D ~(ECHO | ICANON);
>
>
>
>                 // Set modified attributes
>
>                 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
>
> #endif
>
> }
>
>
>
> int main() {
>
>                 enableRawMode();
>
>
>
>                 char c;
>
>                 while (1) {
>
>                                int ret =3D scanf("%c", &c);
>
>                                if ( ret > 0) {
>
>                                                printf("%c", c);
>
>                                } else {
>
>                                                printf("stdin_thread scanf=
 failed! ret: %d\n", ret);
>
>                                }
>
>                 }
>
>                 return 0;
>
> }
>
> ```

Maybe you need a call to:

    if (tcsetattr(fd, TCSANOW, &tty) !=3D 0) {
        log_error("term_config: tcsetattr: %s\n", strerror(errno));
        return errno;
    }

to ensure the changes to the terminal are applied immediately.

Jeff