]> git.ipfire.org Git - thirdparty/glibc.git/blame - libio/bits/stdio.h
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / libio / bits / stdio.h
CommitLineData
085320f5 1/* Optimizing macros and inline functions for stdio functions.
2b778ceb 2 Copyright (C) 1998-2021 Free Software Foundation, Inc.
41bdb6e2 3 This file is part of the GNU C Library.
085320f5
UD
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
085320f5
UD
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
41bdb6e2 13 Lesser General Public License for more details.
085320f5 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
085320f5 18
a4fea3f2
ZW
19#ifndef _BITS_STDIO_H
20#define _BITS_STDIO_H 1
21
085320f5
UD
22#ifndef _STDIO_H
23# error "Never include <bits/stdio.h> directly; use <stdio.h> instead."
24#endif
25
de1c3ebb 26#ifndef __extern_inline
085320f5
UD
27# define __STDIO_INLINE inline
28#else
b037a293 29# define __STDIO_INLINE __extern_inline
085320f5
UD
30#endif
31
32
33#ifdef __USE_EXTERN_INLINES
f9de8bfe 34/* For -D_FORTIFY_SOURCE{,=2,=3} bits/stdio2.h will define a different
de1c3ebb 35 inline. */
5ac3ea17 36# if !(__USE_FORTIFY_LEVEL > 0 && defined __fortify_function)
085320f5
UD
37/* Write formatted output to stdout from argument list ARG. */
38__STDIO_INLINE int
a4fea3f2 39vprintf (const char *__restrict __fmt, __gnuc_va_list __arg)
085320f5
UD
40{
41 return vfprintf (stdout, __fmt, __arg);
42}
de1c3ebb 43# endif
085320f5
UD
44
45/* Read a character from stdin. */
46__STDIO_INLINE int
d9997a45 47getchar (void)
085320f5 48{
26c07172 49 return getc (stdin);
085320f5
UD
50}
51
52
9c8d03d9
JJ
53# ifdef __USE_MISC
54/* Faster version when locking is not necessary. */
55__STDIO_INLINE int
56fgetc_unlocked (FILE *__fp)
57{
a4fea3f2 58 return __getc_unlocked_body (__fp);
9c8d03d9
JJ
59}
60# endif /* misc */
61
62
e1fd4bbe 63# ifdef __USE_POSIX199506
085320f5
UD
64/* This is defined in POSIX.1:1996. */
65__STDIO_INLINE int
d9997a45 66getc_unlocked (FILE *__fp)
085320f5 67{
a4fea3f2 68 return __getc_unlocked_body (__fp);
085320f5
UD
69}
70
71/* This is defined in POSIX.1:1996. */
72__STDIO_INLINE int
d9997a45 73getchar_unlocked (void)
085320f5 74{
a4fea3f2 75 return __getc_unlocked_body (stdin);
085320f5 76}
acd7f096 77# endif /* POSIX */
085320f5
UD
78
79
80/* Write a character to stdout. */
81__STDIO_INLINE int
d9997a45 82putchar (int __c)
085320f5 83{
26c07172 84 return putc (__c, stdout);
085320f5
UD
85}
86
87
88# ifdef __USE_MISC
89/* Faster version when locking is not necessary. */
90__STDIO_INLINE int
d9997a45 91fputc_unlocked (int __c, FILE *__stream)
085320f5 92{
a4fea3f2 93 return __putc_unlocked_body (__c, __stream);
085320f5
UD
94}
95# endif /* misc */
96
97
e1fd4bbe 98# ifdef __USE_POSIX199506
085320f5
UD
99/* This is defined in POSIX.1:1996. */
100__STDIO_INLINE int
d9997a45 101putc_unlocked (int __c, FILE *__stream)
085320f5 102{
a4fea3f2 103 return __putc_unlocked_body (__c, __stream);
085320f5
UD
104}
105
106/* This is defined in POSIX.1:1996. */
107__STDIO_INLINE int
d9997a45 108putchar_unlocked (int __c)
085320f5 109{
a4fea3f2 110 return __putc_unlocked_body (__c, stdout);
085320f5 111}
acd7f096 112# endif /* POSIX */
085320f5
UD
113
114
115# ifdef __USE_GNU
116/* Like `getdelim', but reads up to a newline. */
a4fea3f2 117__STDIO_INLINE __ssize_t
d9997a45 118getline (char **__lineptr, size_t *__n, FILE *__stream)
085320f5
UD
119{
120 return __getdelim (__lineptr, __n, '\n', __stream);
121}
122# endif /* GNU */
123
124
125# ifdef __USE_MISC
126/* Faster versions when locking is not required. */
127__STDIO_INLINE int
f377d022 128__NTH (feof_unlocked (FILE *__stream))
085320f5 129{
a4fea3f2 130 return __feof_unlocked_body (__stream);
085320f5
UD
131}
132
133/* Faster versions when locking is not required. */
134__STDIO_INLINE int
f377d022 135__NTH (ferror_unlocked (FILE *__stream))
085320f5 136{
a4fea3f2 137 return __ferror_unlocked_body (__stream);
085320f5
UD
138}
139# endif /* misc */
140
141#endif /* Use extern inlines. */
142
143
de1c3ebb
UD
144#if defined __USE_MISC && defined __GNUC__ && defined __OPTIMIZE__ \
145 && !defined __cplusplus
085320f5
UD
146/* Perform some simple optimizations. */
147# define fread_unlocked(ptr, size, n, stream) \
8b7fb588 148 (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
8799d935
UD
149 && (size_t) (size) * (size_t) (n) <= 8 \
150 && (size_t) (size) != 0) \
085320f5
UD
151 ? ({ char *__ptr = (char *) (ptr); \
152 FILE *__stream = (stream); \
8b7fb588 153 size_t __cnt; \
8799d935
UD
154 for (__cnt = (size_t) (size) * (size_t) (n); \
155 __cnt > 0; --__cnt) \
085320f5 156 { \
a4fea3f2 157 int __c = getc_unlocked (__stream); \
085320f5 158 if (__c == EOF) \
8b7fb588 159 break; \
085320f5
UD
160 *__ptr++ = __c; \
161 } \
8799d935
UD
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)) \
8b7fb588
UD
166 /* Evaluate all parameters once. */ \
167 ? ((void) (ptr), (void) (stream), (void) (size), \
f98ca075 168 (void) (n), (size_t) 0) \
8b7fb588 169 : fread_unlocked (ptr, size, n, stream))))
085320f5
UD
170
171# define fwrite_unlocked(ptr, size, n, stream) \
8b7fb588 172 (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
f98ca075
RM
173 && (size_t) (size) * (size_t) (n) <= 8 \
174 && (size_t) (size) != 0) \
085320f5
UD
175 ? ({ const char *__ptr = (const char *) (ptr); \
176 FILE *__stream = (stream); \
8b7fb588 177 size_t __cnt; \
8799d935
UD
178 for (__cnt = (size_t) (size) * (size_t) (n); \
179 __cnt > 0; --__cnt) \
a4fea3f2 180 if (putc_unlocked (*__ptr++, __stream) == EOF) \
8b7fb588 181 break; \
8799d935
UD
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)) \
8b7fb588 186 /* Evaluate all parameters once. */ \
8799d935 187 ? ((void) (ptr), (void) (stream), (void) (size), \
f98ca075 188 (void) (n), (size_t) 0) \
8b7fb588 189 : fwrite_unlocked (ptr, size, n, stream))))
085320f5
UD
190#endif
191
192/* Define helper macro. */
193#undef __STDIO_INLINE
a4fea3f2
ZW
194
195#endif /* bits/stdio.h. */