9eca973b5f819a1493d88d4259a658bac1068c8c
[gnulib.git] / lib / openat.h
1 /* provide a replacement openat function
2    Copyright (C) 2004, 2005, 2006 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* written by Jim Meyering */
19
20 #include <fcntl.h>
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <dirent.h>
25 #include <unistd.h>
26 #include <stdbool.h>
27
28 #ifndef __attribute__
29 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
30 #  define __attribute__(x) /* empty */
31 # endif
32 #endif
33
34 #ifndef ATTRIBUTE_NORETURN
35 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
36 #endif
37
38 /* Work around a bug in Solaris 9 and 10: AT_FDCWD is positive.  Its
39    value exceeds INT_MAX, so its use as an int doesn't conform to the
40    C standard, and GCC and Sun C complain in some cases.  If the bug
41    is present, undef AT_FDCWD here, so it can be redefined below.  */
42 #if 0 < AT_FDCWD && AT_FDCWD == 0xffd19553
43 # undef AT_FDCWD
44 #endif
45
46 /* Use the same bit pattern as Solaris 9, but with the proper
47    signedness.  The bit pattern is important, in case this actually is
48    Solaris with the above workaround.  */
49 #ifndef AT_FDCWD
50 # define AT_FDCWD (-3041965)
51 #endif
52
53 /* Use the same values as Solaris 9.  This shouldn't matter, but
54    there's no real reason to differ.  */
55 #ifndef AT_SYMLINK_NOFOLLOW
56 # define AT_SYMLINK_NOFOLLOW 4096
57 # define AT_REMOVEDIR 1
58 #endif
59
60 #ifdef __OPENAT_PREFIX
61
62 # undef openat
63 # define __OPENAT_CONCAT(x, y) x ## y
64 # define __OPENAT_XCONCAT(x, y) __OPENAT_CONCAT (x, y)
65 # define __OPENAT_ID(y) __OPENAT_XCONCAT (__OPENAT_PREFIX, y)
66 # define openat __OPENAT_ID (openat)
67 int openat (int fd, char const *file, int flags, /* mode_t mode */ ...);
68 int openat_permissive (int fd, char const *file, int flags, mode_t mode,
69                        int *cwd_errno);
70 # if ! HAVE_FDOPENDIR
71 #  define fdopendir __OPENAT_ID (fdopendir)
72 # endif
73 DIR *fdopendir (int fd);
74 # define fstatat __OPENAT_ID (fstatat)
75 int fstatat (int fd, char const *file, struct stat *st, int flag);
76 # define unlinkat __OPENAT_ID (unlinkat)
77 int unlinkat (int fd, char const *file, int flag);
78 bool openat_needs_fchdir (void);
79
80 #else
81
82 # define openat_permissive(Fd, File, Flags, Mode, Cwd_errno) \
83     openat (Fd, File, Flags, Mode)
84 # define openat_needs_fchdir() false
85
86 #endif
87
88 int mkdirat (int fd, char const *file, mode_t mode);
89 void openat_restore_fail (int) ATTRIBUTE_NORETURN;
90 void openat_save_fail (int) ATTRIBUTE_NORETURN;
91 int fchmodat (int fd, char const *file, mode_t mode, int flag);
92 int fchownat (int fd, char const *file, uid_t owner, gid_t group, int flag);
93
94 /* Using these function names makes application code
95    slightly more readable than it would be with
96    fchownat (..., 0) or fchownat (..., AT_SYMLINK_NOFOLLOW).  */
97 static inline int
98 chownat (int fd, char const *file, uid_t owner, gid_t group)
99 {
100   return fchownat (fd, file, owner, group, 0);
101 }
102
103 static inline int
104 lchownat (int fd, char const *file, uid_t owner, gid_t group)
105 {
106   return fchownat (fd, file, owner, group, AT_SYMLINK_NOFOLLOW);
107 }
108
109 static inline int
110 chmodat (int fd, char const *file, mode_t mode)
111 {
112   return fchmodat (fd, file, mode, 0);
113 }
114
115 static inline int
116 lchmodat (int fd, char const *file, mode_t mode)
117 {
118   return fchmodat (fd, file, mode, AT_SYMLINK_NOFOLLOW);
119 }