11 long xread(int fd
, void *buf
, long len
)
15 int ret
= read(fd
, buf
+ nr
, len
- nr
);
16 if (ret
== -1 && (errno
== EAGAIN
|| errno
== EINTR
))
25 long xwrite(int fd
, void *buf
, long len
)
29 int ret
= write(fd
, buf
+ nw
, len
- nw
);
30 if (ret
== -1 && (errno
== EAGAIN
|| errno
== EINTR
))
39 static int xpipe_make(char **argv
, int *ifd
, int *ofd
)
48 if (!(pid
= fork())) {
49 if (ifd
) { /* setting up stdin */
55 if (ofd
) { /* setting up stdout */
61 execvp(argv
[0], argv
);
82 int xpipe(char *cmd
, char *ibuf
, long ilen
, char **obuf
, long *olen
)
84 char *argv
[] = {"/bin/sh", "-c", cmd
, NULL
};
86 struct sbuf
*sb
= NULL
;
88 int ifd
= -1, ofd
= -1;
90 int pid
= xpipe_make(argv
, ibuf
? &ifd
: NULL
, obuf
? &ofd
: NULL
);
95 fcntl(ifd
, F_SETFL
, fcntl(ifd
, F_GETFL
, 0) | O_NONBLOCK
);
97 fds
[0].events
= POLLIN
;
99 fds
[1].events
= POLLOUT
;
100 while ((fds
[0].fd
>= 0 || fds
[1].fd
>= 0) && poll(fds
, 2, 200) >= 0) {
101 if (fds
[0].revents
& POLLIN
) {
102 int ret
= read(fds
[0].fd
, buf
, sizeof(buf
));
104 sbuf_mem(sb
, buf
, ret
);
109 } else if (fds
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
112 if (fds
[1].revents
& POLLOUT
) {
113 int ret
= write(fds
[1].fd
, ibuf
+ nw
, ilen
- nw
);
116 if (ret
<= 0 || nw
== ilen
) {
120 } else if (fds
[1].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
126 waitpid(pid
, NULL
, 0);
128 *olen
= sbuf_len(sb
);
129 *obuf
= sbuf_done(sb
);