Hi,
I am trying to implement redirections. I am able to get the result for input and output redirection, individually. However, when I do both together I see not o/p.
file_input - Input file
file_output - output file
cmd - the command that will get exected
argv - arguments
symbol - tell which is the redirection
Code:
//Function that exectues commands involving redirection
int execute_redirect_command(char file_input[], char file_output[], char cmd[], char **argv, char symbol[])
{
FILE *input_ptr, *output_ptr;
int fd;
if(fork() == 0)
{
if(file_input != NULL)
{
input_ptr = fopen(file_input,"r");
fd = fileno(input_ptr);
dup2(fd,0);
close(fd);
}
if(file_output != NULL)
{
if(strcmp(symbol,">>") == 0)
{
output_ptr = fopen(file_output,"a");
}
else
{
output_ptr = fopen(file_output,"w");
}
fd = fileno(output_ptr);
dup2(fd,1);
close(fd);
}
execvp(cmd, argv);
}
else
{
wait(NULL);
}
}
Above is my code. Also would like to know how to do error handling here.
Thanks in advance.