66 lines
1.5 KiB
C
66 lines
1.5 KiB
C
#include <errno.h> /* errno */
|
|
#include <fcntl.h> /* open(2), O_RDONLY */
|
|
#include <stdio.h> /* fprintf(3) */
|
|
#include <string.h> /* strerror(3) */
|
|
#if !defined EX_OK || !defined EX_OSERR || !defined EX_USAGE
|
|
# include <sysexits.h>
|
|
#endif
|
|
#include <unistd.h> /* dup(2), dup2(2), fork(2), STDIN_FILENO */
|
|
#include <sys/types.h> /* <sys/wait.h> */
|
|
#include <sys/wait.h> /* wait(2), WIFEXITED, WEXITSTATUS */
|
|
#include "libpsargs.h"
|
|
|
|
int oserr(char *argv0, char *s){
|
|
|
|
fprintf(stderr, "%s: %s\n", argv0, s);
|
|
|
|
return EX_OSERR;
|
|
}
|
|
|
|
int usage(char *s){
|
|
|
|
fprintf(stderr,
|
|
"Usage: %s \"[\" [command (arguments...)] \"]\""
|
|
" \"[\" [command (arguments...)] \"]\"\n",
|
|
s);
|
|
|
|
return EX_USAGE;
|
|
}
|
|
|
|
int main(int argc, char *argv[]){
|
|
int child;
|
|
char **corr;
|
|
char **curr;
|
|
int fd;
|
|
int i;
|
|
int r;
|
|
|
|
if(argc < 2 || check_arg(curr = &argv[1]) != 2)
|
|
return usage(argc == 0 ? "<no argv[0]>" : argv[0]);
|
|
|
|
/* substitute standard input for /dev/null */
|
|
if((fd = dup(STDIN_FILENO)) < 0
|
|
|| dup2(open("/dev/null", O_RDONLY), STDIN_FILENO) < 0)
|
|
return oserr(argv[0], strerror(errno));
|
|
|
|
for(i = 0; ; ++i){
|
|
*(corr = corresponding_arg(curr++)) = NULL;
|
|
|
|
if(i == 1 || (r = fork()) == 0)
|
|
execvp(*curr, curr);
|
|
|
|
if( r == -1 /* fork(2) failed */
|
|
|| i == 1 || r == 0 /* exec(3) failed */
|
|
|| dup2(fd, STDIN_FILENO) != STDIN_FILENO)
|
|
return oserr(argv[0], strerror(errno));
|
|
|
|
wait(&child);
|
|
if(!WIFEXITED(child) || WEXITSTATUS(child) != 0)
|
|
return 1;
|
|
|
|
curr = corr + 1;
|
|
}
|
|
|
|
/* UNREACHABLE */
|
|
}
|