forked from googleprojectzero/halfempty
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendfile_generic.c
More file actions
41 lines (32 loc) · 861 Bytes
/
sendfile_generic.c
File metadata and controls
41 lines (32 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <glib.h>
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
{
char buf[BUFSIZ];
size_t total = 0;
if (lseek(in_fd, *offset, SEEK_SET) < 0)
return -1;
while (total < count) {
ssize_t r = 0;
ssize_t w = 0;
size_t written = 0;
size_t next = sizeof(buf);
if (next > count - total)
next = count - total;
r = read(in_fd, buf, next);
if (r < 0)
return r;
while (written < (size_t)r) {
w = write(out_fd, buf, (size_t)r);
if (w < 0)
return w;
written += (size_t)w;
g_assert_cmpint(w, <=, r);
r -= w;
}
total += written;
}
return total;
}