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
|
// inputString may not be useful here, only *command
|
||||||
// Try to add a layer of abstraction where possible
|
// Try to add a layer of abstraction where possible
|
||||||
// I.E. think about using function instead for checking
|
// I.E. think about using function instead for checking
|
||||||
void ioRedirection(char *inputString, CommandStruct *command)
|
void ioRedirection(CommandStruct *command)
|
||||||
{
|
{
|
||||||
int newfd;
|
int newfd;
|
||||||
// check command standard output
|
// check command standard output
|
||||||
// TODO: Check command->argv[] for symbol instead
|
// TODO: Check command->argv[] for symbol instead
|
||||||
if (strchr(inputString, '>') != NULL)
|
for(int i = 0; i < sizeof(command); i++)
|
||||||
{
|
{
|
||||||
// TODO: Throwing an error here and line 108 for using command
|
if (strchr(command->argv[i], '>') != NULL)
|
||||||
// Check if targeting a specific string in command->argv works
|
{
|
||||||
newfd = (open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0);
|
output(command->argv[i+1]);
|
||||||
// No if statement for fail checking, brackets just exist here
|
// Check if targeting a specific string in command->argv works
|
||||||
{
|
}
|
||||||
// failed
|
if (strchr(command->argv[i], '<') != NULL)
|
||||||
perror(command);
|
{
|
||||||
exit(1);
|
input(command->[i+1]);
|
||||||
}
|
}
|
||||||
|
if (strchr(command->argv[i], '|')!= NULL)
|
||||||
// copies the new file descriptor
|
{
|
||||||
dup2(newfd, 1);
|
// not sure if i + 1 correct
|
||||||
} // this is supposed to close if statement??
|
// 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
|
// check command standard input
|
||||||
// if (strchr(inputString, '<') != NULL)
|
// if (strchr(inputString, '<') != NULL)
|
||||||
// {
|
// {
|
||||||
|
Loading…
Reference in New Issue
Block a user