I'm creating a shell program, and I want Ctrl+C to kill foreground processes but not background processes for example; &sleep 50
is a background process, and I want it to be so that if I were to use Ctrl+C it would kill any foreground processes, and leave the background unaffected. But for the life of me can't figure out what to do, any help is greatly appreciated :D
int main(void) {
Command cmd;
int n, forSignals;
char **cmds;
char **pipedCmds;
signal(SIGINT, INThandler);
while (!done) {
char *line;
line = readline("> ");
if (!line) {
done = 1;
} else {
stripwhite(line);
if(*line) {
add_history(line);
n = parse(line, &cmd);
PrintCommand(n, &cmd);
cmds = getCmds(cmd.pgm);
pipedCmds = getPipedCmds(cmd.pgm);
executionDecider(line, cmds, pipedCmds, cmd);
}
}
if(line) {
free(line);
}
}
return 0;
}
void INThandler(int sig)
{
signal(sig, SIG_IGN);
kill(SIGINT, 0);
printf("\n");
}
P.S. There is of course the rest of the code on actually executing programs, let me know if it's necessary to be shown, but I believe, this is a good minimally reproducible example.
EDIT: Quite important, don't know how I forgot :/ but I need it to not create a zombie process by doing this, it shouldn't leave ANY zombies behind.
EDIT: Please find linked a URL leading to a code dump of the full project. It may make more sense there: https://codedump.io/share/d8hrj40JdEqL/1/lshc---c-shell-program