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 and stderr */
35 execvp(argv
[0], argv
);
57 * Execute a shell command.
59 * If ibuf is given, it is passed as standard input to the process.
60 * Otherwise, the process reads from the terminal.
62 * If oproc is 0, the process writes directly to the terminal. If it
63 * is 1, process' output is saved and returned. If it is 2, in addition
64 * to returning the output, it is written to the terminal.
66 char *cmd_pipe(char *cmd
, char *ibuf
, int oproc
)
68 char *argv
[] = {"/bin/sh", "-c", cmd
, NULL
};
70 struct sbuf
*sb
= NULL
;
72 int ifd
= -1, ofd
= -1;
73 int slen
= ibuf
!= NULL
? strlen(ibuf
) : 0;
75 int pid
= cmd_make(argv
, ibuf
!= NULL
? &ifd
: NULL
, oproc
? &ofd
: NULL
);
81 signal(SIGINT
, SIG_IGN
);
84 fcntl(ifd
, F_SETFL
, fcntl(ifd
, F_GETFL
, 0) | O_NONBLOCK
);
86 fds
[0].events
= POLLIN
;
88 fds
[1].events
= POLLOUT
;
89 fds
[2].fd
= ibuf
!= NULL
? 0 : -1;
90 fds
[2].events
= POLLIN
;
91 while ((fds
[0].fd
>= 0 || fds
[1].fd
>= 0) && poll(fds
, 3, 200) >= 0) {
92 if (fds
[0].revents
& POLLIN
) {
93 int ret
= read(fds
[0].fd
, buf
, sizeof(buf
));
94 if (ret
> 0 && oproc
== 2)
97 sbuf_mem(sb
, buf
, ret
);
102 } else if (fds
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
106 if (fds
[1].revents
& POLLOUT
) {
107 int ret
= write(fds
[1].fd
, ibuf
+ nw
, slen
- nw
);
110 if (ret
<= 0 || nw
== slen
) {
114 } else if (fds
[1].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
118 if (fds
[2].revents
& POLLIN
) {
119 int ret
= read(fds
[2].fd
, buf
, sizeof(buf
));
121 for (i
= 0; i
< ret
; i
++)
122 if ((unsigned char) buf
[i
] == TK_CTL('c'))
124 } else if (fds
[2].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
130 waitpid(pid
, NULL
, 0);
133 signal(SIGINT
, SIG_DFL
);
136 return sbuf_done(sb
);
140 int cmd_exec(char *cmd
)
142 cmd_pipe(cmd
, NULL
, 0);