0

I'm getting user input in a main reading loop, and I want to get custom input if ctrl-c is pressed. But it seems that there's a difference between user input and some string wrote in the standard input with write(0, foo, strlen(foo);.

Here's a small program that shows that:

#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>

void ctrlc_handler(int signal)
{
    write(0, "abc", 3);
}

int main(void)
{
    char    buf[5];

    signal(SIGINT, ctrlc_handler);
    while (1)
    {
        write(1, "> ", 2);
        memset(buf, 0, 5);
        read(0, &buf, 4);
        printf("Read: %s", buf);

    }
    return (0);
}

And an exemple of output:

> Hey
Read: Hey
> ^Cabc
Read:
lfalkau
  • 896
  • 7
  • 21

1 Answers1

0

Found this answer:

Writing to stdin and reading from stdout (UNIX/LINUX/C Programming)

Seems that we can't write to stdin, even if in my case it seems to work and the write function doesn't return -1

lfalkau
  • 896
  • 7
  • 21