* io/bits/fcntl2.h (open, open64, openat, openat64): Add
[glibc.git] / io / bits / fcntl2.h
blob5b9fcd5e71e9eb4f3bc6b1f68273749ef5cf35ab
1 /* Checking macros for fcntl functions.
2 Copyright (C) 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _FCNTL_H
21 # error "Never include <bits/fcntl2.h> directly; use <fcntl.h> instead."
22 #endif
24 /* Check that calls to open and openat with O_CREAT set have an
25 appropriate third/fourth parameter. */
26 #ifndef __USE_FILE_OFFSET64
27 extern int __open_2 (__const char *__path, int __oflag) __nonnull ((1));
28 #else
29 extern int __REDIRECT (__open_2, (__const char *__file, int __oflag),
30 __open64_2) __nonnull ((1));
31 #endif
33 #define open(fname, flags, ...) \
34 (__extension__ \
35 ({ int ___r; \
36 /* If the compiler complains about an invalid type, excess elements, \
37 etc. in the initialization this means a parameter of the wrong type \
38 has been passed to open. */ \
39 int ___arr[] = { __VA_ARGS__ }; \
40 if (__builtin_constant_p (flags) && ((flags) & O_CREAT) != 0) \
41 { \
42 /* If the compiler complains about the size of this array type the \
43 mode parameter is missing since O_CREAT has been used. */ \
44 typedef int __open_missing_mode[((flags) & O_CREAT) != 0 \
45 ? ((long int) sizeof (___arr) \
46 - (long int) sizeof (int)) : 1];\
47 } \
48 if (sizeof (___arr) == 0) \
49 { \
50 if (__builtin_constant_p (flags) && ((flags) & O_CREAT) == 0) \
51 ___r = open (fname, flags); \
52 else \
53 ___r = __open_2 (fname, flags); \
54 } \
55 else \
56 { \
57 /* If the compiler complains about the size of this array type too \
58 many parameters have been passed to open. */ \
59 typedef int __open_too_many_args[-(sizeof (___arr) \
60 > sizeof (int))]; \
61 ___r = open (fname, flags, ___arr[0]); \
62 } \
63 ___r; \
64 }))
67 #ifdef __USE_LARGEFILE64
68 extern int __open64_2 (__const char *__path, int __oflag) __nonnull ((1));
70 # define open64(fname, flags, ...) \
71 (__extension__ \
72 ({ int ___r; \
73 /* If the compiler complains about an invalid type, excess elements, \
74 etc. in the initialization this means a parameter of the wrong type \
75 has been passed to open64. */ \
76 int ___arr[] = { __VA_ARGS__ }; \
77 if (__builtin_constant_p (flags) && ((flags) & O_CREAT) != 0) \
78 { \
79 /* If the compiler complains about the size of this array type the \
80 mode parameter is missing since O_CREAT has been used. */ \
81 typedef int __open_missing_mode[((flags) & O_CREAT) != 0 \
82 ? ((long int) sizeof (___arr) \
83 - (long int) sizeof (int)) : 1];\
84 } \
85 if (sizeof (___arr) == 0) \
86 { \
87 if (__builtin_constant_p (flags) && ((flags) & O_CREAT) == 0) \
88 ___r = open64 (fname, flags); \
89 else \
90 ___r = __open64_2 (fname, flags); \
91 } \
92 else \
93 { \
94 /* If the compiler complains about the size of this array type too \
95 many parameters have been passed to open64. */ \
96 typedef int __open_too_many_args[-(sizeof (___arr) \
97 > sizeof (int))]; \
98 ___r = open64 (fname, flags, ___arr[0]); \
99 } \
100 ___r; \
102 #endif
104 #ifdef __USE_ATFILE
105 # ifndef __USE_FILE_OFFSET64
106 extern int __openat_2 (int __fd, __const char *__path, int __oflag)
107 __nonnull ((2));
108 # else
109 extern int __REDIRECT (__openat_2, (int __fd, __const char *__file,
110 int __oflag), __openat64_2)
111 __nonnull ((2));
112 # endif
114 # define openat(fd, fname, flags, ...) \
115 (__extension__ \
116 ({ int ___r; \
117 /* If the compiler complains about an invalid type, excess elements, \
118 etc. in the initialization this means a parameter of the wrong type \
119 has been passed to openat. */ \
120 int ___arr[] = { __VA_ARGS__ }; \
121 if (__builtin_constant_p (flags) && ((flags) & O_CREAT) != 0) \
123 /* If the compiler complains about the size of this array type the \
124 mode parameter is missing since O_CREAT has been used. */ \
125 typedef int __open_missing_mode[((flags) & O_CREAT) != 0 \
126 ? ((long int) sizeof (___arr) \
127 - (long int) sizeof (int)) : 1];\
129 if (sizeof (___arr) == 0) \
131 if (__builtin_constant_p (flags) && ((flags) & O_CREAT) == 0) \
132 ___r = openat (fd, fname, flags); \
133 else \
134 ___r = __openat_2 (fd, fname, flags); \
136 else \
138 /* If the compiler complains about the size of this array type too \
139 many parameters have been passed to openat. */ \
140 typedef int __open_too_many_args[-(sizeof (___arr) \
141 > sizeof (int))]; \
142 ___r = openat (fd, fname, flags, ___arr[0]); \
144 ___r; \
148 # ifdef __USE_LARGEFILE64
149 extern int __openat64_2 (int __fd, __const char *__path, int __oflag)
150 __nonnull ((2));
152 # define openat64(fd, fname, flags, ...) \
153 (__extension__ \
154 ({ int ___r; \
155 /* If the compiler complains about an invalid type, excess elements, \
156 etc. in the initialization this means a parameter of the wrong type \
157 has been passed to openat64. */ \
158 int ___arr[] = { __VA_ARGS__ }; \
159 if (__builtin_constant_p (flags) && ((flags) & O_CREAT) != 0) \
161 /* If the compiler complains about the size of this array type the \
162 mode parameter is missing since O_CREAT has been used. */ \
163 typedef int __open_missing_mode[((flags) & O_CREAT) != 0 \
164 ? ((long int) sizeof (___arr) \
165 - (long int) sizeof (int)) : 1];\
167 if (sizeof (___arr) == 0) \
169 if (__builtin_constant_p (flags) && ((flags) & O_CREAT) == 0) \
170 ___r = openat64 (fd, fname, flags); \
171 else \
172 ___r = __openat64_2 (fd, fname, flags); \
174 else \
176 /* If the compiler complains about the size of this array type too \
177 many parameters have been passed to openat64. */ \
178 typedef int __open_too_many_args[-(sizeof (___arr) \
179 > sizeof (int))]; \
180 ___r = openat64 (fd, fname, flags, ___arr[0]); \
182 ___r; \
184 # endif
185 #endif