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

65
main.c
View file

@ -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)
{
/**
* 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 -1;
return ERROR;
}
return 0;
}
}
int getUserName(char* name)
{
/**
* 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,18 +114,30 @@ 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);
if(fd >= 0)
{
dup2(fd, 1); // make stdout go to file
dup2(fd, 2); // make stderr 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");
@ -120,6 +146,7 @@ int getUserName(char* name)
}
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
@ -237,3 +264,11 @@ int getFileNameIndex(char** args)
i++;
return i-1;
}
void freeRemainderInArr(char** args, int beforLast)
{
free(args[beforLast+1]);
args[beforLast] = NULL;
free(args[beforLast]);
args[beforLast - 1] = NULL;
}