sendfile_linux.c (748B)
- // 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/sendfile.h>
- // generic
- #include <errno.h>
- #include <stddef.h> // NULL
- #include <sys/types.h> // ssize_t, off_t
- ssize_t
- auto_fd_copy(int fd_in, int fd_out, size_t len)
- {
- off_t *off = NULL;
- while(1)
- {
- ssize_t ret = sendfile(fd_out, fd_in, off, len);
- if(ret < 0)
- {
- switch(errno)
- {
- case EINVAL:
- errno = 0;
- return manual_copy(fd_in, fd_out, len, 0);
- case EAGAIN:
- errno = 0;
- continue;
- default:
- return ret;
- }
- }
- return ret;
- }
- return -1;
- }