/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred."
 *
 */
/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"@(#)libc-port:stdio/popen.c	1.13"
/*LINTLIBRARY*/
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>

#define	tst(a,b) (*mode == 'r'? (b) : (a))
#define	RDR	0
#define	WTR	1

struct popen_pid {		/* structure to store the pipes */
	int pid;
	int fd;
	struct popen_pid *next_pid;
};

extern FILE *fdopen();
extern int execl(), fork(), pipe(), close(), fcntl();

static char bin_shell[] = "/bin/sh" ;
static char shell[] = "sh";
static char shflg[] = "-c";

struct popen_pid *first_pid = 0;

FILE *
popen(cmd, mode)
char	*cmd, *mode;
{
	int	p[2];
	register struct popen_pid *poptr, *ptr;
	register int myside, yourside, pid;

	if(pipe(p) < 0)
		return(NULL);
	myside = tst(p[WTR], p[RDR]);
	yourside = tst(p[RDR], p[WTR]);
	if((pid = fork()) == 0) {
		/* myside and yourside reverse roles in child */
		int	stdio;

		/* close all pipes from other popen's */
		poptr = first_pid;
		while (poptr) {
			if (poptr->pid)
				close(poptr->fd);
			poptr = poptr->next_pid;
		}
		stdio = tst(0, 1);
		(void) close(myside);
		(void) close(stdio);
		(void) fcntl(yourside, F_DUPFD, stdio);
		(void) close(yourside);
		(void) execl(bin_shell, shell, shflg, cmd, (char *)0);
		_exit(1);
	}
	if(pid == -1)
		return(NULL);
	ptr = (struct popen_pid *)calloc(1, sizeof *ptr);
	ptr->pid = pid;
	ptr->fd = myside;
	ptr->next_pid = 0;
	if (!first_pid)
		first_pid = ptr;
	else { 
		poptr = first_pid;
		while (poptr->next_pid) 
			poptr = poptr->next_pid;
		poptr->next_pid = ptr;
	}
	(void) close(yourside);
	return(fdopen(myside, mode));
}

int
pclose(ptr)
FILE	*ptr;
{
	register int f, r;
	register struct popen_pid *poptr, *prev = 0;
	int status;
	void (*hstat)(), (*istat)(), (*qstat)();

	f = fileno(ptr);
	(void) fclose(ptr);
	istat = signal(SIGINT, SIG_IGN);
	qstat = signal(SIGQUIT, SIG_IGN);
	hstat = signal(SIGHUP, SIG_IGN);

	poptr = first_pid;
	while (poptr && poptr->fd != f) {
		prev = poptr;
		poptr = poptr->next_pid; 
	}
	if (!poptr)			/* There are no pending processes */
		return(-1);
	/* while the child is not done and no error has occured wait in the loop*/
	while ((r = waitpid(poptr->pid,&status,0)) && errno == EINTR)
		;
	if(r == -1)
		status = -1;
	(void) signal(SIGINT, istat);
	(void) signal(SIGQUIT, qstat);
	(void) signal(SIGHUP, hstat);
	/* mark this pipe closed */
	if (!prev) 
		first_pid = poptr->next_pid;
	else 
		prev->next_pid = poptr->next_pid;
	poptr->pid = 0;
	free(poptr);
	return(status);
}
