10 static int cmd_make(char **argv
, int *ifd
, int *ofd
)
19 if (!(pid
= fork())) {
20 if (ifd
) { /* setting up stdin */
26 if (ofd
) { /* setting up stdout */
32 execvp(argv
[0], argv
);
53 /* execute a command; process input if iproc and process output if oproc */
54 char *cmd_pipe(char *cmd
, char *ibuf
, int iproc
, int oproc
)
56 char *argv
[] = {"/bin/sh", "-c", cmd
, NULL
};
58 struct sbuf
*sb
= NULL
;
60 int ifd
= -1, ofd
= -1;
61 int slen
= iproc
? strlen(ibuf
) : 0;
63 int pid
= cmd_make(argv
, iproc
? &ifd
: NULL
, oproc
? &ofd
: NULL
);
69 signal(SIGINT
, SIG_IGN
);
73 fds
[0].events
= POLLIN
;
75 fds
[1].events
= POLLOUT
;
76 fds
[2].fd
= iproc
? 0 : -1;
77 fds
[2].events
= POLLIN
;
78 while ((fds
[0].fd
>= 0 || fds
[1].fd
>= 0) && poll(fds
, 3, 200) >= 0) {
79 if (fds
[0].revents
& POLLIN
) {
80 int ret
= read(fds
[0].fd
, buf
, sizeof(buf
));
82 sbuf_mem(sb
, buf
, ret
);
85 } else if (fds
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
88 if (fds
[1].revents
& POLLOUT
) {
89 int ret
= write(fds
[1].fd
, ibuf
+ nw
, slen
- nw
);
92 if (ret
<= 0 || nw
== slen
)
94 } else if (fds
[1].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
97 if (fds
[2].revents
& POLLIN
) {
98 int ret
= read(fds
[2].fd
, buf
, sizeof(buf
));
100 for (i
= 0; i
< ret
; i
++)
101 if ((unsigned char) buf
[i
] == TK_CTL('c'))
103 } else if (fds
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
109 waitpid(pid
, NULL
, 0);
112 signal(SIGINT
, SIG_DFL
);
115 return sbuf_done(sb
);
119 int cmd_exec(char *cmd
)
121 cmd_pipe(cmd
, NULL
, 0, 0);