exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / faccessat.c
blob8178ca8632eafefb924011eb4fffe4e9d1856864
1 /* Check the access rights of a file relative to an open directory.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* written by Eric Blake */
19 /* If the user's config.h happens to include <unistd.h>, let it include only
20 the system's <unistd.h> here, so that orig_faccessat doesn't recurse to
21 rpl_faccessat. */
22 #define _GL_INCLUDING_UNISTD_H
23 #include <config.h>
25 /* Specification. */
26 #include <unistd.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #undef _GL_INCLUDING_UNISTD_H
35 #if HAVE_FACCESSAT
36 static int
37 orig_faccessat (int fd, char const *name, int mode, int flag)
39 return faccessat (fd, name, mode, flag);
41 #endif
43 #ifdef __osf__
44 /* Write "unistd.h" here, not <unistd.h>, otherwise OSF/1 5.1 DTK cc
45 eliminates this include because of the preliminary #include <unistd.h>
46 above. */
47 # include "unistd.h"
48 #else
49 # include <unistd.h>
50 #endif
52 #ifndef HAVE_ACCESS
53 /* Mingw lacks access, but it also lacks real vs. effective ids, so
54 the gnulib euidaccess module is good enough. */
55 # undef access
56 # define access euidaccess
57 #endif
59 #if HAVE_FACCESSAT
61 int
62 rpl_faccessat (int fd, char const *file, int mode, int flag)
64 int result = orig_faccessat (fd, file, mode, flag);
66 if (result == 0 && file[strlen (file) - 1] == '/')
68 struct stat st;
69 result = fstatat (fd, file, &st, 0);
70 if (result == 0 && !S_ISDIR (st.st_mode))
72 errno = ENOTDIR;
73 return -1;
77 return result;
80 #else /* !HAVE_FACCESSAT */
82 /* Invoke access or euidaccess on file, FILE, using mode MODE, in the directory
83 open on descriptor FD. If possible, do it without changing the
84 working directory. Otherwise, resort to using save_cwd/fchdir, then
85 (access|euidaccess)/restore_cwd. If either the save_cwd or the
86 restore_cwd fails, then give a diagnostic and exit nonzero.
87 Note that this implementation only supports AT_EACCESS, although some
88 native versions also support AT_SYMLINK_NOFOLLOW. */
90 # define AT_FUNC_NAME faccessat
91 # define AT_FUNC_F1 euidaccess
92 # define AT_FUNC_F2 access
93 # define AT_FUNC_USE_F1_COND AT_EACCESS
94 # define AT_FUNC_POST_FILE_PARAM_DECLS , int mode, int flag
95 # define AT_FUNC_POST_FILE_ARGS , mode
96 # include "at-func.c"
98 #endif