int64_t copy_fd_to_stream(int fd, FILE *output) { int64_t total = 0; while(1) { char buffer[COPY_BUFFER_SIZE]; int64_t actual_read = full_read(fd, buffer, sizeof(buffer)); if(actual_read <= 0) { if(total == 0) return -1; else break; } int64_t actual_write = full_fwrite(output, buffer, actual_read); if(actual_write == -1) { if(total == 0) return -1; else break; } total += actual_write; } return total; }
INT64_T link_stream_to_file(struct link * link, FILE * file, INT64_T length, time_t stoptime) { char buffer[65536]; INT64_T total = 0; INT64_T ractual, wactual; while(length > 0) { INT64_T chunk = MIN(sizeof(buffer), length); ractual = link_read(link, buffer, chunk, stoptime); if(ractual <= 0) break; wactual = full_fwrite(file, buffer, ractual); if(wactual != ractual) { total = -1; break; } total += ractual; length -= ractual; } return total; }
int64_t link_stream_to_file(struct link * link, FILE * file, int64_t length, time_t stoptime) { int64_t total = 0; while(length > 0) { char buffer[1<<16]; size_t chunk = MIN(sizeof(buffer), (size_t)length); ssize_t ractual = link_read(link, buffer, chunk, stoptime); if(ractual <= 0) break; ssize_t wactual = full_fwrite(file, buffer, ractual); if(wactual != ractual) { total = -1; break; } total += ractual; length -= ractual; } return total; }