/* $Header:untty.c 12.0$ */
/* $ACIS:untty.c 12.0$ */
/* $Source: /ibm/acis/usr/src/ibm/RCS/untty.c,v $ */

#ifndef lint
static char *rcsid = "$Header:untty.c 12.0$";
#endif

/*
 * 5799-WZQ (C) COPYRIGHT IBM CORPORATION 1987
 * LICENSED MATERIALS - PROPERTY OF IBM
 * REFER TO COPYRIGHT INSTRUCTIONS FORM NUMBER G120-2083
 */
#include <sgtty.h>
#include <signal.h>
#include <stdio.h>

main(argc,argv)

int argc;
char **argv;

{
	int pid,tty,filedesi[2];

	if (argc<2)
	{
		fprintf(stderr,"Usage: %s command [ args ... ]\n",argv[0]);
		exit(1);
	}

	pipe(filedesi);
        
	if ((pid=fork())==0)
	{
		close(filedesi[0]);
		fdopen(filedesi[1],"w");
		dup2(filedesi[1],1);
		close(filedesi[1]);
		while (!feof(stdin))
		{
			putc(getchar(),stdout);
			fflush(stdout);
		}
		_exit(0);
	}
	tty=open("/dev/tty",1);
	ioctl(tty,TIOCNOTTY,0);
	close(tty);
        
	close(filedesi[1]);
	fdopen(filedesi[0],"r");
	dup2(filedesi[0],0);
	close(filedesi[0]);
	argv++;
	execvp(argv[0],argv);
	fprintf(stderr,"%s: Can't exec %s\n",argv[-1],argv[0]);
	kill(pid,SIGTERM);
	exit(1);
}
