* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)popen.c 5.4 (Berkeley) 3/26/86";
#endif LIBC_SCCS and not lint
#define tst(a,b) (*mode == 'r'? (b) : (a))
int myside
, hisside
, pid
;
nfiles
= getdtablesize();
popen_pid
= (int *)malloc(nfiles
* sizeof *popen_pid
);
for (pid
= 0; pid
< nfiles
; pid
++)
myside
= tst(p
[WTR
], p
[RDR
]);
hisside
= tst(p
[RDR
], p
[WTR
]);
if ((pid
= vfork()) == 0) {
/* myside and hisside reverse roles in child */
if (hisside
!= tst(0, 1)) {
dup2(hisside
, tst(0, 1));
execl("/bin/sh", "sh", "-c", cmd
, (char *)NULL
);
return (fdopen(myside
, mode
));
int child
, pid
, status
, omask
;
child
= popen_pid
[fileno(ptr
)];
popen_pid
[fileno(ptr
)] = -1;
omask
= sigblock(sigmask(SIGINT
)|sigmask(SIGQUIT
)|sigmask(SIGHUP
));
while ((pid
= wait(&status
)) != child
&& pid
!= -1)
(void) sigsetmask(omask
);
return (pid
== -1 ? -1 : status
);