- size_t dataoff, smax;
- off_t sent;
- int eof, ret;
-
- sent = 0;
- eof = 0;
- while(!eof || (buf->d > 0)) {
- if(!eof && (buf->d < buf->s) && ((max < 0) || (sent + buf->d < max))) {
- while(1) {
- ret = recv(src, buf->b + buf->d, buf->s - buf->d, MSG_DONTWAIT);
- if((ret < 0) && (errno == EAGAIN)) {
- } else if(ret < 0) {
- return(-1);
- } else if(ret == 0) {
- eof = 1;
- break;
- } else {
- buf->d += ret;
- break;
- }
- if(buf->d > 0)
- break;
- if(block(src, EV_READ, 0) <= 0)
- return(-1);
- }
- }
- for(dataoff = 0; (dataoff < buf->d) && ((max < 0) || (sent < max));) {
- if(block(dst, EV_WRITE, 120) <= 0)
- return(-1);
- smax = buf->d - dataoff;
- if(sent + smax > max)
- smax = max - sent;
- ret = send(dst, buf->b + dataoff, smax, MSG_NOSIGNAL | MSG_DONTWAIT);
- if(ret < 0)
- return(-1);
- dataoff += ret;
- sent += ret;
- }
- bufeat(*buf, dataoff);
+ size_t read;
+ off_t total;
+ char buf[8192];
+
+ total = 0;
+ while(!feof(in) && ((max < 0) || (total < max))) {
+ read = sizeof(buf);
+ if(max >= 0)
+ read = min(max - total, read);
+ read = fread(buf, 1, read, in);
+ if(ferror(in))
+ return(-1);
+ if(fwrite(buf, 1, read, out) != read)
+ return(-1);
+ total += read;