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 #include "src/unistd/lseek.h"
10 
11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12 #include "src/__support/common.h"
13 
14 #include <errno.h>
15 #include <sys/syscall.h> // For syscall numbers.
16 #include <unistd.h>
17 
18 namespace __llvm_libc {
19 
20 LLVM_LIBC_FUNCTION(off_t, lseek, (int fd, off_t offset, int whence)) {
21   off_t result;
22 #ifdef SYS_lseek
23   long ret = __llvm_libc::syscall(SYS_lseek, fd, offset, whence);
24   result = ret;
25 #elif defined(SYS__llseek)
26   long ret = __llvm_libc::syscall(SYS__lseek, fd, offset >> 32, offset, &result,
27                                   whence);
28 #else
29 #error "lseek and _llseek syscalls not available."
30 #endif
31 
32   if (ret < 0) {
33     errno = -ret;
34     return -1;
35   }
36   return result;
37 }
38 
39 } // namespace __llvm_libc
40