blob: c3f267e422b7b4096cf8ff21523130577ab5ff28 [file] [log] [blame]
xf.libdd93d52023-05-12 07:10:14 -07001/* Checking macros for fcntl functions.
2 Copyright (C) 2007-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#ifndef _FCNTL_H
20# error "Never include <bits/fcntl2.h> directly; use <fcntl.h> instead."
21#endif
22
23/* Check that calls to open and openat with O_CREAT or O_TMPFILE set have an
24 appropriate third/fourth parameter. */
25#ifndef __USE_FILE_OFFSET64
26extern int __open_2 (const char *__path, int __oflag) __nonnull ((1));
27extern int __REDIRECT (__open_alias, (const char *__path, int __oflag, ...),
28 open) __nonnull ((1));
29#else
30extern int __REDIRECT (__open_2, (const char *__path, int __oflag),
31 __open64_2) __nonnull ((1));
32extern int __REDIRECT (__open_alias, (const char *__path, int __oflag, ...),
33 open64) __nonnull ((1));
34#endif
35__errordecl (__open_too_many_args,
36 "open can be called either with 2 or 3 arguments, not more");
37__errordecl (__open_missing_mode,
38 "open with O_CREAT or O_TMPFILE in second argument needs 3 arguments");
39
40__fortify_function int
41open (const char *__path, int __oflag, ...)
42{
43 if (__va_arg_pack_len () > 1)
44 __open_too_many_args ();
45
46 if (__builtin_constant_p (__oflag))
47 {
48 if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
49 {
50 __open_missing_mode ();
51 return __open_2 (__path, __oflag);
52 }
53 return __open_alias (__path, __oflag, __va_arg_pack ());
54 }
55
56 if (__va_arg_pack_len () < 1)
57 return __open_2 (__path, __oflag);
58
59 return __open_alias (__path, __oflag, __va_arg_pack ());
60}
61
62
63#ifdef __USE_LARGEFILE64
64extern int __open64_2 (const char *__path, int __oflag) __nonnull ((1));
65extern int __REDIRECT (__open64_alias, (const char *__path, int __oflag,
66 ...), open64) __nonnull ((1));
67__errordecl (__open64_too_many_args,
68 "open64 can be called either with 2 or 3 arguments, not more");
69__errordecl (__open64_missing_mode,
70 "open64 with O_CREAT or O_TMPFILE in second argument needs 3 arguments");
71
72__fortify_function int
73open64 (const char *__path, int __oflag, ...)
74{
75 if (__va_arg_pack_len () > 1)
76 __open64_too_many_args ();
77
78 if (__builtin_constant_p (__oflag))
79 {
80 if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
81 {
82 __open64_missing_mode ();
83 return __open64_2 (__path, __oflag);
84 }
85 return __open64_alias (__path, __oflag, __va_arg_pack ());
86 }
87
88 if (__va_arg_pack_len () < 1)
89 return __open64_2 (__path, __oflag);
90
91 return __open64_alias (__path, __oflag, __va_arg_pack ());
92}
93#endif
94
95
96#ifdef __USE_ATFILE
97# ifndef __USE_FILE_OFFSET64
98extern int __openat_2 (int __fd, const char *__path, int __oflag)
99 __nonnull ((2));
100extern int __REDIRECT (__openat_alias, (int __fd, const char *__path,
101 int __oflag, ...), openat)
102 __nonnull ((2));
103# else
104extern int __REDIRECT (__openat_2, (int __fd, const char *__path,
105 int __oflag), __openat64_2)
106 __nonnull ((2));
107extern int __REDIRECT (__openat_alias, (int __fd, const char *__path,
108 int __oflag, ...), openat64)
109 __nonnull ((2));
110# endif
111__errordecl (__openat_too_many_args,
112 "openat can be called either with 3 or 4 arguments, not more");
113__errordecl (__openat_missing_mode,
114 "openat with O_CREAT or O_TMPFILE in third argument needs 4 arguments");
115
116__fortify_function int
117openat (int __fd, const char *__path, int __oflag, ...)
118{
119 if (__va_arg_pack_len () > 1)
120 __openat_too_many_args ();
121
122 if (__builtin_constant_p (__oflag))
123 {
124 if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
125 {
126 __openat_missing_mode ();
127 return __openat_2 (__fd, __path, __oflag);
128 }
129 return __openat_alias (__fd, __path, __oflag, __va_arg_pack ());
130 }
131
132 if (__va_arg_pack_len () < 1)
133 return __openat_2 (__fd, __path, __oflag);
134
135 return __openat_alias (__fd, __path, __oflag, __va_arg_pack ());
136}
137
138
139# ifdef __USE_LARGEFILE64
140extern int __openat64_2 (int __fd, const char *__path, int __oflag)
141 __nonnull ((2));
142extern int __REDIRECT (__openat64_alias, (int __fd, const char *__path,
143 int __oflag, ...), openat64)
144 __nonnull ((2));
145__errordecl (__openat64_too_many_args,
146 "openat64 can be called either with 3 or 4 arguments, not more");
147__errordecl (__openat64_missing_mode,
148 "openat64 with O_CREAT or O_TMPFILE in third argument needs 4 arguments");
149
150__fortify_function int
151openat64 (int __fd, const char *__path, int __oflag, ...)
152{
153 if (__va_arg_pack_len () > 1)
154 __openat64_too_many_args ();
155
156 if (__builtin_constant_p (__oflag))
157 {
158 if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
159 {
160 __openat64_missing_mode ();
161 return __openat64_2 (__fd, __path, __oflag);
162 }
163 return __openat64_alias (__fd, __path, __oflag, __va_arg_pack ());
164 }
165
166 if (__va_arg_pack_len () < 1)
167 return __openat64_2 (__fd, __path, __oflag);
168
169 return __openat64_alias (__fd, __path, __oflag, __va_arg_pack ());
170}
171# endif
172#endif