1 /* Optimizing macros and inline functions for stdio functions.
2 Copyright (C) 1998-2022 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/>. */
20 #define _BITS_STDIO_H 1
23 # error "Never include <bits/stdio.h> directly; use <stdio.h> instead."
26 #ifndef __extern_inline
27 # define __STDIO_INLINE inline
29 # define __STDIO_INLINE __extern_inline
33 #ifdef __USE_EXTERN_INLINES
34 /* For -D_FORTIFY_SOURCE{,=2,=3} bits/stdio2.h will define a different
36 # if !(__USE_FORTIFY_LEVEL > 0 && defined __fortify_function)
37 /* Write formatted output to stdout from argument list ARG. */
39 vprintf (const char *__restrict __fmt
, __gnuc_va_list __arg
)
41 return vfprintf (stdout
, __fmt
, __arg
);
45 /* Read a character from stdin. */
54 /* Faster version when locking is not necessary. */
56 fgetc_unlocked (FILE *__fp
)
58 return __getc_unlocked_body (__fp
);
63 # ifdef __USE_POSIX199506
64 /* This is defined in POSIX.1:1996. */
66 getc_unlocked (FILE *__fp
)
68 return __getc_unlocked_body (__fp
);
71 /* This is defined in POSIX.1:1996. */
73 getchar_unlocked (void)
75 return __getc_unlocked_body (stdin
);
80 /* Write a character to stdout. */
84 return putc (__c
, stdout
);
89 /* Faster version when locking is not necessary. */
91 fputc_unlocked (int __c
, FILE *__stream
)
93 return __putc_unlocked_body (__c
, __stream
);
98 # ifdef __USE_POSIX199506
99 /* This is defined in POSIX.1:1996. */
101 putc_unlocked (int __c
, FILE *__stream
)
103 return __putc_unlocked_body (__c
, __stream
);
106 /* This is defined in POSIX.1:1996. */
108 putchar_unlocked (int __c
)
110 return __putc_unlocked_body (__c
, stdout
);
116 /* Like `getdelim', but reads up to a newline. */
117 __STDIO_INLINE __ssize_t
118 getline (char **__lineptr
, size_t *__n
, FILE *__stream
)
120 return __getdelim (__lineptr
, __n
, '\n', __stream
);
126 /* Faster versions when locking is not required. */
128 __NTH (feof_unlocked (FILE *__stream
))
130 return __feof_unlocked_body (__stream
);
133 /* Faster versions when locking is not required. */
135 __NTH (ferror_unlocked (FILE *__stream
))
137 return __ferror_unlocked_body (__stream
);
141 #endif /* Use extern inlines. */
144 #if defined __USE_MISC && defined __GNUC__ && defined __OPTIMIZE__ \
145 && !defined __cplusplus
146 /* Perform some simple optimizations. */
147 # define fread_unlocked(ptr, size, n, stream) \
148 (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
149 && (size_t) (size) * (size_t) (n) <= 8 \
150 && (size_t) (size) != 0) \
151 ? ({ char *__ptr = (char *) (ptr); \
152 FILE *__stream = (stream); \
154 for (__cnt = (size_t) (size) * (size_t) (n); \
155 __cnt > 0; --__cnt) \
157 int __c = getc_unlocked (__stream); \
162 ((size_t) (size) * (size_t) (n) - __cnt) \
163 / (size_t) (size); }) \
164 : (((__builtin_constant_p (size) && (size_t) (size) == 0) \
165 || (__builtin_constant_p (n) && (size_t) (n) == 0)) \
166 /* Evaluate all parameters once. */ \
167 ? ((void) (ptr), (void) (stream), (void) (size), \
168 (void) (n), (size_t) 0) \
169 : fread_unlocked (ptr, size, n, stream))))
171 # define fwrite_unlocked(ptr, size, n, stream) \
172 (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
173 && (size_t) (size) * (size_t) (n) <= 8 \
174 && (size_t) (size) != 0) \
175 ? ({ const char *__ptr = (const char *) (ptr); \
176 FILE *__stream = (stream); \
178 for (__cnt = (size_t) (size) * (size_t) (n); \
179 __cnt > 0; --__cnt) \
180 if (putc_unlocked (*__ptr++, __stream) == EOF) \
182 ((size_t) (size) * (size_t) (n) - __cnt) \
183 / (size_t) (size); }) \
184 : (((__builtin_constant_p (size) && (size_t) (size) == 0) \
185 || (__builtin_constant_p (n) && (size_t) (n) == 0)) \
186 /* Evaluate all parameters once. */ \
187 ? ((void) (ptr), (void) (stream), (void) (size), \
188 (void) (n), (size_t) 0) \
189 : fwrite_unlocked (ptr, size, n, stream))))
192 /* Define helper macro. */
193 #undef __STDIO_INLINE
195 #endif /* bits/stdio.h. */