9 __weak_alias(popen
, _popen
)
10 __weak_alias(pclose
, _pclose
)
13 #include <sys/types.h>
23 typedef union wait wait_arg
;
29 int _dup2(int oldd
, int newd
); /* not present in System 5 */
30 int _execl(const char *name
, const char *_arg
, ... );
32 int _pipe(int fildes
[2]);
33 pid_t
_wait(wait_arg
*status
);
34 void _exit(int status
);
36 static int pids
[OPEN_MAX
];
44 int Xtype
= *type
== 'r' ? 0 : *type
== 'w' ? 1 : 2;
49 (pid
= _fork()) < 0) return 0;
55 for (p
= pids
; p
< &pids
[OPEN_MAX
]; p
++) {
56 if (*p
) _close((int)(p
- pids
));
59 _dup2(piped
[!Xtype
], !Xtype
);
60 _close(piped
[!Xtype
]);
61 _execl("/bin/sh", "sh", "-c", command
, (char *) 0);
62 _exit(127); /* like system() ??? */
65 pids
[piped
[Xtype
]] = pid
;
66 _close(piped
[!Xtype
]);
67 return fdopen(piped
[Xtype
], type
);
71 #define ret_val status.w_status
73 #define ret_val status
80 int fd
= fileno(stream
);
84 void (*intsave
)(int) = signal(SIGINT
, SIG_IGN
);
85 void (*quitsave
)(int) = signal(SIGQUIT
, SIG_IGN
);
87 while ((wret
= _wait(&status
)) != -1) {
88 if (wret
== pids
[fd
]) break;
90 if (wret
== -1) ret_val
= -1;
91 signal(SIGINT
, intsave
);
92 signal(SIGQUIT
, quitsave
);
107 /* ignore the error on the close() */
108 tmp
= errno
; (void) _close(newd
); errno
= tmp
;
109 while ((fd
= _dup(oldd
)) != newd
) {