else if (i < (fork_num-1)) {//i is NOT the last command fprintf(stderr,"Middle case i = %d and command = [%s]\n", i,program_name ); int pipeid2[2]; if (pipe(pipeid2)==-1){ perror("Failed piping middle pipe"); } childpid = fork(); if (childpid == 0 ) { if ((dup2(pipeid[0], 0) == -1 ) || (dup2(pipeid2[1],1)==-1)) perror("Failed to redirect STDIN"); else if ((close(pipeid[0]) == -1) || (close(pipeid2[1]) == -1) ) { perror("Failed at closing"); } else { if (execvp(program_name,cmd_to_fork) == -1) { perror("Failure calling Middle executable"); } } return 1; } wait(&status); if (WIFEXITED(status) != 1) { perror("child exit error"); } close(pipeid[0]); close(pipeid2[1]); }