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 char *cmd_pipe(char *cmd
, char *s
)
55 char *argv
[] = {"/bin/sh", "-c", cmd
, NULL
};
62 int pid
= cmd_make(argv
, &ifd
, &ofd
);
67 fds
[0].events
= POLLIN
;
69 fds
[1].events
= POLLOUT
;
71 fds
[2].events
= POLLIN
;
72 while ((fds
[0].fd
>= 0 || fds
[1].fd
>= 0) && poll(fds
, 3, 200) >= 0) {
73 if (fds
[0].revents
& POLLIN
) {
74 int ret
= read(fds
[0].fd
, buf
, sizeof(buf
));
76 sbuf_mem(sb
, buf
, ret
);
79 } else if (fds
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
82 if (fds
[1].revents
& POLLOUT
) {
83 int ret
= write(fds
[1].fd
, s
+ nw
, slen
- nw
);
86 if (ret
<= 0 || nw
== slen
)
88 } else if (fds
[1].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
91 if (fds
[2].revents
& POLLIN
) {
92 int ret
= read(fds
[2].fd
, buf
, sizeof(buf
));
94 for (i
= 0; i
< ret
; i
++)
95 if ((unsigned char) buf
[i
] == TK_CTL('c'))
97 } else if (fds
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
103 waitpid(pid
, NULL
, 0);
104 return sbuf_done(sb
);
107 int cmd_exec(char *cmd
)
109 char *argv
[] = {"/bin/sh", "-c", cmd
, NULL
};
110 int pid
= cmd_make(argv
, NULL
, NULL
);
113 signal(SIGINT
, SIG_IGN
);
116 waitpid(pid
, NULL
, 0);
117 printf("[terminated]\n");