added EV , fixed input and output

This commit is contained in:
Samantha Boyer 2022-10-29 22:55:36 -05:00
parent a0d601f1e3
commit 0062cd1cc5
3 changed files with 10 additions and 8 deletions

View File

@ -6,6 +6,7 @@ const char* GetHomeDir(void);
typedef struct CommandStruct { typedef struct CommandStruct {
char* argv[100]; // Max 100 commands char* argv[100]; // Max 100 commands
int argc; int argc;
char* envp[20]; // Enviroment Varibles
} CommandStruct; } CommandStruct;
void CopyCommandStruct(CommandStruct* oldCommand, CommandStruct* newCommand, int start, int end); void CopyCommandStruct(CommandStruct* oldCommand, CommandStruct* newCommand, int start, int end);

View File

@ -98,13 +98,12 @@ void CheckRedirection(CommandStruct* command)
// TODO: Check command->argv[] for symbol instead // TODO: Check command->argv[] for symbol instead
for (int i = 0; i < command->argc; i++) for (int i = 0; i < command->argc; i++)
{ {
if ((command->argv[i][0] == '>') && (command->argv[i][1] == '>')) if ((command->argv[i][0] == '>') && (command->argv[i][1] == '>')) printf(">> was found.\n");
printf(">> was found.\n");
// ParseInput(inputString, command, ">>"); // ParseInput(inputString, command, ">>");
if (command->argv[i][0] == '<') if (command->argv[i][0] == '<')
printf("< was found.\n"); printf("< was found.\n");
// ParseInput(inputString, command, "<"); // ParseInput(inputString, command, "<");
if (command->argv[i][0] == '>') if (command->argv[i][0] == '>' && command->argv[i][1] != '>')
printf("> was found.\n"); printf("> was found.\n");
// ParseInput(inputString, command, ">"); // ParseInput(inputString, command, ">");
if (command->argv[i][0] == '|') if (command->argv[i][0] == '|')
@ -133,7 +132,7 @@ void ioRedirection(CommandStruct* command)
{ {
append(command->argv[i-1], command->argv[i+1]); append(command->argv[i-1], command->argv[i+1]);
} }
else if (strchr(command->argv[i], '<')!= NULL)//input else if (command->argv[i][0] =='<')//input
{ {
input(command->argv[i+1]); input(command->argv[i+1]);
} }
@ -141,7 +140,7 @@ void ioRedirection(CommandStruct* command)
{ {
//ExecPipe(); //ExecPipe();
} }
else if (strchr(command->argv[i], '>')!= NULL)//output else if (command->argv[i][0] == '>')//output
{ {
output(command->argv[i+1]); output(command->argv[i+1]);
} }
@ -152,7 +151,7 @@ void ioRedirection(CommandStruct* command)
void output(char *command){ void output(char *command){
int newfd; int newfd;
if (newfd = open(command, O_CREAT | O_TRUNC | O_WRONLY, 0644) < 0) if (newfd = open(command, O_CREAT | O_CREAT, 0666) < 0)
{ {
perror(command); perror(command);
exit(1); exit(1);
@ -163,7 +162,7 @@ void output(char *command){
void input(char *command) void input(char *command)
{ {
int newfd; int newfd;
if (newfd = open(command, O_RDONLY) < 0) if (newfd = open(command, O_RDONLY | O_CREAT) < 0)
{ {
perror(command); perror(command);
exit(1); exit(1);

View File

@ -86,6 +86,8 @@ void RunChild(CommandStruct* commandChild, int* pipefd)
dup2(pipefd[1], 1); dup2(pipefd[1], 1);
close(pipefd[1]); close(pipefd[1]);
// execve() is recommended, execvpe() may be better // execve() is recommended, execvpe() may be better
CheckRedirection(commandChild);
ioRedirection(commandChild);
execvp(commandChild->argv[0], commandChild->argv); execvp(commandChild->argv[0], commandChild->argv);
// exec does not return if it succeeds // exec does not return if it succeeds
printf("ERROR: Could not execute %s\n", commandChild->argv[0]); printf("ERROR: Could not execute %s\n", commandChild->argv[0]);