xref: /titanic_51/usr/src/lib/libbc/libc/gen/common/popen.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1995, by Sun Microsystems, Inc.
3*7c478bd9Sstevel@tonic-gate  * All rights reserved.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*7c478bd9Sstevel@tonic-gate 	  /* from UCB 5.2 85/06/05 */
8*7c478bd9Sstevel@tonic-gate 
9*7c478bd9Sstevel@tonic-gate /*
10*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
11*7c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
12*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
13*7c478bd9Sstevel@tonic-gate  */
14*7c478bd9Sstevel@tonic-gate 
15*7c478bd9Sstevel@tonic-gate #include <stdio.h>
16*7c478bd9Sstevel@tonic-gate #include <signal.h>
17*7c478bd9Sstevel@tonic-gate #include <vfork.h>
18*7c478bd9Sstevel@tonic-gate 
19*7c478bd9Sstevel@tonic-gate #define	tst(a,b)	(*mode == 'r'? (b) : (a))
20*7c478bd9Sstevel@tonic-gate #define	RDR	0
21*7c478bd9Sstevel@tonic-gate #define	WTR	1
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate extern	char *malloc();
24*7c478bd9Sstevel@tonic-gate extern	int execl(), vfork(), pipe(), close(), fcntl();
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate static	int *popen_pid;
27*7c478bd9Sstevel@tonic-gate static	int nfiles;
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate FILE *
30*7c478bd9Sstevel@tonic-gate popen(cmd,mode)
31*7c478bd9Sstevel@tonic-gate 	char *cmd;
32*7c478bd9Sstevel@tonic-gate 	char *mode;
33*7c478bd9Sstevel@tonic-gate {
34*7c478bd9Sstevel@tonic-gate 	int p[2];
35*7c478bd9Sstevel@tonic-gate 	register int *poptr;
36*7c478bd9Sstevel@tonic-gate 	register int myside, hisside, pid;
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate 	if (nfiles <= 0)
39*7c478bd9Sstevel@tonic-gate 		nfiles = getdtablesize();
40*7c478bd9Sstevel@tonic-gate 	if (popen_pid == NULL) {
41*7c478bd9Sstevel@tonic-gate 		popen_pid = (int *)malloc(nfiles * sizeof *popen_pid);
42*7c478bd9Sstevel@tonic-gate 		if (popen_pid == NULL)
43*7c478bd9Sstevel@tonic-gate 			return (NULL);
44*7c478bd9Sstevel@tonic-gate 		for (pid = 0; pid < nfiles; pid++)
45*7c478bd9Sstevel@tonic-gate 			popen_pid[pid] = -1;
46*7c478bd9Sstevel@tonic-gate 	}
47*7c478bd9Sstevel@tonic-gate 	if (pipe(p) < 0)
48*7c478bd9Sstevel@tonic-gate 		return (NULL);
49*7c478bd9Sstevel@tonic-gate 	myside = tst(p[WTR], p[RDR]);
50*7c478bd9Sstevel@tonic-gate 	hisside = tst(p[RDR], p[WTR]);
51*7c478bd9Sstevel@tonic-gate 	if ((pid = vfork()) == 0) {
52*7c478bd9Sstevel@tonic-gate 		/* myside and hisside reverse roles in child */
53*7c478bd9Sstevel@tonic-gate 		int	stdio;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 		/* close all pipes from other popen's */
56*7c478bd9Sstevel@tonic-gate 		for (poptr = popen_pid; poptr < popen_pid+nfiles; poptr++) {
57*7c478bd9Sstevel@tonic-gate 			if(*poptr >= 0)
58*7c478bd9Sstevel@tonic-gate 				close(poptr - popen_pid);
59*7c478bd9Sstevel@tonic-gate 		}
60*7c478bd9Sstevel@tonic-gate 		stdio = tst(0, 1);
61*7c478bd9Sstevel@tonic-gate 		(void) close(myside);
62*7c478bd9Sstevel@tonic-gate 		if (hisside != stdio) {
63*7c478bd9Sstevel@tonic-gate 			(void) dup2(hisside, stdio);
64*7c478bd9Sstevel@tonic-gate 			(void) close(hisside);
65*7c478bd9Sstevel@tonic-gate 		}
66*7c478bd9Sstevel@tonic-gate 		(void) execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
67*7c478bd9Sstevel@tonic-gate 		_exit(127);
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 	if (pid == -1) {
70*7c478bd9Sstevel@tonic-gate 		close(myside);
71*7c478bd9Sstevel@tonic-gate 		close(hisside);
72*7c478bd9Sstevel@tonic-gate 		return (NULL);
73*7c478bd9Sstevel@tonic-gate 	}
74*7c478bd9Sstevel@tonic-gate 	popen_pid[myside] = pid;
75*7c478bd9Sstevel@tonic-gate 	close(hisside);
76*7c478bd9Sstevel@tonic-gate 	return (fdopen(myside, mode));
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate int
80*7c478bd9Sstevel@tonic-gate pclose(ptr)
81*7c478bd9Sstevel@tonic-gate 	FILE *ptr;
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	int child = -1;
84*7c478bd9Sstevel@tonic-gate 	int pid, status, omask;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	if (popen_pid != NULL) {
87*7c478bd9Sstevel@tonic-gate 		child = popen_pid[fileno(ptr)];
88*7c478bd9Sstevel@tonic-gate 		popen_pid[fileno(ptr)] = -1;
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 	fclose(ptr);
91*7c478bd9Sstevel@tonic-gate 	if (child == -1)
92*7c478bd9Sstevel@tonic-gate 		return (-1);
93*7c478bd9Sstevel@tonic-gate 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
94*7c478bd9Sstevel@tonic-gate 	while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
95*7c478bd9Sstevel@tonic-gate 		;
96*7c478bd9Sstevel@tonic-gate 	(void) sigsetmask(omask);
97*7c478bd9Sstevel@tonic-gate 	return (pid == -1 ? -1 : status);
98*7c478bd9Sstevel@tonic-gate }
99