From bc1f7e0712338f6bf3c03ed263a9bd2e5af280e5 Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Wed, 2 Apr 2014 14:37:41 +0300 Subject: [PATCH] Signal Handler Added. - redy to deploy --- main.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 75740b2..7926161 100644 --- a/main.c +++ b/main.c @@ -21,8 +21,10 @@ void FreeTokens(char** tokens) ; void checkIfQuit(char* word); int getFileNameIndex(char** args); void freeRemainderInArr(char** args, int beforLast); +void sigHandler(int sig); - +//GLOBAL INT - THIS WILL HOLD CHILD PID OR -1 IF THERE IS NO CHILD +int pidOfChild = -1; int main() { @@ -37,7 +39,7 @@ int main() if(getUserName(user_n) == ERROR) goto quit; - signal(SIGINT, SIG_IGN); // ignore SIGINT + signal(SIGINT, sigHandler); // Change the default SIGINT handler while(1) { @@ -113,9 +115,7 @@ int main() return ERROR; } else if(pid == 0) // child - { - signal(SIGINT, SIG_DFL); // set the child SIGINT handler to default! -> ^C will kill child but not father. - + { if(writeToFile == true) { int i_lastArg = getFileNameIndex(args); @@ -286,3 +286,15 @@ void freeRemainderInArr(char** args, int beforLast) free(args[beforLast]); args[beforLast - 1] = NULL; } + +/** + * This function will be calld when the SIGINT is invoked. + * only if there is a child, the "father" will kill it. + * @param sig - + */ +void sigHandler(int sig) +{ + if(pidOfChild >= 0) + kill(pidOfChild, SIGINT); + pidOfChild = -1; +} \ No newline at end of file