+ int total = 0;
+ fd_set w_fds, r_fds;
+ int fd_count, count;
+ struct timeval tv;
+
+ no_flush++;
+
+ while (total < len) {
+ FD_ZERO(&w_fds);
+ FD_ZERO(&r_fds);
+ FD_SET(fd,&w_fds);
+ fd_count = fd;
+
+ if (io_error_fd != -1) {
+ FD_SET(io_error_fd,&r_fds);
+ if (io_error_fd > fd_count)
+ fd_count = io_error_fd;
+ }
+
+ tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
+ tv.tv_usec = 0;
+
+ errno = 0;
+
+ count = select(fd_count+1,
+ io_error_fd != -1?&r_fds:NULL,
+ &w_fds,NULL,
+ &tv);
+
+ if (count <= 0) {
+ if (errno == EBADF) {
+ exit_cleanup(RERR_SOCKETIO);
+ }
+ check_timeout();
+ continue;
+ }
+
+ if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
+ read_error_fd();
+ }
+
+ if (FD_ISSET(fd, &w_fds)) {
+ int ret, n = len-total;
+
+ if (n > PIPE_BUF) n = PIPE_BUF;
+
+ ret = write(fd,buf+total,n?n:1);
+
+ if (ret == -1 && errno == EINTR) {
+ continue;
+ }
+
+ if (ret <= 0) {
+ rprintf(FERROR,"erroring writing %d bytes - exiting\n", len);
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ total += ret;
+
+ if (io_timeout)
+ last_io = time(NULL);
+ }
+ }
+
+ no_flush--;