Signal Handler Added. - redy to deploy

This commit is contained in:
Sagi Dayan 2014-04-02 14:37:41 +03:00
parent 91af9dcc91
commit bc1f7e0712

22
main.c
View file

@ -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;
}