Imported Upstream version 1.6
[debian/gzip] / lib / openat.h
1 /* provide a replacement openat function
2    Copyright (C) 2004-2006, 2008-2013 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 #include <fcntl.h>
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <stdbool.h>
28
29 _GL_INLINE_HEADER_BEGIN
30
31 #if !HAVE_OPENAT
32
33 int openat_permissive (int fd, char const *file, int flags, mode_t mode,
34                        int *cwd_errno);
35 bool openat_needs_fchdir (void);
36
37 #else
38
39 # define openat_permissive(Fd, File, Flags, Mode, Cwd_errno) \
40     openat (Fd, File, Flags, Mode)
41 # define openat_needs_fchdir() false
42
43 #endif
44
45 _Noreturn void openat_restore_fail (int);
46 _Noreturn void openat_save_fail (int);
47
48 /* Using these function names makes application code
49    slightly more readable than it would be with
50    fchownat (..., 0) or fchownat (..., AT_SYMLINK_NOFOLLOW).  */
51
52 #if GNULIB_FCHOWNAT
53
54 # ifndef FCHOWNAT_INLINE
55 #  define FCHOWNAT_INLINE _GL_INLINE
56 # endif
57
58 FCHOWNAT_INLINE int
59 chownat (int fd, char const *file, uid_t owner, gid_t group)
60 {
61   return fchownat (fd, file, owner, group, 0);
62 }
63
64 FCHOWNAT_INLINE int
65 lchownat (int fd, char const *file, uid_t owner, gid_t group)
66 {
67   return fchownat (fd, file, owner, group, AT_SYMLINK_NOFOLLOW);
68 }
69
70 #endif
71
72 #if GNULIB_FCHMODAT
73
74 # ifndef FCHMODAT_INLINE
75 #  define FCHMODAT_INLINE _GL_INLINE
76 # endif
77
78 FCHMODAT_INLINE int
79 chmodat (int fd, char const *file, mode_t mode)
80 {
81   return fchmodat (fd, file, mode, 0);
82 }
83
84 FCHMODAT_INLINE int
85 lchmodat (int fd, char const *file, mode_t mode)
86 {
87   return fchmodat (fd, file, mode, AT_SYMLINK_NOFOLLOW);
88 }
89
90 #endif
91
92 #if GNULIB_STATAT
93
94 # ifndef STATAT_INLINE
95 #  define STATAT_INLINE _GL_INLINE
96 # endif
97
98 STATAT_INLINE int
99 statat (int fd, char const *name, struct stat *st)
100 {
101   return fstatat (fd, name, st, 0);
102 }
103
104 STATAT_INLINE int
105 lstatat (int fd, char const *name, struct stat *st)
106 {
107   return fstatat (fd, name, st, AT_SYMLINK_NOFOLLOW);
108 }
109
110 #endif
111
112 /* For now, there are no wrappers named laccessat or leuidaccessat,
113    since gnulib doesn't support faccessat(,AT_SYMLINK_NOFOLLOW) and
114    since access rights on symlinks are of limited utility.  Likewise,
115    wrappers are not provided for accessat or euidaccessat, so as to
116    avoid dragging in -lgen on some platforms.  */
117
118 _GL_INLINE_HEADER_END
119
120 #endif /* _GL_HEADER_OPENAT */