rendered paste bodystatic int pipline(char **args, int argsNb,int bg, int pip0, int pip1, char *pipeArgs){
printf("in piplien fun\n");
//saving stdout fd
int fdStdout = dup(1);
int fdStdin = dup(0);
if(pip0!=-1 && pip1==-1){ //there's something to read
int tpip[2];
pipe(tpip);
printf("now reading from pipe\n");
dup2(tpip[1], 1);
dup2(tpip[0], 0);
//parse pipeArgs an give them to run command
run_command(args, argsNb, bg);
printf("%s", pipeArgs);
close(tpip[0]);
close(tpip[1]);
dup2(fdStdin, 0);
dup2(fdStdout, 1);
//printf("got from pipe: %s\n", pipeArgs);
}
else if(pip1!=-1){ // now redirect output to pipe
printf("now start writing to pipe\n");
dup2(pip1,1);
setvbuf(stdout,(char*)NULL,_IONBF,0);
printf("------pipe starts here\n");
run_command(args, argsNb, bg);
int res = read(pip0, pipeArgs, 1000); //read from pipe
//int res = read(pip0, pipeArgs, sizeof(pipeArgs)); //read from pipe
if (res == -1) return (error);
else printf("ok: reading from pipe\n");
printf("------pipe ends here\n");
//restore
close(pip1);
dup2(fdStdout,1);
printf("writing back to stdout\n");
}
else
run_command(args, argsNb, bg);
printf("exit of piplien fun\n");
return(0);
}