SIGINT detection works now. adding some documentatios, still need to find memoty leaks

This commit is contained in:
Sagi Dayan 2014-03-26 15:52:32 +02:00
parent 1469f517d6
commit 1bca3de24d

109
main.c
View file

@ -20,6 +20,8 @@ char** ReadTokens(FILE* stream, int* writeToFile) ;
void FreeTokens(char** tokens) ; void FreeTokens(char** tokens) ;
void checkIfQuit(char* word); void checkIfQuit(char* word);
int getFileNameIndex(char** args); int getFileNameIndex(char** args);
void freeRemainderInArr(char** args, int beforLast);
int main() int main()
@ -35,18 +37,20 @@ int main()
if(getUserName(user_n) == ERROR) if(getUserName(user_n) == ERROR)
goto quit; goto quit;
signal(SIGINT, SIG_IGN); // ignore SIGINT
while(1) while(1)
{ {
printf("%d %s@%s$ ",exitCode, user_n, host_n); printf("%d %s@%s$ ",exitCode, user_n, host_n); // Print Prompt
args = ReadTokens(stdin, &writeToFile); args = ReadTokens(stdin, &writeToFile); // get user command and tokenize it to args, check if there is a redirect to file.
if(args != NULL) 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 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; goto quit;
} }
writeToFile = false; writeToFile = false; // set the "boolean" back to default
} }
quit: quit:
@ -55,25 +59,35 @@ int main()
return 0; return 0;
} }
int getHostName(char* host) /**
{ * Atemting to get the host name from the system
if(gethostname(host, 10) != 0) * @param host - the string that will hold the host name
{ * @return 0 if all went well, -1 (ERROR) otherwise
fprintf(stderr, "%s\n", "Error: while getting host name"); */
return -1; int getHostName(char* host)
} {
return 0; if(gethostname(host, 10) != 0)
} {
fprintf(stderr, "%s\n", "Error: while getting host name");
return ERROR;
}
return 0;
}
int getUserName(char* name) /**
{ * Atemting to get the user name from the system
if(getlogin_r(name, 10) != 0) * @param host - the string that will hold the user name
{ * @return 0 if all went well, -1 (ERROR) otherwise
fprintf(stderr, "%s\n", "Error: while getting User name"); */
return ERROR; int getUserName(char* name)
} {
return 0; 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. * This function will atempt to create a new Process.
@ -100,26 +114,39 @@ int getUserName(char* name)
} }
else if(pid == 0) // child 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) if(writeToFile == true)
{ {
int i_lastArg = getFileNameIndex(args); int i_lastArg = getFileNameIndex(args);
char* fileName = (char *)malloc(strlen(args[i_lastArg])*sizeof(char)); char* fileName = (char *)malloc(strlen(args[i_lastArg])*sizeof(char));
strcpy(fileName, args[i_lastArg]); 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); int fd = open(fileName, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
dup2(fd, 1); // make stdout go to file if(fd >= 0)
dup2(fd, 2); // make stderr go to file {
close(fd); 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; exit(1); //child error - chield needs to be closed manually. exit code 225;
} }
} }
else //pid > 0 father else //pid > 0 father
waitpid(pid,&status,0); waitpid(pid,&status,0);
free(path); free(path);
free(exe); free(exe);
if(status == EXIT_1) //in my computer exit(1) returns 256 - ex demands wanted 255 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(i == wordCnt-2) // one before the last argument. to see if to put output into file.
if(strcmp(">", returnArr[i]) == 0) if(strcmp(">", returnArr[i]) == 0)
*writeToFile = true; *writeToFile = true;
tok = strtok(NULL, " \n"); tok = strtok(NULL, " \n");
i++; i++;
} }
returnArr[wordCnt] = NULL; // the last index will be NULL - so we know where the end is. returnArr[wordCnt] = NULL; // the last index will be NULL - so we know where the end is.
return returnArr; return returnArr;
} }
@ -236,4 +263,12 @@ int getFileNameIndex(char** args)
while(args[i] != NULL) while(args[i] != NULL)
i++; i++;
return i-1; return i-1;
} }
void freeRemainderInArr(char** args, int beforLast)
{
free(args[beforLast+1]);
args[beforLast] = NULL;
free(args[beforLast]);
args[beforLast - 1] = NULL;
}