00001
00002
00003
00004
00005
00006
00007
00008 #include <errno.h>
00009 #include <stdio.h>
00010 #include <string.h>
00011 #include <unistd.h>
00012
00013 #include "darwin-sendfile.h"
00014
00015 ssize_t sendfile64(int out_fd, int in_fd, __off64_t *offset, size_t size)
00016 {
00017 ssize_t written = 0;
00018
00019 if (*offset)
00020 if (lseek(in_fd, *offset, SEEK_SET) == -1)
00021 return -1;
00022
00023 while (size > 0)
00024 {
00025 char buf[60000], *p;
00026 ssize_t in_count, out_count, wanted;
00027
00028 wanted = (size > sizeof buf) ? (sizeof buf) : size;
00029 in_count = read(in_fd, buf, (size_t) wanted);
00030
00031 if (in_count == -1)
00032 {
00033 fprintf(stderr, "sendfile64(): failed to read %ld bytes: %s",
00034 (long) wanted, strerror(errno));
00035 return -1;
00036 }
00037 else if (in_count == 0)
00038 break;
00039
00040 size -= in_count;
00041 p = buf;
00042
00043 while (in_count > 0)
00044 {
00045 out_count = write(out_fd, p, (size_t) in_count);
00046 if (out_count == -1 || out_count == 0)
00047 {
00048 fprintf(stderr, "sendfile64(): failed to write %ld bytes: %s",
00049 (long) in_count, strerror(errno));
00050 return -1;
00051 }
00052 in_count -= out_count;
00053 p += out_count;
00054 written += out_count;
00055 }
00056 }
00057
00058 *offset += written;
00059 return written;
00060 }