vfs: check userland buffers before reading them.
[haiku.git] / src / system / libroot / posix / glibc / include / bits / stdio.h
blob387da3c4b07d47e18bcce828e67d9fd3bed125ce
1 /* Optimizing macros and inline functions for stdio functions.
2 Copyright (C) 1998, 2000, 2001 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 _STDIO_H
21 # error "Never include <bits/stdio.h> directly; use <stdio.h> instead."
22 #endif
24 #ifdef __cplusplus
25 # define __STDIO_INLINE inline
26 #else
27 # define __STDIO_INLINE __extern_inline
28 #endif
31 #ifdef __USE_EXTERN_INLINES
32 /* Write formatted output to stdout from argument list ARG. */
33 __STDIO_INLINE int
34 vprintf (__const char *__restrict __fmt, _G_va_list __arg) __THROW
36 return vfprintf (stdout, __fmt, __arg);
39 /* Read a character from stdin. */
40 __STDIO_INLINE int
41 getchar (void) __THROW
43 return _IO_getc (stdin);
47 # if defined __USE_POSIX || defined __USE_MISC
48 /* This is defined in POSIX.1:1996. */
49 __STDIO_INLINE int
50 getc_unlocked (FILE *__fp) __THROW
52 return _IO_getc_unlocked (__fp);
55 /* This is defined in POSIX.1:1996. */
56 __STDIO_INLINE int
57 getchar_unlocked (void) __THROW
59 return _IO_getc_unlocked (stdin);
61 # endif /* POSIX || misc */
64 /* Write a character to stdout. */
65 __STDIO_INLINE int
66 putchar (int __c) __THROW
68 return _IO_putc (__c, stdout);
72 # ifdef __USE_MISC
73 /* Faster version when locking is not necessary. */
74 __STDIO_INLINE int
75 fputc_unlocked (int __c, FILE *__stream) __THROW
77 return _IO_putc_unlocked (__c, __stream);
79 # endif /* misc */
82 # if defined __USE_POSIX || defined __USE_MISC
83 /* This is defined in POSIX.1:1996. */
84 __STDIO_INLINE int
85 putc_unlocked (int __c, FILE *__stream) __THROW
87 return _IO_putc_unlocked (__c, __stream);
90 /* This is defined in POSIX.1:1996. */
91 __STDIO_INLINE int
92 putchar_unlocked (int __c) __THROW
94 return _IO_putc_unlocked (__c, stdout);
96 # endif /* POSIX || misc */
99 # ifdef __USE_GNU
100 /* Like `getdelim', but reads up to a newline. */
101 __STDIO_INLINE _IO_ssize_t
102 getline (char **__lineptr, size_t *__n, FILE *__stream) __THROW
104 return __getdelim (__lineptr, __n, '\n', __stream);
106 # endif /* GNU */
109 # ifdef __USE_MISC
110 /* Faster versions when locking is not required. */
111 __STDIO_INLINE int
112 feof_unlocked (FILE *__stream) __THROW
114 return _IO_feof_unlocked (__stream);
117 /* Faster versions when locking is not required. */
118 __STDIO_INLINE int
119 ferror_unlocked (FILE *__stream) __THROW
121 return _IO_ferror_unlocked (__stream);
123 # endif /* misc */
125 #endif /* Use extern inlines. */
128 #if defined __USE_MISC && defined __GNUC__ && defined __OPTIMIZE__
129 /* Perform some simple optimizations. */
130 # define fread_unlocked(ptr, size, n, stream) \
131 (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
132 && (size_t) ((size) * (n)) <= 8 && (size) != 0) \
133 ? ({ char *__ptr = (char *) (ptr); \
134 FILE *__stream = (stream); \
135 size_t __cnt; \
136 for (__cnt = (size) * (n); __cnt > 0; --__cnt) \
138 int __c = _IO_getc_unlocked (__stream); \
139 if (__c == EOF) \
140 break; \
141 *__ptr++ = __c; \
143 ((size_t) ((size) * (n)) - __cnt) / (size); }) \
144 : (((__builtin_constant_p (size) && (size) == 0) \
145 || (__builtin_constant_p (n) && (n) == 0)) \
146 /* Evaluate all parameters once. */ \
147 ? ((void) (ptr), (void) (stream), (void) (size), \
148 (void) (n), 0) \
149 : fread_unlocked (ptr, size, n, stream))))
151 # define fwrite_unlocked(ptr, size, n, stream) \
152 (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
153 && (size_t) ((size) * (n)) <= 8 && (size) != 0) \
154 ? ({ const char *__ptr = (const char *) (ptr); \
155 FILE *__stream = (stream); \
156 size_t __cnt; \
157 for (__cnt = (size) * (n); __cnt > 0; --__cnt) \
158 if (_IO_putc_unlocked (*__ptr++, __stream) == EOF) \
159 break; \
160 ((size_t) ((size) * (n)) - __cnt) / (size); }) \
161 : (((__builtin_constant_p (size) && (size) == 0) \
162 || (__builtin_constant_p (n) && (n) == 0)) \
163 /* Evaluate all parameters once. */ \
164 ? ((void) (ptr), (void) (stream), (void) (size), n) \
165 : fwrite_unlocked (ptr, size, n, stream))))
166 #endif
168 /* Define helper macro. */
169 #undef __STDIO_INLINE