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: