-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendfile.c
More file actions
55 lines (46 loc) · 1.03 KB
/
sendfile.c
File metadata and controls
55 lines (46 loc) · 1.03 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* A replacement for sendfile(2) using read(2), write(2) and lseek(2).
* Attempts to at least account for EINTR.
*/
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define BUFSZ 32
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count) {
off_t os;
ssize_t nread, nwrite, total;
char buf[BUFSZ], *start, *end;
os = 0;
if (offset != NULL)
os = *offset;
if (lseek(in_fd, os, SEEK_SET) == -1)
return -1;
total = count;
while (total > 0 && ((nread = read(in_fd, buf, BUFSZ)) > 0
|| errno == EINTR)) {
if (nread == -1) //EINTR
continue;
start = buf;
if (nread > total)
nread = total;
end = start + nread;
while (start != end) {
if ((nwrite = write(out_fd, buf, nread)) == -1) {
if (errno == EINTR)
continue;
else
return -1;
}
start += nwrite;
}
total -= nread;
}
return count - total;
}
int main(int argc, char **argv) {
int fd;
fd = open(argv[1], O_RDONLY);
sendfile(STDOUT_FILENO, fd, NULL, 100);
exit(EXIT_SUCCESS);
}