#include /* errno */ #include /* fprintf(3), stderr, "psops.c" */ #include /* strerror(3) */ #include /* EX_OSERR */ #include /* execvp(3) */ #include /* */ #include /* wait(2) */ #include "libpsargs.h" /* corresponding_arg(3), "psops.c" */ static char *program_name = "pspipe"; /* At the start of the loop argv[0] is { '[', '\0' } and file descriptor 0 is * the intended standard input */ int f(char **argv){ static int child; char **corr; int fd[2]; int r; *(corr = corresponding_arg(argv)) = NULL; if(corr[1] != NULL){ if(pipe(fd) != 0){ fprintf(stderr, "%s: %s: %s\n", program_name, argv[1], strerror(errno) ); return EX_OSERR; } if((r = fork()) == -1){ fprintf(stderr, "%s: %s\n", program_name, strerror(errno) ); return EX_OSERR; } if(r == 0) dup2(fd[0], 0); else dup2(fd[1], 1); close(fd[1]); close(fd[0]); if(r == 1) execvp(argv[1], argv+1); } argv = corr; return *++argv == NULL ? WEXITSTATUS(child) : f(corr + 1); } #include "psops.c"