Writing output to file works! - need some more documentation

This commit is contained in:
Sagi Dayan 2014-03-26 00:23:18 +02:00
parent e24b5ed20c
commit 1469f517d6

55
main.c
View file

@ -3,8 +3,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#define true 1
#define false 0
#define LEN 512
#define PATH "/bin/"
#define ERROR -1
@ -12,11 +14,12 @@
int getHostName(char* host);
int getUserName(char* name);
int runExec(char** args);
int runExec(char** args, int writeToFile);
void PrintTokens (char** tokens);
char** ReadTokens(FILE* stream) ;
char** ReadTokens(FILE* stream, int* writeToFile) ;
void FreeTokens(char** tokens) ;
void checkIfQuit(char* word);
int getFileNameIndex(char** args);
int main()
@ -25,6 +28,7 @@ int main()
char* host_n = (char *)malloc(sizeof(char));
char* user_n = (char *)malloc(sizeof(char));
char** args;
int writeToFile = false;
if(getHostName(host_n) == ERROR)
goto quit;
@ -34,17 +38,18 @@ int main()
while(1)
{
printf("%d %s@%s$ ",exitCode, user_n, host_n);
args = ReadTokens(stdin);
args = ReadTokens(stdin, &writeToFile);
if(args != NULL)
{
exitCode = runExec(args);
exitCode = runExec(args, writeToFile);
FreeTokens(args); //in case of error while trying to create process - this method will free the memory already
if(exitCode == ERROR)
goto quit;
}
writeToFile = false;
}
quit:
quit:
free(host_n);
free(user_n);
return 0;
@ -76,7 +81,7 @@ int getUserName(char* name)
* @param args - an array of strings that contains all the arguments from the user
* @return int - exit status of child
*/
int runExec(char** args)
int runExec(char** args, int writeToFile)
{
int status;
pid_t pid;
@ -86,8 +91,6 @@ int getUserName(char* name)
strcpy(path, PATH);
strcat(path, exe);
printf("try: %s \n",args[0]);
pid = fork();
if(pid < 0)
@ -97,15 +100,28 @@ int getUserName(char* name)
}
else if(pid == 0) // child
{
if(execv(path, args) != 0)
if(writeToFile == true)
{
fprintf(stderr, "%s%s%s\n", "myTerminal: ", args[0] ,": command not found");
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;
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(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
return status-1;
return status;
@ -118,7 +134,7 @@ int getUserName(char* name)
* @param stream FILE* - in our case stdin
* @return if all went well , an Array of arguements from the input line.
*/
char** ReadTokens(FILE* stream)
char** ReadTokens(FILE* stream, int* writeToFile)
{
char** returnArr;
char input[LEN]; // the var to hold the line
@ -154,16 +170,17 @@ int getUserName(char* name)
while (tok != NULL)
{
returnArr[i] = (char *)malloc(strlen(tok)*sizeof(char));
//returnArr[i] = tok;
strcpy(returnArr[i], tok);
if(wordCnt == 1)
checkIfQuit(tok);
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++;
}
returnArr[wordCnt] = NULL; // the last index will be NULL - so we know where the end is.
return returnArr;
}
@ -175,7 +192,7 @@ int getUserName(char* name)
*/
void checkIfQuit(char* word)
{
if(strcmp(word, "quit") == 0)
if(strcmp(word, "exit") == 0)
exit(0);
}
@ -211,4 +228,12 @@ int getUserName(char* name)
free(tokens[i]); // free the last index
tokens = NULL; // free the array
free(tokens);
}
int getFileNameIndex(char** args)
{
int i = 0;
while(args[i] != NULL)
i++;
return i-1;
}