1 /* Check the access rights of a file relative to an open directory.
2 Copyright (C) 2009-2020 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
22 #define _GL_INCLUDING_UNISTD_H
33 #undef _GL_INCLUDING_UNISTD_H
37 orig_faccessat (int fd
, char const *name
, int mode
, int flag
)
39 return faccessat (fd
, name
, mode
, flag
);
43 /* Write "unistd.h" here, not <unistd.h>, otherwise OSF/1 5.1 DTK cc
44 eliminates this include because of the preliminary #include <unistd.h>
49 /* Mingw lacks access, but it also lacks real vs. effective ids, so
50 the gnulib euidaccess module is good enough. */
52 # define access euidaccess
58 rpl_faccessat (int fd
, char const *file
, int mode
, int flag
)
60 int result
= orig_faccessat (fd
, file
, mode
, flag
);
62 if (result
== 0 && file
[strlen (file
) - 1] == '/')
65 result
= fstatat (fd
, file
, &st
, 0);
66 if (result
== 0 && !S_ISDIR (st
.st_mode
))
76 #else /* !HAVE_FACCESSAT */
78 /* Invoke access or euidaccess on file, FILE, using mode MODE, in the directory
79 open on descriptor FD. If possible, do it without changing the
80 working directory. Otherwise, resort to using save_cwd/fchdir, then
81 (access|euidaccess)/restore_cwd. If either the save_cwd or the
82 restore_cwd fails, then give a diagnostic and exit nonzero.
83 Note that this implementation only supports AT_EACCESS, although some
84 native versions also support AT_SYMLINK_NOFOLLOW. */
86 # define AT_FUNC_NAME faccessat
87 # define AT_FUNC_F1 euidaccess
88 # define AT_FUNC_F2 access
89 # define AT_FUNC_USE_F1_COND AT_EACCESS
90 # define AT_FUNC_POST_FILE_PARAM_DECLS , int mode, int flag
91 # define AT_FUNC_POST_FILE_ARGS , mode