Hope you can help me to resolve this problem.
For school I have to transform Ctrl+C to a command which doesn\'t shut down the shell, but he reminds through
Here's a trivial implementation of handling SIGINT
using sigaction which will work on posix systems. Left out error checking for brevity. The linked manual should explain about sigaction
.
Basically the program loops through an infinite loop and break if user types exit. Using write
as you can't use printf in signal handler. See signal manual for a list of functions that can be used safely in a signal handler.
#include
#include
#include
#include
char s[]="Type 'exit' to terminate\n";
void int_handler (int signum)
{
write(fileno(stdin), s, sizeof s - 1);
}
int main (void)
{
char str[256];
struct sigaction sh;
sh.sa_handler = int_handler;
sigemptyset (&sh.sa_mask);
sh.sa_flags = 0;
sigaction (SIGINT, &sh, NULL);
printf("%s", s);
while(1) {
fgets(str, sizeof str, stdin);
char *p = strchr(str, '\n');
if(p) *p = 0;
if(!strcmp(str, "exit")) {
printf("Exiting on request...");
break;
}
}
return 0;
}