11 static int cmd_make(char **argv
, int *ifd
, int *ofd
)
20 if (!(pid
= fork())) {
21 if (ifd
) { /* setting up stdin */
27 if (ofd
) { /* setting up stdout */
33 execvp(argv
[0], argv
);
54 /* execute a command; process input if iproc and process output if oproc */
55 char *cmd_pipe(char *cmd
, char *ibuf
, int iproc
, int oproc
)
57 char *argv
[] = {"/bin/sh", "-c", cmd
, NULL
};
59 struct sbuf
*sb
= NULL
;
61 int ifd
= -1, ofd
= -1;
62 int slen
= iproc
? strlen(ibuf
) : 0;
64 int pid
= cmd_make(argv
, iproc
? &ifd
: NULL
, oproc
? &ofd
: NULL
);
70 signal(SIGINT
, SIG_IGN
);
73 fcntl(ifd
, F_SETFL
, fcntl(ifd
, F_GETFL
, 0) | O_NONBLOCK
);
75 fds
[0].events
= POLLIN
;
77 fds
[1].events
= POLLOUT
;
78 fds
[2].fd
= iproc
? 0 : -1;
79 fds
[2].events
= POLLIN
;
80 while ((fds
[0].fd
>= 0 || fds
[1].fd
>= 0) && poll(fds
, 3, 200) >= 0) {
81 if (fds
[0].revents
& POLLIN
) {
82 int ret
= read(fds
[0].fd
, buf
, sizeof(buf
));
84 sbuf_mem(sb
, buf
, ret
);
87 } else if (fds
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
90 if (fds
[1].revents
& POLLOUT
) {
91 int ret
= write(fds
[1].fd
, ibuf
+ nw
, slen
- nw
);
94 if (ret
<= 0 || nw
== slen
)
96 } else if (fds
[1].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
99 if (fds
[2].revents
& POLLIN
) {
100 int ret
= read(fds
[2].fd
, buf
, sizeof(buf
));
102 for (i
= 0; i
< ret
; i
++)
103 if ((unsigned char) buf
[i
] == TK_CTL('c'))
105 } else if (fds
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
111 waitpid(pid
, NULL
, 0);
114 signal(SIGINT
, SIG_DFL
);
117 return sbuf_done(sb
);
121 int cmd_exec(char *cmd
)
123 cmd_pipe(cmd
, NULL
, 0, 0);