xref: /llvm-project/libc/src/unistd/linux/access.cpp (revision abc49cc19463970d5523d7d3332e4c1f83bc2ef7)
1419580c6SSiva Chandra Reddy //===-- Linux implementation of access ------------------------------------===//
2419580c6SSiva Chandra Reddy //
3419580c6SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4419580c6SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
5419580c6SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6419580c6SSiva Chandra Reddy //
7419580c6SSiva Chandra Reddy //===----------------------------------------------------------------------===//
8419580c6SSiva Chandra Reddy 
9419580c6SSiva Chandra Reddy #include "src/unistd/access.h"
10419580c6SSiva Chandra Reddy 
11419580c6SSiva Chandra Reddy #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12419580c6SSiva Chandra Reddy #include "src/__support/common.h"
13419580c6SSiva Chandra Reddy 
14*abc49cc1SJob Henandez Lara #include "hdr/fcntl_macros.h"
155ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
16803437dbSMichael Jones #include "src/errno/libc_errno.h"
17419580c6SSiva Chandra Reddy #include <sys/syscall.h> // For syscall numbers.
18419580c6SSiva Chandra Reddy 
195ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
20419580c6SSiva Chandra Reddy 
21419580c6SSiva Chandra Reddy LLVM_LIBC_FUNCTION(int, access, (const char *path, int mode)) {
22419580c6SSiva Chandra Reddy #ifdef SYS_access
23b6bc9d72SGuillaume Chatelet   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_access, path, mode);
24cae9c64fSSiva Chandra #elif defined(SYS_faccessat)
25f0a3954eSMichael Jones   int ret =
26b6bc9d72SGuillaume Chatelet       LIBC_NAMESPACE::syscall_impl<int>(SYS_faccessat, AT_FDCWD, path, mode, 0);
27419580c6SSiva Chandra Reddy #else
285b22df99SMikhail R. Gadelha #error "access and faccessat syscalls not available."
29419580c6SSiva Chandra Reddy #endif
30419580c6SSiva Chandra Reddy 
31419580c6SSiva Chandra Reddy   if (ret < 0) {
32204587a3SSiva Chandra Reddy     libc_errno = -ret;
33419580c6SSiva Chandra Reddy     return -1;
34419580c6SSiva Chandra Reddy   }
35419580c6SSiva Chandra Reddy   return 0;
36419580c6SSiva Chandra Reddy }
37419580c6SSiva Chandra Reddy 
385ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
39