Skip to content

Instantly share code, notes, and snippets.

@arunas-cesonis
Created June 15, 2020 18:59
Show Gist options
  • Save arunas-cesonis/4164e738d0b1730e1c750049347bb5c2 to your computer and use it in GitHub Desktop.
Save arunas-cesonis/4164e738d0b1730e1c750049347bb5c2 to your computer and use it in GitHub Desktop.
ssize_t
nxs_sendfile(const int out_fd, const int in_fd,
off_t *offset, const size_t size) {
#if defined(__linux__)
const ssize_t nw = sendfile(out_fd, in_fd, offset, size);
return nw;
#elif defined(__FreeBSD__)
off_t sbytes = 0;
const ssize_t nw = sendfile(in_fd, out_fd, *offset, size,
NULL, &sbytes,
/* FreeBSD: need to wait for file fd read event ? */
/* SF_MNOWAIT | SF_NODISKIO */
0);
*offset += sbytes;
T("out=%d in=%d *offset=%ld size=%lu nw=%ld sbytes=%ld", out_fd, in_fd,
*offset, size, nw, sbytes
);
return sbytes;
#elif defined(__APPLE__)
off_t len = size;
const int r =sendfile(in_fd, out_fd, *offset, &len, NULL, 0);
*offset += len;
return 0 == r ? len : -1;
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment