54 lines
1.1 KiB
C
54 lines
1.1 KiB
C
#include <errno.h> /* errno */
|
|
#include <stdio.h> /* fprintf(3), stderr, "psops.c" */
|
|
#include <string.h> /* strerror(3) */
|
|
#include <sysexits.h> /* EX_OSERR */
|
|
#include <unistd.h> /* execvp(3) */
|
|
#include <sys/types.h> /* <sys/wait.h> */
|
|
#include <sys/wait.h> /* 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"
|