Writing output to file works! - need some more documentation
This commit is contained in:
parent
e24b5ed20c
commit
1469f517d6
1 changed files with 40 additions and 15 deletions
55
main.c
55
main.c
|
@ -3,8 +3,10 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#define true 1
|
||||||
|
#define false 0
|
||||||
#define LEN 512
|
#define LEN 512
|
||||||
#define PATH "/bin/"
|
#define PATH "/bin/"
|
||||||
#define ERROR -1
|
#define ERROR -1
|
||||||
|
@ -12,11 +14,12 @@
|
||||||
|
|
||||||
int getHostName(char* host);
|
int getHostName(char* host);
|
||||||
int getUserName(char* name);
|
int getUserName(char* name);
|
||||||
int runExec(char** args);
|
int runExec(char** args, int writeToFile);
|
||||||
void PrintTokens (char** tokens);
|
void PrintTokens (char** tokens);
|
||||||
char** ReadTokens(FILE* stream) ;
|
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 main()
|
int main()
|
||||||
|
@ -25,6 +28,7 @@ int main()
|
||||||
char* host_n = (char *)malloc(sizeof(char));
|
char* host_n = (char *)malloc(sizeof(char));
|
||||||
char* user_n = (char *)malloc(sizeof(char));
|
char* user_n = (char *)malloc(sizeof(char));
|
||||||
char** args;
|
char** args;
|
||||||
|
int writeToFile = false;
|
||||||
|
|
||||||
if(getHostName(host_n) == ERROR)
|
if(getHostName(host_n) == ERROR)
|
||||||
goto quit;
|
goto quit;
|
||||||
|
@ -34,17 +38,18 @@ int main()
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
printf("%d %s@%s$ ",exitCode, user_n, host_n);
|
printf("%d %s@%s$ ",exitCode, user_n, host_n);
|
||||||
args = ReadTokens(stdin);
|
args = ReadTokens(stdin, &writeToFile);
|
||||||
if(args != NULL)
|
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
|
FreeTokens(args); //in case of error while trying to create process - this method will free the memory already
|
||||||
if(exitCode == ERROR)
|
if(exitCode == ERROR)
|
||||||
goto quit;
|
goto quit;
|
||||||
}
|
}
|
||||||
|
writeToFile = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
quit:
|
quit:
|
||||||
free(host_n);
|
free(host_n);
|
||||||
free(user_n);
|
free(user_n);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -76,7 +81,7 @@ int getUserName(char* name)
|
||||||
* @param args - an array of strings that contains all the arguments from the user
|
* @param args - an array of strings that contains all the arguments from the user
|
||||||
* @return int - exit status of child
|
* @return int - exit status of child
|
||||||
*/
|
*/
|
||||||
int runExec(char** args)
|
int runExec(char** args, int writeToFile)
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
@ -86,8 +91,6 @@ int getUserName(char* name)
|
||||||
strcpy(path, PATH);
|
strcpy(path, PATH);
|
||||||
strcat(path, exe);
|
strcat(path, exe);
|
||||||
|
|
||||||
printf("try: %s \n",args[0]);
|
|
||||||
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
|
|
||||||
if(pid < 0)
|
if(pid < 0)
|
||||||
|
@ -97,15 +100,28 @@ int getUserName(char* name)
|
||||||
}
|
}
|
||||||
else if(pid == 0) // child
|
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;
|
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);
|
||||||
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
|
||||||
return status-1;
|
return status-1;
|
||||||
return status;
|
return status;
|
||||||
|
@ -118,7 +134,7 @@ int getUserName(char* name)
|
||||||
* @param stream FILE* - in our case stdin
|
* @param stream FILE* - in our case stdin
|
||||||
* @return if all went well , an Array of arguements from the input line.
|
* @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** returnArr;
|
||||||
char input[LEN]; // the var to hold the line
|
char input[LEN]; // the var to hold the line
|
||||||
|
@ -154,16 +170,17 @@ int getUserName(char* name)
|
||||||
while (tok != NULL)
|
while (tok != NULL)
|
||||||
{
|
{
|
||||||
returnArr[i] = (char *)malloc(strlen(tok)*sizeof(char));
|
returnArr[i] = (char *)malloc(strlen(tok)*sizeof(char));
|
||||||
//returnArr[i] = tok;
|
|
||||||
strcpy(returnArr[i], tok);
|
strcpy(returnArr[i], tok);
|
||||||
if(wordCnt == 1)
|
if(wordCnt == 1)
|
||||||
checkIfQuit(tok);
|
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");
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,7 +192,7 @@ int getUserName(char* name)
|
||||||
*/
|
*/
|
||||||
void checkIfQuit(char* word)
|
void checkIfQuit(char* word)
|
||||||
{
|
{
|
||||||
if(strcmp(word, "quit") == 0)
|
if(strcmp(word, "exit") == 0)
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,4 +228,12 @@ int getUserName(char* name)
|
||||||
free(tokens[i]); // free the last index
|
free(tokens[i]); // free the last index
|
||||||
tokens = NULL; // free the array
|
tokens = NULL; // free the array
|
||||||
free(tokens);
|
free(tokens);
|
||||||
|
}
|
||||||
|
|
||||||
|
int getFileNameIndex(char** args)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
while(args[i] != NULL)
|
||||||
|
i++;
|
||||||
|
return i-1;
|
||||||
}
|
}
|
Loading…
Reference in a new issue