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