start work on pspipe(1) again
This commit is contained in:
parent
0fe87b68e8
commit
563234721b
@ -1,13 +1,18 @@
|
||||
all: pscat
|
||||
|
||||
pscat: pscat.o ../libpsargs/libpsargs.o
|
||||
$(CC) $(CFLAGS) -g -o pscat ../libpsargs/libpsargs.o pscat.o
|
||||
|
||||
pscat.o: pscat.c
|
||||
$(CC) $(CFLAGS) -c -I../libpsargs -o pscat.o pscat.c
|
||||
$(CC) $(CFLAGS) -c -DPSCAT=1 -I../libpsargs -o pscat.o pscat.c
|
||||
|
||||
pspipe: pspipe.o ../libpsargs/libpsargs.o
|
||||
$(CC) $(CFLAGS) -g -o pspipe ../libpsargs/libpsargs.o pspipe.o
|
||||
|
||||
pspipe.o: pscat.c
|
||||
$(CC) $(CFLAGS) -c -DPSPIPE=1 -I../libpsargs -o pspipe.o pscat.c
|
||||
|
||||
../libpsargs/libpsargs.o:
|
||||
$(MAKE) -C ../libpsargs
|
||||
|
||||
clean:
|
||||
rm -f *.o pscat
|
||||
|
||||
.PHONY: clean
|
||||
.PHONY: all
|
||||
|
@ -1,43 +1,94 @@
|
||||
#include <errno.h> /* errno */
|
||||
#include <stdio.h> /* fprintf(3), stderr */
|
||||
#include <string.h> /* strerror(3) */
|
||||
#include <sysexits.h> /* EX_USAGE */
|
||||
#include <unistd.h> /* execvp(3) */
|
||||
#include <sys/types.h> /* <sys/wait.h> */
|
||||
#include <sys/wait.h> /* wait(2) */
|
||||
|
||||
#include "libpsargs.h"
|
||||
|
||||
static char *program_name = "pscat";
|
||||
#include "libpsargs.h" /* check_arg(3), corresponding_arg(3) */
|
||||
|
||||
#define L_PAREN '['
|
||||
#define R_PAREN ']'
|
||||
|
||||
#if defined(PSCAT)
|
||||
static char *program_name = "pscat";
|
||||
|
||||
int
|
||||
forking(char **argv){
|
||||
f(char **argv){
|
||||
int child;
|
||||
char **corr;
|
||||
|
||||
corr = corresponding_arg(argv, L_PAREN, R_PAREN);
|
||||
*corr = NULL;
|
||||
if(fork() == 0){
|
||||
execvp(argv[1], argv+1);
|
||||
}else{
|
||||
*(corr = corresponding_arg(argv, L_PAREN, R_PAREN)) = NULL;
|
||||
switch(fork()){
|
||||
case -1:
|
||||
fprintf(stderr, "%s: %s\n", program_name, strerror(errno));
|
||||
return EX_OSERR;
|
||||
case 0: execvp(argv[1], argv+1);
|
||||
default:
|
||||
wait(&child);
|
||||
argv = corr;
|
||||
if(*++argv == NULL)
|
||||
return WIFEXITED(child) * WEXITSTATUS(child);
|
||||
else
|
||||
return forking(argv);
|
||||
return *++argv == NULL
|
||||
? WIFEXITED(child) * WEXITSTATUS(child)
|
||||
: f(argv);
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined(PSPIPE)
|
||||
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){
|
||||
int child;
|
||||
char **corr;
|
||||
static int fd[2];
|
||||
int r;
|
||||
|
||||
*(corr = corresponding_arg(argv, L_PAREN, R_PAREN)) = 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){
|
||||
printf(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);
|
||||
else{
|
||||
argv = corr;
|
||||
return *++argv == NULL
|
||||
? WIFEXITED(child) * WEXITSTATUS(child)
|
||||
: f(argv);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
|
||||
if(argc != 0)
|
||||
program_name = argv[0];
|
||||
if(check_arg(++argv, L_PAREN, R_PAREN) == 0){
|
||||
fprintf(stderr,
|
||||
"Usage: %s \"[\" [utility [argument...]] \"]\" ...\n",
|
||||
argv[-1] == NULL ? program_name : argv[-1]
|
||||
program_name
|
||||
);
|
||||
return EX_USAGE;
|
||||
}else
|
||||
return forking(argv);
|
||||
}else return f(argv);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user