From 1bca3de24d6bd15fdecd43a030270d5f512c1496 Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Wed, 26 Mar 2014 15:52:32 +0200 Subject: [PATCH] SIGINT detection works now. adding some documentatios, still need to find memoty leaks --- main.c | 109 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 72 insertions(+), 37 deletions(-) diff --git a/main.c b/main.c index 473ae75..90da7b7 100644 --- a/main.c +++ b/main.c @@ -20,6 +20,8 @@ char** ReadTokens(FILE* stream, int* writeToFile) ; void FreeTokens(char** tokens) ; void checkIfQuit(char* word); int getFileNameIndex(char** args); +void freeRemainderInArr(char** args, int beforLast); + int main() @@ -35,18 +37,20 @@ int main() if(getUserName(user_n) == ERROR) goto quit; + signal(SIGINT, SIG_IGN); // ignore SIGINT + while(1) { - printf("%d %s@%s$ ",exitCode, user_n, host_n); - args = ReadTokens(stdin, &writeToFile); - if(args != NULL) + printf("%d %s@%s$ ",exitCode, user_n, host_n); // Print Prompt + args = ReadTokens(stdin, &writeToFile); // get user command and tokenize it to args, check if there is a redirect to file. + if(args != NULL) // if user did enter a command do the following { - exitCode = runExec(args, writeToFile); + exitCode = runExec(args, writeToFile); // fork and run the process, return child exit code FreeTokens(args); //in case of error while trying to create process - this method will free the memory already - if(exitCode == ERROR) + if(exitCode == ERROR) // in case we need to exit. goto quit; } - writeToFile = false; + writeToFile = false; // set the "boolean" back to default } quit: @@ -55,25 +59,35 @@ int main() return 0; } -int getHostName(char* host) -{ - if(gethostname(host, 10) != 0) - { - fprintf(stderr, "%s\n", "Error: while getting host name"); - return -1; - } - return 0; -} +/** + * Atemting to get the host name from the system + * @param host - the string that will hold the host name + * @return 0 if all went well, -1 (ERROR) otherwise + */ + int getHostName(char* host) + { + if(gethostname(host, 10) != 0) + { + fprintf(stderr, "%s\n", "Error: while getting host name"); + return ERROR; + } + return 0; + } -int getUserName(char* name) -{ - if(getlogin_r(name, 10) != 0) - { - fprintf(stderr, "%s\n", "Error: while getting User name"); - return ERROR; - } - return 0; -} +/** + * Atemting to get the user name from the system + * @param host - the string that will hold the user name + * @return 0 if all went well, -1 (ERROR) otherwise + */ + int getUserName(char* name) + { + if(getlogin_r(name, 10) != 0) + { + fprintf(stderr, "%s\n", "Error: while getting User name"); + return ERROR; + } + return 0; + } /** * This function will atempt to create a new Process. @@ -100,26 +114,39 @@ int getUserName(char* name) } 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); char* fileName = (char *)malloc(strlen(args[i_lastArg])*sizeof(char)); strcpy(fileName, args[i_lastArg]); - args[i_lastArg - 1] = NULL; + freeRemainderInArr(args, i_lastArg); + int fd = open(fileName, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); - dup2(fd, 1); // make stdout go to file - dup2(fd, 2); // make stderr go to file - close(fd); + if(fd >= 0) + { + dup2(fd, 1); // make stdout go to file + close(fd); + } + else + { + fprintf(stderr, "%s%s%s\n", "Unable to open file ",fileName," : printing out put to default stdout:"); + } - } - if(execv(path, args) != 0) - { - fprintf(stderr, "%s%s%s\n", "myTerminal: ", args[0] ,": command not found"); + + } + + + if(execv(path, args) != 0) + { + fprintf(stderr, "%s%s%s\n", "myTerminal: ", args[0] ,": command not found"); exit(1); //child error - chield needs to be closed manually. exit code 225; } } else //pid > 0 father waitpid(pid,&status,0); + free(path); free(exe); if(status == EXIT_1) //in my computer exit(1) returns 256 - ex demands wanted 255 @@ -176,10 +203,10 @@ int getUserName(char* name) if(i == wordCnt-2) // one before the last argument. to see if to put output into file. if(strcmp(">", returnArr[i]) == 0) *writeToFile = true; - tok = strtok(NULL, " \n"); - i++; - } - + tok = strtok(NULL, " \n"); + i++; + } + returnArr[wordCnt] = NULL; // the last index will be NULL - so we know where the end is. return returnArr; } @@ -236,4 +263,12 @@ int getFileNameIndex(char** args) while(args[i] != NULL) i++; return i-1; -} \ No newline at end of file +} + +void freeRemainderInArr(char** args, int beforLast) +{ + free(args[beforLast+1]); + args[beforLast] = NULL; + free(args[beforLast]); + args[beforLast - 1] = NULL; +}