I want to have my program ignore the (Ctrl-C) signal and instead should terminate on (Ctrl-D).
How can i do that?
Printable View
I want to have my program ignore the (Ctrl-C) signal and instead should terminate on (Ctrl-D).
How can i do that?
Ctrl-C is usually SIGINT, so all you need to ignore it. :)
Code:struct sigaction sac;
if (sigaction(SIGINT, NULL, &sac) == -1)
perror("Couldn't get old handler");
else {
sac.sa_handler = SIG_IGN;
if (sigaction(SIGINT, &sac, NULL) == -1)
perror("Couldn't ignore SIGINT");
}
And ctrl-D generates EOF on the input stream.
Aren't you glad you learnt how to use fgets :)
Code:while ( fgets( buff, sizeof buff, stdin ) != NULL ) {
// do stuff
}
if ( feof(stdin) ) {
// user pressed ctrl-D
}