my code shows me error
echo: write error: Bad file descriptor
when i execute echo hi | wc
this is my code:
void execute_commands(struct command mycommands[], int no_commands)
{
int i;
int *arr[no_commands-1];
for (i=0; i<no_commands-1; i++)
arr[i] = (int *)malloc(2 * sizeof(int));
for (i=0; i<no_commands-1; i++)
pipe(arr[i]);
for( i = 0; i < no_commands - 1; i++)
execute_command(mycommands[i].comm, mycommands[i].no_args, mycommands[i].args, arr[i][1], arr[i][0]);
}
int execute_command(char *command, int argc, char *args[], int fd_in, int fd_out )
{
pid_t pid;
int child_status;
if ((pid = fork()) == 0){
if (fd_in != -1) {
dup2(fd_in, 0);
close(fd_in);
}
if (fd_out != -1){
dup2(fd_out, 1);
close(fd_out);
}
execvp(command, args);
exit(0);
}
else if (pid > 0){
if (fd_in != -1) close(fd_in);
if (fd_out != -1) close(fd_out);
waitpid(pid, &child_status, 0);
}
else {
fprintf(stderr, "Error in creating a child process!\n");
exit(-1);
}
return 0;
}