Back to index

glibc  2.9
stdio.h
Go to the documentation of this file.
00001 /* Optimizing macros and inline functions for stdio functions.
00002    Copyright (C) 1998, 2000, 2001, 2004, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifndef _STDIO_H
00021 # error "Never include <bits/stdio.h> directly; use <stdio.h> instead."
00022 #endif
00023 
00024 #ifndef __extern_inline
00025 # define __STDIO_INLINE inline
00026 #else
00027 # define __STDIO_INLINE __extern_inline
00028 #endif
00029 
00030 
00031 #ifdef __USE_EXTERN_INLINES
00032 /* For -D_FORTIFY_SOURCE{,=2} bits/stdio2.h will define a different
00033    inline.  */
00034 # if !(__USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline)
00035 /* Write formatted output to stdout from argument list ARG.  */
00036 __STDIO_INLINE int
00037 vprintf (__const char *__restrict __fmt, _G_va_list __arg)
00038 {
00039   return vfprintf (stdout, __fmt, __arg);
00040 }
00041 # endif
00042 
00043 /* Read a character from stdin.  */
00044 __STDIO_INLINE int
00045 getchar (void)
00046 {
00047   return _IO_getc (stdin);
00048 }
00049 
00050 
00051 # ifdef __USE_MISC
00052 /* Faster version when locking is not necessary.  */
00053 __STDIO_INLINE int
00054 fgetc_unlocked (FILE *__fp)
00055 {
00056   return _IO_getc_unlocked (__fp);
00057 }
00058 # endif /* misc */
00059 
00060 
00061 # if defined __USE_POSIX || defined __USE_MISC
00062 /* This is defined in POSIX.1:1996.  */
00063 __STDIO_INLINE int
00064 getc_unlocked (FILE *__fp)
00065 {
00066   return _IO_getc_unlocked (__fp);
00067 }
00068 
00069 /* This is defined in POSIX.1:1996.  */
00070 __STDIO_INLINE int
00071 getchar_unlocked (void)
00072 {
00073   return _IO_getc_unlocked (stdin);
00074 }
00075 # endif       /* POSIX || misc */
00076 
00077 
00078 /* Write a character to stdout.  */
00079 __STDIO_INLINE int
00080 putchar (int __c)
00081 {
00082   return _IO_putc (__c, stdout);
00083 }
00084 
00085 
00086 # ifdef __USE_MISC
00087 /* Faster version when locking is not necessary.  */
00088 __STDIO_INLINE int
00089 fputc_unlocked (int __c, FILE *__stream)
00090 {
00091   return _IO_putc_unlocked (__c, __stream);
00092 }
00093 # endif /* misc */
00094 
00095 
00096 # if defined __USE_POSIX || defined __USE_MISC
00097 /* This is defined in POSIX.1:1996.  */
00098 __STDIO_INLINE int
00099 putc_unlocked (int __c, FILE *__stream)
00100 {
00101   return _IO_putc_unlocked (__c, __stream);
00102 }
00103 
00104 /* This is defined in POSIX.1:1996.  */
00105 __STDIO_INLINE int
00106 putchar_unlocked (int __c)
00107 {
00108   return _IO_putc_unlocked (__c, stdout);
00109 }
00110 # endif       /* POSIX || misc */
00111 
00112 
00113 # ifdef       __USE_GNU
00114 /* Like `getdelim', but reads up to a newline.  */
00115 __STDIO_INLINE _IO_ssize_t
00116 getline (char **__lineptr, size_t *__n, FILE *__stream)
00117 {
00118   return __getdelim (__lineptr, __n, '\n', __stream);
00119 }
00120 # endif /* GNU */
00121 
00122 
00123 # ifdef __USE_MISC
00124 /* Faster versions when locking is not required.  */
00125 __STDIO_INLINE int
00126 __NTH (feof_unlocked (FILE *__stream))
00127 {
00128   return _IO_feof_unlocked (__stream);
00129 }
00130 
00131 /* Faster versions when locking is not required.  */
00132 __STDIO_INLINE int
00133 __NTH (ferror_unlocked (FILE *__stream))
00134 {
00135   return _IO_ferror_unlocked (__stream);
00136 }
00137 # endif /* misc */
00138 
00139 #endif /* Use extern inlines.  */
00140 
00141 
00142 #if defined __USE_MISC && defined __GNUC__ && defined __OPTIMIZE__ \
00143     && !defined __cplusplus
00144 /* Perform some simple optimizations.  */
00145 # define fread_unlocked(ptr, size, n, stream) \
00146   (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n)    \
00147                  && (size_t) (size) * (size_t) (n) <= 8              \
00148                  && (size_t) (size) != 0)                            \
00149                 ? ({ char *__ptr = (char *) (ptr);                          \
00150                      FILE *__stream = (stream);                      \
00151                      size_t __cnt;                                   \
00152                      for (__cnt = (size_t) (size) * (size_t) (n);           \
00153                          __cnt > 0; --__cnt)                                \
00154                       {                                              \
00155                         int __c = _IO_getc_unlocked (__stream);             \
00156                         if (__c == EOF)                              \
00157                           break;                                     \
00158                         *__ptr++ = __c;                              \
00159                       }                                              \
00160                      ((size_t) (size) * (size_t) (n) - __cnt)               \
00161                      / (size_t) (size); })                                  \
00162                 : (((__builtin_constant_p (size) && (size_t) (size) == 0)   \
00163                     || (__builtin_constant_p (n) && (size_t) (n) == 0))     \
00164                      /* Evaluate all parameters once.  */                   \
00165                    ? ((void) (ptr), (void) (stream), (void) (size),         \
00166                      (void) (n), (size_t) 0)                                \
00167                    : fread_unlocked (ptr, size, n, stream))))
00168 
00169 # define fwrite_unlocked(ptr, size, n, stream) \
00170   (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n)    \
00171                  && (size_t) (size) * (size_t) (n) <= 8              \
00172                  && (size_t) (size) != 0)                            \
00173                 ? ({ const char *__ptr = (const char *) (ptr);       \
00174                      FILE *__stream = (stream);                      \
00175                      size_t __cnt;                                   \
00176                      for (__cnt = (size_t) (size) * (size_t) (n);           \
00177                          __cnt > 0; --__cnt)                                \
00178                       if (_IO_putc_unlocked (*__ptr++, __stream) == EOF)   \
00179                         break;                                       \
00180                      ((size_t) (size) * (size_t) (n) - __cnt)               \
00181                      / (size_t) (size); })                                  \
00182                 : (((__builtin_constant_p (size) && (size_t) (size) == 0)   \
00183                     || (__builtin_constant_p (n) && (size_t) (n) == 0))     \
00184                      /* Evaluate all parameters once.  */                   \
00185                    ? ((void) (ptr), (void) (stream), (void) (size),         \
00186                      (void) (n), (size_t) 0)                                \
00187                    : fwrite_unlocked (ptr, size, n, stream))))
00188 #endif
00189 
00190 /* Define helper macro.  */
00191 #undef __STDIO_INLINE