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