fixed the io redirection
This commit is contained in:
parent
72c42aa825
commit
f87379c9da
@ -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)
|
||||
// {
|
||||
|
Loading…
Reference in New Issue
Block a user