| 1 |
//===-- Linux implementation of pwrite ------------------------------------===// |
| 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/unistd/pwrite.h" |
| 10 |
|
| 11 |
#include "src/__support/OSUtil/syscall.h" // For internal syscall function. |
| 12 |
#include "src/__support/common.h" |
| 13 |
|
| 14 |
#include "hdr/stdint_proxy.h" // For uint64_t. |
| 15 |
#include "src/__support/libc_errno.h" |
| 16 |
#include "src/__support/macros/config.h" |
| 17 |
#include <sys/syscall.h> // For syscall numbers. |
| 18 |
|
| 19 |
namespace LIBC_NAMESPACE_DECL { |
| 20 |
|
| 21 |
LLVM_LIBC_FUNCTION(ssize_t, pwrite, |
| 22 |
(int fd, const void *buf, size_t count, off_t offset)) { |
| 23 |
|
| 24 |
ssize_t ret; |
| 25 |
if constexpr (sizeof(long) == sizeof(uint32_t) && |
| 26 |
sizeof(off_t) == sizeof(uint64_t)) { |
| 27 |
// This is a 32-bit system with a 64-bit offset, offset must be split. |
| 28 |
const uint64_t bits = cpp::bit_cast<uint64_t>(offset); |
| 29 |
const uint32_t lo = bits & UINT32_MAX; |
| 30 |
const uint32_t hi = bits >> 32; |
| 31 |
const long offset_low = cpp::bit_cast<long>(static_cast<long>(lo)); |
| 32 |
const long offset_high = cpp::bit_cast<long>(static_cast<long>(hi)); |
| 33 |
ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_pwrite64, fd, buf, count, |
| 34 |
offset_low, offset_high); |
| 35 |
} else { |
| 36 |
ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_pwrite64, fd, buf, count, |
| 37 |
offset); |
| 38 |
} |
| 39 |
|
| 40 |
if (ret < 0) { |
| 41 |
libc_errno = static_cast<int>(-ret); |
| 42 |
return -1; |
| 43 |
} |
| 44 |
return ret; |
| 45 |
} |
| 46 |
|
| 47 |
} // namespace LIBC_NAMESPACE_DECL |