1//===-- Linux implementation of lseek -------------------------------------===//
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#ifndef LLVM_LIBC_SRC___SUPPORT_FILE_LINUX_LSEEKIMPL_H
10#define LLVM_LIBC_SRC___SUPPORT_FILE_LINUX_LSEEKIMPL_H
11
12#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
13#include "src/__support/common.h"
14#include "src/__support/error_or.h"
15#include "src/errno/libc_errno.h"
16
17#include <stdint.h> // For uint64_t.
18#include <sys/syscall.h> // For syscall numbers.
19#include <unistd.h> // For off_t.
20
21namespace LIBC_NAMESPACE {
22namespace internal {
23
24LIBC_INLINE ErrorOr<off_t> lseekimpl(int fd, off_t offset, int whence) {
25 off_t result;
26#ifdef SYS_lseek
27 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_lseek, ts: fd, ts: offset, ts: whence);
28 result = ret;
29#elif defined(SYS_llseek) || defined(SYS__llseek)
30 static_assert(sizeof(size_t) == 4, "size_t must be 32 bits.");
31#ifdef SYS_llseek
32 constexpr long LLSEEK_SYSCALL_NO = SYS_llseek;
33#elif defined(SYS__llseek)
34 constexpr long LLSEEK_SYSCALL_NO = SYS__llseek;
35#endif
36 off_t offset_64 = offset;
37 int ret = LIBC_NAMESPACE::syscall_impl<int>(
38 LLSEEK_SYSCALL_NO, fd, offset_64 >> 32, offset_64, &result, whence);
39#else
40#error "lseek, llseek and _llseek syscalls not available."
41#endif
42 if (ret < 0)
43 return Error(-ret);
44 return result;
45}
46
47} // namespace internal
48} // namespace LIBC_NAMESPACE
49
50#endif // LLVM_LIBC_SRC___SUPPORT_FILE_LINUX_LSEEKIMPL_H
51

source code of libc/src/__support/File/linux/lseekImpl.h