From f87379c9dac963bf834922da1990ece858d42c7e Mon Sep 17 00:00:00 2001 From: Samantha Boyer Date: Thu, 27 Oct 2022 22:45:40 -0500 Subject: [PATCH] fixed the io redirection --- src/Integrated.c | 69 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/src/Integrated.c b/src/Integrated.c index fedc7be..d1a2ef0 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -93,27 +93,62 @@ void ReadPishrc(CommandStruct *command, char *inputString) // inputString may not be useful here, only *command // Try to add a layer of abstraction where possible // I.E. think about using function instead for checking -void ioRedirection(char *inputString, CommandStruct *command) +void ioRedirection(CommandStruct *command) { int newfd; // check command standard output // TODO: Check command->argv[] for symbol instead - if (strchr(inputString, '>') != NULL) - { - // TODO: Throwing an error here and line 108 for using command - // Check if targeting a specific string in command->argv works - newfd = (open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0); - // No if statement for fail checking, brackets just exist here - { - // failed - perror(command); - exit(1); - } - - // copies the new file descriptor - dup2(newfd, 1); - } // this is supposed to close if statement?? - + for(int i = 0; i < sizeof(command); i++) + { + if (strchr(command->argv[i], '>') != NULL) + { + output(command->argv[i+1]); + // Check if targeting a specific string in command->argv works + } + if (strchr(command->argv[i], '<') != NULL) + { + input(command->[i+1]); + } + if (strchr(command->argv[i], '|')!= NULL) + { + // not sure if i + 1 correct + // pipe(command->argv[i+1]); + } + if (strpbrk(command->argv[i], ">>")!= NULL) + { + append(command->argv[i-1], command->argv[i+1]); + } + } +} +void output(char *command){ + int newfd; + if(newfd = open(command, O_CREAT | O_TRUNC | O_WRONLY, 0644)< 0) + { + perror(command); + exit(1); + } + dup2(newfd,1); +} +void input(char *command) +{ + int newfd; + if(newfd = open(command, O_RDONLY)<0) + { + perror(command); + exit(1); + } + dup2(newfd,0); +} +void pipe(){} +void append(char *input, char *file) +{ + FILE *fp; + + fp = fopen(file, "a+"); + + fputs(input); + fclose(fp); +} // check command standard input // if (strchr(inputString, '<') != NULL) // {