/*
* wrapper cmd [args]..
*
* This program forks off a subprocess and feeds it data  from  stdin.
* When we get EOF on stdin, we exit, leaving the subprocess behind as
* an orphan.
*
* This is a pure kludge to solve some  of  the  problems  in  process
* handling  in  both tcl and perl.  We use execvp() so that the usual
* search path is searched for the cmd name.
*
* Eventually we may do one that handles both stdin and stdout. That's
* a bit trickier, though, because it entails a select() loop.
*
* AUTHOR
*   John Chambers <jc@trillian.mit.edu>
*/
#include "V.h"

int p[2];

main(ac,av) char ** av;
{	int  a, c, x;

	setlinebuf(stdout);
	ac = Vinit(ac,av);
	a = 1;

	if (Pipe(p) < 0) {
		fprintf(stderr,"%s: Can't create pipe [Err %d=%s=%s]\n",pname,Errinfo);
		Exit(errno);
	}

	if (c = Fork()) {
		V3 "Parent process %d.",mypid D;
		Dup2(p[1],1);
		Close(p[0]);
		while ((x = getchar()) != EOF) {
			putchar(x);
		}
		fflush(stdout);
		Exit(0);
	} else {
		V3 "Child process %d.",c D;
		Dup2(p[0],0);
		Close(p[1]);
		Execvp(av[a],av+a);
		V1 "Can't exec \"%s\" ... [Err %d=%s=%s]",av[a],Errinfo D;
		Exit(errno);
	}

	Exit(0);
}
