#include #include #include #include #include #include #include #define PATH_SEP '/' #ifdef _BSD_ # include extern char *sys_errlist[]; # define raise(s) kill(getpid(),(s)) # define strerror(e) sys_errlist[(e)] #endif static char __ident[] = "@(#)(c)SPDsoft, Wed Mar 15, 1995"; #define VERS_STR ((char*)&__ident[4]) void usage(void); int launch( char *cmd[], int *status ); extern int errno; char *app; main(int argc, char **argv ) { int i, opt, status; int unmount=0; int quiet=0; struct stat sbp; extern char *optarg; extern int optind,opterr; if ((app = strrchr(argv[0], PATH_SEP)) != NULL) app = app+1; else app = argv[0]; while ( (opt=getopt(argc,argv,"hV")) != EOF ) { switch(opt) { case 'V': fprintf(stderr, "%s: %s\n", app, VERS_STR); exit(0); break; case 'h': default: usage(); break; } } if ((argc-optind)<1) usage(); launch(&(argv[1]),&status); if(status!=0) { fprintf(stderr,"%s: %s exit with %d (%s)\n", app, argv[1], status, strerror(status)); exit(status); } exit(0); } void usage(void) { fprintf(stderr,"Use: %s [-h] cmd\n", app); fprintf(stderr,"%s: -h : This text\n", app); exit(0); } int launch( char *cmd[], int *status ) { int res=0; int st,i; int pid, wpid; int pipefd[2]; char s[256]; pipe (pipefd); switch ( pid = fork() ) { case -1: /* error */ perror ( "fork" ); res = -1; break; case 0: /* son */ close(1); /* close stdout */ dup(pipefd[1]); /* so first fd, which is stdout gets pipefd */ close(pipefd[0]); execv ( cmd[0], &cmd[0] ); fprintf(stderr, "%s: execv %s: %s\n", app, cmd[0], strerror(errno)); exit(-2); break; default: close(0); /* close stdin */ dup(pipefd[0]); close(pipefd[1]); while ( 0<(i=read(0,s,sizeof(s)))) { fprintf(stdout, "\n### CHUNK % .3d ###\n",i); fflush(stdout); write(1,s,sizeof(s)); } *status = 0; while ( pid !=(wpid= wait ( &st )) ) if (WIFSTOPPED(st)) { fprintf(stderr,"%s: %s stoped %d\n", app, cmd[0], WSTOPSIG(st)); res = -6; } else if(WIFEXITED(st)) { *status = WEXITSTATUS(st); } else if(WIFSIGNALED(st)) { fprintf(stderr,"%s: %s signaled %d\n", app, cmd[0],WTERMSIG(st)); res = -6; } if(st&0200) fprintf(stderr, "%s: %s core dumped\n", app, cmd[0]); break; } /* switch */ return res; }