1//===-- Linux implementation of sendfile ----------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/sys/sendfile/sendfile.h"
10
11#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12#include "src/__support/common.h"
13
14#include "src/errno/libc_errno.h"
15#include <sys/sendfile.h>
16#include <sys/syscall.h> // For syscall numbers.
17
18namespace LIBC_NAMESPACE {
19
20LLVM_LIBC_FUNCTION(ssize_t, sendfile,
21 (int out_fd, int in_fd, off_t *offset, size_t count)) {
22#ifdef SYS_sendfile
23 ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_sendfile, ts: in_fd,
24 ts: out_fd, ts: offset, ts: count);
25#elif defined(SYS_sendfile64)
26 // Same as sendfile but can handle large offsets
27 static_assert(sizeof(off_t) == 8);
28 ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_sendfile64, in_fd,
29 out_fd, offset, count);
30#else
31#error "sendfile and sendfile64 syscalls not available."
32#endif
33 if (ret < 0) {
34 libc_errno = static_cast<int>(-ret);
35 return -1;
36 }
37 return ret;
38}
39
40} // namespace LIBC_NAMESPACE
41

source code of libc/src/sys/sendfile/linux/sendfile.cpp