brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.8 KiB · 47df99a Raw
53 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_FILE_LINUX_LSEEKIMPL_H10#define LLVM_LIBC_SRC___SUPPORT_FILE_LINUX_LSEEKIMPL_H11 12#include "hdr/stdint_proxy.h" // For uint64_t.13#include "hdr/types/off_t.h"14#include "src/__support/OSUtil/syscall.h" // For internal syscall function.15#include "src/__support/common.h"16#include "src/__support/error_or.h"17#include "src/__support/libc_errno.h"18#include "src/__support/macros/config.h"19 20#include <sys/syscall.h> // For syscall numbers.21 22namespace LIBC_NAMESPACE_DECL {23namespace internal {24 25LIBC_INLINE ErrorOr<off_t> lseekimpl(int fd, off_t offset, int whence) {26  off_t result;27#ifdef SYS_lseek28  result = LIBC_NAMESPACE::syscall_impl<off_t>(SYS_lseek, fd, offset, whence);29  if (result < 0)30    return Error(-static_cast<int>(result));31#elif defined(SYS_llseek) || defined(SYS__llseek)32  static_assert(sizeof(size_t) == 4, "size_t must be 32 bits.");33#ifdef SYS_llseek34  constexpr long LLSEEK_SYSCALL_NO = SYS_llseek;35#elif defined(SYS__llseek)36  constexpr long LLSEEK_SYSCALL_NO = SYS__llseek;37#endif38  off_t offset_64 = offset;39  int ret = LIBC_NAMESPACE::syscall_impl<int>(40      LLSEEK_SYSCALL_NO, fd, offset_64 >> 32, offset_64, &result, whence);41  if (ret < 0)42    return Error(-ret);43#else44#error "lseek, llseek and _llseek syscalls not available."45#endif46  return result;47}48 49} // namespace internal50} // namespace LIBC_NAMESPACE_DECL51 52#endif // LLVM_LIBC_SRC___SUPPORT_FILE_LINUX_LSEEKIMPL_H53