1 /* Checking macros for stdio functions.
2 Copyright (C) 2004-2020 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, see
17 <https://www.gnu.org/licenses/>. */
19 #ifndef _BITS_STDIO2_H
20 #define _BITS_STDIO2_H 1
23 # error "Never include <bits/stdio2.h> directly; use <stdio.h> instead."
26 extern int __sprintf_chk (char *__restrict __s
, int __flag
, size_t __slen
,
27 const char *__restrict __format
, ...) __THROW
;
28 extern int __vsprintf_chk (char *__restrict __s
, int __flag
, size_t __slen
,
29 const char *__restrict __format
,
30 __gnuc_va_list __ap
) __THROW
;
33 __fortify_function
int
34 __NTH (sprintf (char *__restrict __s
, const char *__restrict __fmt
, ...))
36 return __builtin___sprintf_chk (__s
, __USE_FORTIFY_LEVEL
- 1,
37 __bos (__s
), __fmt
, __va_arg_pack ());
39 #elif !defined __cplusplus
40 # define sprintf(str, ...) \
41 __builtin___sprintf_chk (str, __USE_FORTIFY_LEVEL - 1, __bos (str), \
45 __fortify_function
int
46 __NTH (vsprintf (char *__restrict __s
, const char *__restrict __fmt
,
49 return __builtin___vsprintf_chk (__s
, __USE_FORTIFY_LEVEL
- 1,
50 __bos (__s
), __fmt
, __ap
);
53 #if defined __USE_ISOC99 || defined __USE_UNIX98
55 extern int __snprintf_chk (char *__restrict __s
, size_t __n
, int __flag
,
56 size_t __slen
, const char *__restrict __format
,
58 extern int __vsnprintf_chk (char *__restrict __s
, size_t __n
, int __flag
,
59 size_t __slen
, const char *__restrict __format
,
60 __gnuc_va_list __ap
) __THROW
;
63 __fortify_function
int
64 __NTH (snprintf (char *__restrict __s
, size_t __n
,
65 const char *__restrict __fmt
, ...))
67 return __builtin___snprintf_chk (__s
, __n
, __USE_FORTIFY_LEVEL
- 1,
68 __bos (__s
), __fmt
, __va_arg_pack ());
70 # elif !defined __cplusplus
71 # define snprintf(str, len, ...) \
72 __builtin___snprintf_chk (str, len, __USE_FORTIFY_LEVEL - 1, __bos (str), \
76 __fortify_function
int
77 __NTH (vsnprintf (char *__restrict __s
, size_t __n
,
78 const char *__restrict __fmt
, __gnuc_va_list __ap
))
80 return __builtin___vsnprintf_chk (__s
, __n
, __USE_FORTIFY_LEVEL
- 1,
81 __bos (__s
), __fmt
, __ap
);
86 #if __USE_FORTIFY_LEVEL > 1
88 extern int __fprintf_chk (FILE *__restrict __stream
, int __flag
,
89 const char *__restrict __format
, ...);
90 extern int __printf_chk (int __flag
, const char *__restrict __format
, ...);
91 extern int __vfprintf_chk (FILE *__restrict __stream
, int __flag
,
92 const char *__restrict __format
, __gnuc_va_list __ap
);
93 extern int __vprintf_chk (int __flag
, const char *__restrict __format
,
97 __fortify_function
int
98 fprintf (FILE *__restrict __stream
, const char *__restrict __fmt
, ...)
100 return __fprintf_chk (__stream
, __USE_FORTIFY_LEVEL
- 1, __fmt
,
104 __fortify_function
int
105 printf (const char *__restrict __fmt
, ...)
107 return __printf_chk (__USE_FORTIFY_LEVEL
- 1, __fmt
, __va_arg_pack ());
109 # elif !defined __cplusplus
110 # define printf(...) \
111 __printf_chk (__USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
112 # define fprintf(stream, ...) \
113 __fprintf_chk (stream, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
116 __fortify_function
int
117 vprintf (const char *__restrict __fmt
, __gnuc_va_list __ap
)
119 #ifdef __USE_EXTERN_INLINES
120 return __vfprintf_chk (stdout
, __USE_FORTIFY_LEVEL
- 1, __fmt
, __ap
);
122 return __vprintf_chk (__USE_FORTIFY_LEVEL
- 1, __fmt
, __ap
);
126 __fortify_function
int
127 vfprintf (FILE *__restrict __stream
,
128 const char *__restrict __fmt
, __gnuc_va_list __ap
)
130 return __vfprintf_chk (__stream
, __USE_FORTIFY_LEVEL
- 1, __fmt
, __ap
);
133 # ifdef __USE_XOPEN2K8
134 extern int __dprintf_chk (int __fd
, int __flag
, const char *__restrict __fmt
,
135 ...) __attribute__ ((__format__ (__printf__
, 3, 4)));
136 extern int __vdprintf_chk (int __fd
, int __flag
,
137 const char *__restrict __fmt
, __gnuc_va_list __arg
)
138 __attribute__ ((__format__ (__printf__
, 3, 0)));
140 # ifdef __va_arg_pack
141 __fortify_function
int
142 dprintf (int __fd
, const char *__restrict __fmt
, ...)
144 return __dprintf_chk (__fd
, __USE_FORTIFY_LEVEL
- 1, __fmt
,
147 # elif !defined __cplusplus
148 # define dprintf(fd, ...) \
149 __dprintf_chk (fd, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
152 __fortify_function
int
153 vdprintf (int __fd
, const char *__restrict __fmt
, __gnuc_va_list __ap
)
155 return __vdprintf_chk (__fd
, __USE_FORTIFY_LEVEL
- 1, __fmt
, __ap
);
161 extern int __asprintf_chk (char **__restrict __ptr
, int __flag
,
162 const char *__restrict __fmt
, ...)
163 __THROW
__attribute__ ((__format__ (__printf__
, 3, 4))) __wur
;
164 extern int __vasprintf_chk (char **__restrict __ptr
, int __flag
,
165 const char *__restrict __fmt
, __gnuc_va_list __arg
)
166 __THROW
__attribute__ ((__format__ (__printf__
, 3, 0))) __wur
;
167 extern int __obstack_printf_chk (struct obstack
*__restrict __obstack
,
168 int __flag
, const char *__restrict __format
,
170 __THROW
__attribute__ ((__format__ (__printf__
, 3, 4)));
171 extern int __obstack_vprintf_chk (struct obstack
*__restrict __obstack
,
173 const char *__restrict __format
,
174 __gnuc_va_list __args
)
175 __THROW
__attribute__ ((__format__ (__printf__
, 3, 0)));
177 # ifdef __va_arg_pack
178 __fortify_function
int
179 __NTH (asprintf (char **__restrict __ptr
, const char *__restrict __fmt
, ...))
181 return __asprintf_chk (__ptr
, __USE_FORTIFY_LEVEL
- 1, __fmt
,
185 __fortify_function
int
186 __NTH (__asprintf (char **__restrict __ptr
, const char *__restrict __fmt
,
189 return __asprintf_chk (__ptr
, __USE_FORTIFY_LEVEL
- 1, __fmt
,
193 __fortify_function
int
194 __NTH (obstack_printf (struct obstack
*__restrict __obstack
,
195 const char *__restrict __fmt
, ...))
197 return __obstack_printf_chk (__obstack
, __USE_FORTIFY_LEVEL
- 1, __fmt
,
200 # elif !defined __cplusplus
201 # define asprintf(ptr, ...) \
202 __asprintf_chk (ptr, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
203 # define __asprintf(ptr, ...) \
204 __asprintf_chk (ptr, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
205 # define obstack_printf(obstack, ...) \
206 __obstack_printf_chk (obstack, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
209 __fortify_function
int
210 __NTH (vasprintf (char **__restrict __ptr
, const char *__restrict __fmt
,
211 __gnuc_va_list __ap
))
213 return __vasprintf_chk (__ptr
, __USE_FORTIFY_LEVEL
- 1, __fmt
, __ap
);
216 __fortify_function
int
217 __NTH (obstack_vprintf (struct obstack
*__restrict __obstack
,
218 const char *__restrict __fmt
, __gnuc_va_list __ap
))
220 return __obstack_vprintf_chk (__obstack
, __USE_FORTIFY_LEVEL
- 1, __fmt
,
228 #if __GLIBC_USE (DEPRECATED_GETS)
229 extern char *__gets_chk (char *__str
, size_t) __wur
;
230 extern char *__REDIRECT (__gets_warn
, (char *__str
), gets
)
231 __wur
__warnattr ("please use fgets or getline instead, gets can't "
232 "specify buffer size");
234 __fortify_function __wur
char *
237 if (__bos (__str
) != (size_t) -1)
238 return __gets_chk (__str
, __bos (__str
));
239 return __gets_warn (__str
);
243 extern char *__fgets_chk (char *__restrict __s
, size_t __size
, int __n
,
244 FILE *__restrict __stream
) __wur
;
245 extern char *__REDIRECT (__fgets_alias
,
246 (char *__restrict __s
, int __n
,
247 FILE *__restrict __stream
), fgets
) __wur
;
248 extern char *__REDIRECT (__fgets_chk_warn
,
249 (char *__restrict __s
, size_t __size
, int __n
,
250 FILE *__restrict __stream
), __fgets_chk
)
251 __wur
__warnattr ("fgets called with bigger size than length "
252 "of destination buffer");
254 __fortify_function __wur
char *
255 fgets (char *__restrict __s
, int __n
, FILE *__restrict __stream
)
257 if (__bos (__s
) != (size_t) -1)
259 if (!__builtin_constant_p (__n
) || __n
<= 0)
260 return __fgets_chk (__s
, __bos (__s
), __n
, __stream
);
262 if ((size_t) __n
> __bos (__s
))
263 return __fgets_chk_warn (__s
, __bos (__s
), __n
, __stream
);
265 return __fgets_alias (__s
, __n
, __stream
);
268 extern size_t __fread_chk (void *__restrict __ptr
, size_t __ptrlen
,
269 size_t __size
, size_t __n
,
270 FILE *__restrict __stream
) __wur
;
271 extern size_t __REDIRECT (__fread_alias
,
272 (void *__restrict __ptr
, size_t __size
,
273 size_t __n
, FILE *__restrict __stream
),
275 extern size_t __REDIRECT (__fread_chk_warn
,
276 (void *__restrict __ptr
, size_t __ptrlen
,
277 size_t __size
, size_t __n
,
278 FILE *__restrict __stream
),
280 __wur
__warnattr ("fread called with bigger size * nmemb than length "
281 "of destination buffer");
283 __fortify_function __wur
size_t
284 fread (void *__restrict __ptr
, size_t __size
, size_t __n
,
285 FILE *__restrict __stream
)
287 if (__bos0 (__ptr
) != (size_t) -1)
289 if (!__builtin_constant_p (__size
)
290 || !__builtin_constant_p (__n
)
291 || (__size
| __n
) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
292 return __fread_chk (__ptr
, __bos0 (__ptr
), __size
, __n
, __stream
);
294 if (__size
* __n
> __bos0 (__ptr
))
295 return __fread_chk_warn (__ptr
, __bos0 (__ptr
), __size
, __n
, __stream
);
297 return __fread_alias (__ptr
, __size
, __n
, __stream
);
301 extern char *__fgets_unlocked_chk (char *__restrict __s
, size_t __size
,
302 int __n
, FILE *__restrict __stream
) __wur
;
303 extern char *__REDIRECT (__fgets_unlocked_alias
,
304 (char *__restrict __s
, int __n
,
305 FILE *__restrict __stream
), fgets_unlocked
) __wur
;
306 extern char *__REDIRECT (__fgets_unlocked_chk_warn
,
307 (char *__restrict __s
, size_t __size
, int __n
,
308 FILE *__restrict __stream
), __fgets_unlocked_chk
)
309 __wur
__warnattr ("fgets_unlocked called with bigger size than length "
310 "of destination buffer");
312 __fortify_function __wur
char *
313 fgets_unlocked (char *__restrict __s
, int __n
, FILE *__restrict __stream
)
315 if (__bos (__s
) != (size_t) -1)
317 if (!__builtin_constant_p (__n
) || __n
<= 0)
318 return __fgets_unlocked_chk (__s
, __bos (__s
), __n
, __stream
);
320 if ((size_t) __n
> __bos (__s
))
321 return __fgets_unlocked_chk_warn (__s
, __bos (__s
), __n
, __stream
);
323 return __fgets_unlocked_alias (__s
, __n
, __stream
);
328 # undef fread_unlocked
329 extern size_t __fread_unlocked_chk (void *__restrict __ptr
, size_t __ptrlen
,
330 size_t __size
, size_t __n
,
331 FILE *__restrict __stream
) __wur
;
332 extern size_t __REDIRECT (__fread_unlocked_alias
,
333 (void *__restrict __ptr
, size_t __size
,
334 size_t __n
, FILE *__restrict __stream
),
335 fread_unlocked
) __wur
;
336 extern size_t __REDIRECT (__fread_unlocked_chk_warn
,
337 (void *__restrict __ptr
, size_t __ptrlen
,
338 size_t __size
, size_t __n
,
339 FILE *__restrict __stream
),
340 __fread_unlocked_chk
)
341 __wur
__warnattr ("fread_unlocked called with bigger size * nmemb than "
342 "length of destination buffer");
344 __fortify_function __wur
size_t
345 fread_unlocked (void *__restrict __ptr
, size_t __size
, size_t __n
,
346 FILE *__restrict __stream
)
348 if (__bos0 (__ptr
) != (size_t) -1)
350 if (!__builtin_constant_p (__size
)
351 || !__builtin_constant_p (__n
)
352 || (__size
| __n
) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
353 return __fread_unlocked_chk (__ptr
, __bos0 (__ptr
), __size
, __n
,
356 if (__size
* __n
> __bos0 (__ptr
))
357 return __fread_unlocked_chk_warn (__ptr
, __bos0 (__ptr
), __size
, __n
,
361 # ifdef __USE_EXTERN_INLINES
362 if (__builtin_constant_p (__size
)
363 && __builtin_constant_p (__n
)
364 && (__size
| __n
) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
365 && __size
* __n
<= 8)
367 size_t __cnt
= __size
* __n
;
368 char *__cptr
= (char *) __ptr
;
372 for (; __cnt
> 0; --__cnt
)
374 int __c
= getc_unlocked (__stream
);
379 return (__cptr
- (char *) __ptr
) / __size
;
382 return __fread_unlocked_alias (__ptr
, __size
, __n
, __stream
);
386 #endif /* bits/stdio2.h. */