openat: Work around compilation error with OSF/1 5.1 DTK cc.
[gnulib.git] / lib / openat.h
1 /* provide a replacement openat function
2    Copyright (C) 2004-2006, 2008-2011 Free Software Foundation, Inc.
3
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.
8
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.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* written by Jim Meyering */
18
19 #ifndef _GL_HEADER_OPENAT
20 #define _GL_HEADER_OPENAT
21
22 /* Write "fcntl.h" here, not <fcntl.h>, otherwise OSF/1 5.1 DTK cc miscompiles
23    openat.c because that file has a preliminary #include <fcntl.h>.  */
24 #include "fcntl.h"
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <dirent.h>
29 #include <unistd.h>
30 #include <stdbool.h>
31
32 #if !HAVE_OPENAT
33
34 int openat_permissive (int fd, char const *file, int flags, mode_t mode,
35                        int *cwd_errno);
36 bool openat_needs_fchdir (void);
37
38 #else
39
40 # define openat_permissive(Fd, File, Flags, Mode, Cwd_errno) \
41     openat (Fd, File, Flags, Mode)
42 # define openat_needs_fchdir() false
43
44 #endif
45
46 _Noreturn void openat_restore_fail (int);
47 _Noreturn void openat_save_fail (int);
48
49 /* Using these function names makes application code
50    slightly more readable than it would be with
51    fchownat (..., 0) or fchownat (..., AT_SYMLINK_NOFOLLOW).  */
52 static inline int
53 chownat (int fd, char const *file, uid_t owner, gid_t group)
54 {
55   return fchownat (fd, file, owner, group, 0);
56 }
57
58 static inline int
59 lchownat (int fd, char const *file, uid_t owner, gid_t group)
60 {
61   return fchownat (fd, file, owner, group, AT_SYMLINK_NOFOLLOW);
62 }
63
64 static inline int
65 chmodat (int fd, char const *file, mode_t mode)
66 {
67   return fchmodat (fd, file, mode, 0);
68 }
69
70 static inline int
71 lchmodat (int fd, char const *file, mode_t mode)
72 {
73   return fchmodat (fd, file, mode, AT_SYMLINK_NOFOLLOW);
74 }
75
76 static inline int
77 statat (int fd, char const *name, struct stat *st)
78 {
79   return fstatat (fd, name, st, 0);
80 }
81
82 static inline int
83 lstatat (int fd, char const *name, struct stat *st)
84 {
85   return fstatat (fd, name, st, AT_SYMLINK_NOFOLLOW);
86 }
87
88 /* For now, there are no wrappers named laccessat or leuidaccessat,
89    since gnulib doesn't support faccessat(,AT_SYMLINK_NOFOLLOW) and
90    since access rights on symlinks are of limited utility.  Likewise,
91    wrappers are not provided for accessat or euidaccessat, so as to
92    avoid dragging in -lgen on some platforms.  */
93
94 #endif /* _GL_HEADER_OPENAT */