sendfile_freebsd.c (910B)
- // utils-std: Collection of commonly available Unix tools
- // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- // SPDX-License-Identifier: MPL-2.0
- #define _DEFAULT_SOURCE
- #include "./fs.h"
- // for sendfile
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <sys/uio.h>
- // generic
- #include <errno.h>
- #include <stddef.h> // NULL
- #include <limits.h> // SSIZE_MAX
- ssize_t
- auto_fd_copy(int fd_in, int fd_out, size_t len)
- {
- off_t off = 0;
- if(len == SSIZE_MAX) len = 0;
- while(1)
- {
- off_t sbytes = 0;
- ssize_t ret = sendfile(fd_out, fd_in, off, len, NULL, &sbytes, 0);
- if(ret < 0)
- {
- switch(errno)
- {
- case EINVAL:
- case EBADF:
- errno = 0;
- return manual_copy(fd_in, fd_out, len, 0);
- case EAGAIN:
- errno = 0;
- off += sbytes;
- if(len != 0) len -= sbytes;
- continue;
- default:
- return ret;
- }
- }
- return ret;
- }
- return -1;
- }