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

53
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,16 +59,26 @@ int main()
return 0; 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) int getHostName(char* host)
{ {
if(gethostname(host, 10) != 0) if(gethostname(host, 10) != 0)
{ {
fprintf(stderr, "%s\n", "Error: while getting host name"); fprintf(stderr, "%s\n", "Error: while getting host name");
return -1; return ERROR;
} }
return 0; 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) int getUserName(char* name)
{ {
if(getlogin_r(name, 10) != 0) if(getlogin_r(name, 10) != 0)
@ -100,18 +114,30 @@ 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);
if(fd >= 0)
{
dup2(fd, 1); // make stdout go to file dup2(fd, 1); // make stdout go to file
dup2(fd, 2); // make stderr go to file
close(fd); 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) if(execv(path, args) != 0)
{ {
fprintf(stderr, "%s%s%s\n", "myTerminal: ", args[0] ,": command not found"); fprintf(stderr, "%s%s%s\n", "myTerminal: ", args[0] ,": command not found");
@ -120,6 +146,7 @@ int getUserName(char* name)
} }
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
@ -237,3 +264,11 @@ int getFileNameIndex(char** args)
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;
}