Improve responsiveness while in 'replace-buffer-contents'
[emacs.git] / lib / faccessat.c
blob8aab7863cddfafcdd22fa6d9da06c9cc6d6dfab6
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
21 rpl_faccessat. */
22 #define _GL_INCLUDING_UNISTD_H
23 #include <config.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #undef _GL_INCLUDING_UNISTD_H
32 #if HAVE_FACCESSAT
33 static int
34 orig_faccessat (int fd, char const *name, int mode, int flag)
36 return faccessat (fd, name, mode, flag);
38 #endif
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>
42 above. */
43 #include "unistd.h"
45 #ifndef HAVE_ACCESS
46 /* Mingw lacks access, but it also lacks real vs. effective ids, so
47 the gnulib euidaccess module is good enough. */
48 # undef access
49 # define access euidaccess
50 #endif
52 #if HAVE_FACCESSAT
54 int
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] == '/')
61 struct stat st;
62 result = fstatat (fd, file, &st, 0);
63 if (result == 0 && !S_ISDIR (st.st_mode))
65 errno = ENOTDIR;
66 return -1;
70 return result;
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
89 # include "at-func.c"
91 #endif