Back to index

glibc  2.9
stdio2.h
Go to the documentation of this file.
00001 /* Checking macros for stdio functions.
00002    Copyright (C) 2004, 2005, 2007, 2008 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/stdio2.h> directly; use <stdio.h> instead."
00022 #endif
00023 
00024 extern int __sprintf_chk (char *__restrict __s, int __flag, size_t __slen,
00025                        __const char *__restrict __format, ...) __THROW;
00026 extern int __vsprintf_chk (char *__restrict __s, int __flag, size_t __slen,
00027                         __const char *__restrict __format,
00028                         _G_va_list __ap) __THROW;
00029 
00030 #ifdef __va_arg_pack
00031 __extern_always_inline int
00032 __NTH (sprintf (char *__restrict __s, __const char *__restrict __fmt, ...))
00033 {
00034   return __builtin___sprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
00035                               __bos (__s), __fmt, __va_arg_pack ());
00036 }
00037 #elif !defined __cplusplus
00038 # define sprintf(str, ...) \
00039   __builtin___sprintf_chk (str, __USE_FORTIFY_LEVEL - 1, __bos (str), \
00040                         __VA_ARGS__)
00041 #endif
00042 
00043 __extern_always_inline int
00044 __NTH (vsprintf (char *__restrict __s, __const char *__restrict __fmt,
00045                _G_va_list __ap))
00046 {
00047   return __builtin___vsprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
00048                                __bos (__s), __fmt, __ap);
00049 }
00050 
00051 #if defined __USE_BSD || defined __USE_ISOC99 || defined __USE_UNIX98
00052 
00053 extern int __snprintf_chk (char *__restrict __s, size_t __n, int __flag,
00054                         size_t __slen, __const char *__restrict __format,
00055                         ...) __THROW;
00056 extern int __vsnprintf_chk (char *__restrict __s, size_t __n, int __flag,
00057                          size_t __slen, __const char *__restrict __format,
00058                          _G_va_list __ap) __THROW;
00059 
00060 # ifdef __va_arg_pack
00061 __extern_always_inline int
00062 __NTH (snprintf (char *__restrict __s, size_t __n,
00063                __const char *__restrict __fmt, ...))
00064 {
00065   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
00066                                __bos (__s), __fmt, __va_arg_pack ());
00067 }
00068 # elif !defined __cplusplus
00069 #  define snprintf(str, len, ...) \
00070   __builtin___snprintf_chk (str, len, __USE_FORTIFY_LEVEL - 1, __bos (str), \
00071                          __VA_ARGS__)
00072 # endif
00073 
00074 __extern_always_inline int
00075 __NTH (vsnprintf (char *__restrict __s, size_t __n,
00076                 __const char *__restrict __fmt, _G_va_list __ap))
00077 {
00078   return __builtin___vsnprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
00079                                 __bos (__s), __fmt, __ap);
00080 }
00081 
00082 #endif
00083 
00084 #if __USE_FORTIFY_LEVEL > 1
00085 
00086 extern int __fprintf_chk (FILE *__restrict __stream, int __flag,
00087                        __const char *__restrict __format, ...);
00088 extern int __printf_chk (int __flag, __const char *__restrict __format, ...);
00089 extern int __vfprintf_chk (FILE *__restrict __stream, int __flag,
00090                         __const char *__restrict __format, _G_va_list __ap);
00091 extern int __vprintf_chk (int __flag, __const char *__restrict __format,
00092                        _G_va_list __ap);
00093 
00094 # ifdef __va_arg_pack
00095 __extern_always_inline int
00096 fprintf (FILE *__restrict __stream, __const char *__restrict __fmt, ...)
00097 {
00098   return __fprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt,
00099                      __va_arg_pack ());
00100 }
00101 
00102 __extern_always_inline int
00103 printf (__const char *__restrict __fmt, ...)
00104 {
00105   return __printf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ());
00106 }
00107 # elif !defined __cplusplus
00108 #  define printf(...) \
00109   __printf_chk (__USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
00110 #  define fprintf(stream, ...) \
00111   __fprintf_chk (stream, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
00112 # endif
00113 
00114 __extern_always_inline int
00115 vprintf (__const char *__restrict __fmt, _G_va_list __ap)
00116 {
00117 #ifdef __USE_EXTERN_INLINES
00118   return __vfprintf_chk (stdout, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
00119 #else
00120   return __vprintf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __ap);
00121 #endif
00122 }
00123 
00124 __extern_always_inline int
00125 vfprintf (FILE *__restrict __stream,
00126          __const char *__restrict __fmt, _G_va_list __ap)
00127 {
00128   return __vfprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
00129 }
00130 
00131 # ifdef __USE_GNU
00132 
00133 extern int __asprintf_chk (char **__restrict __ptr, int __flag,
00134                         __const char *__restrict __fmt, ...)
00135      __THROW __attribute__ ((__format__ (__printf__, 3, 4))) __wur;
00136 extern int __vasprintf_chk (char **__restrict __ptr, int __flag,
00137                          __const char *__restrict __fmt, _G_va_list __arg)
00138      __THROW __attribute__ ((__format__ (__printf__, 3, 0))) __wur;
00139 extern int __dprintf_chk (int __fd, int __flag, __const char *__restrict __fmt,
00140                        ...) __attribute__ ((__format__ (__printf__, 3, 4)));
00141 extern int __vdprintf_chk (int __fd, int __flag,
00142                         __const char *__restrict __fmt, _G_va_list __arg)
00143      __attribute__ ((__format__ (__printf__, 3, 0)));
00144 extern int __obstack_printf_chk (struct obstack *__restrict __obstack,
00145                              int __flag, __const char *__restrict __format,
00146                              ...)
00147      __THROW __attribute__ ((__format__ (__printf__, 3, 4)));
00148 extern int __obstack_vprintf_chk (struct obstack *__restrict __obstack,
00149                               int __flag,
00150                               __const char *__restrict __format,
00151                               _G_va_list __args)
00152      __THROW __attribute__ ((__format__ (__printf__, 3, 0)));
00153 
00154 #  ifdef __va_arg_pack
00155 __extern_always_inline int
00156 __NTH (asprintf (char **__restrict __ptr, __const char *__restrict __fmt, ...))
00157 {
00158   return __asprintf_chk (__ptr, __USE_FORTIFY_LEVEL - 1, __fmt,
00159                       __va_arg_pack ());
00160 }
00161 
00162 __extern_always_inline int
00163 __NTH (__asprintf (char **__restrict __ptr, __const char *__restrict __fmt,
00164                  ...))
00165 {
00166   return __asprintf_chk (__ptr, __USE_FORTIFY_LEVEL - 1, __fmt,
00167                       __va_arg_pack ());
00168 }
00169 
00170 __extern_always_inline int
00171 dprintf (int __fd, __const char *__restrict __fmt, ...)
00172 {
00173   return __dprintf_chk (__fd, __USE_FORTIFY_LEVEL - 1, __fmt,
00174                      __va_arg_pack ());
00175 }
00176 
00177 __extern_always_inline int
00178 __NTH (obstack_printf (struct obstack *__restrict __obstack,
00179                      __const char *__restrict __fmt, ...))
00180 {
00181   return __obstack_printf_chk (__obstack, __USE_FORTIFY_LEVEL - 1, __fmt,
00182                             __va_arg_pack ());
00183 }
00184 #  elif !defined __cplusplus
00185 #   define asprintf(ptr, ...) \
00186   __asprintf_chk (ptr, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
00187 #   define __asprintf(ptr, ...) \
00188   __asprintf_chk (ptr, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
00189 #   define dprintf(fd, ...) \
00190   __dprintf_chk (fd, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
00191 #   define obstack_printf(obstack, ...) \
00192   __obstack_printf_chk (obstack, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
00193 #  endif
00194 
00195 __extern_always_inline int
00196 __NTH (vasprintf (char **__restrict __ptr, __const char *__restrict __fmt,
00197                 _G_va_list __ap))
00198 {
00199   return __vasprintf_chk (__ptr, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
00200 }
00201 
00202 __extern_always_inline int
00203 vdprintf (int __fd, __const char *__restrict __fmt, _G_va_list __ap)
00204 {
00205   return __vdprintf_chk (__fd, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
00206 }
00207 
00208 __extern_always_inline int
00209 __NTH (obstack_vprintf (struct obstack *__restrict __obstack,
00210                      __const char *__restrict __fmt, _G_va_list __ap))
00211 {
00212   return __obstack_vprintf_chk (__obstack, __USE_FORTIFY_LEVEL - 1, __fmt,
00213                             __ap);
00214 }
00215 
00216 # endif
00217 
00218 #endif
00219 
00220 extern char *__gets_chk (char *__str, size_t) __wur;
00221 extern char *__REDIRECT (__gets_warn, (char *__str), gets)
00222      __wur __warnattr ("please use fgets or getline instead, gets can't "
00223                      "specify buffer size");
00224 
00225 __extern_always_inline __wur char *
00226 gets (char *__str)
00227 {
00228   if (__bos (__str) != (size_t) -1)
00229     return __gets_chk (__str, __bos (__str));
00230   return __gets_warn (__str);
00231 }
00232 
00233 extern char *__fgets_chk (char *__restrict __s, size_t __size, int __n,
00234                        FILE *__restrict __stream) __wur;
00235 extern char *__REDIRECT (__fgets_alias,
00236                       (char *__restrict __s, int __n,
00237                        FILE *__restrict __stream), fgets) __wur;
00238 extern char *__REDIRECT (__fgets_chk_warn,
00239                       (char *__restrict __s, size_t __size, int __n,
00240                        FILE *__restrict __stream), __fgets_chk)
00241      __wur __warnattr ("fgets called with bigger size than length "
00242                      "of destination buffer");
00243 
00244 __extern_always_inline __wur char *
00245 fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
00246 {
00247   if (__bos (__s) != (size_t) -1)
00248     {
00249       if (!__builtin_constant_p (__n) || __n <= 0)
00250        return __fgets_chk (__s, __bos (__s), __n, __stream);
00251 
00252       if ((size_t) __n > __bos (__s))
00253        return __fgets_chk_warn (__s, __bos (__s), __n, __stream);
00254     }
00255   return __fgets_alias (__s, __n, __stream);
00256 }
00257 
00258 extern size_t __fread_chk (void *__restrict __ptr, size_t __ptrlen,
00259                         size_t __size, size_t __n,
00260                         FILE *__restrict __stream) __wur;
00261 extern size_t __REDIRECT (__fread_alias,
00262                        (void *__restrict __ptr, size_t __size,
00263                         size_t __n, FILE *__restrict __stream),
00264                        fread) __wur;
00265 extern size_t __REDIRECT (__fread_chk_warn,
00266                        (void *__restrict __ptr, size_t __ptrlen,
00267                         size_t __size, size_t __n,
00268                         FILE *__restrict __stream),
00269                        __fread_chk)
00270      __wur __warnattr ("fread called with bigger size * nmemb than length "
00271                      "of destination buffer");
00272 
00273 __extern_always_inline __wur size_t
00274 fread (void *__restrict __ptr, size_t __size, size_t __n,
00275        FILE *__restrict __stream)
00276 {
00277   if (__bos0 (__ptr) != (size_t) -1)
00278     {
00279       if (!__builtin_constant_p (__size)
00280          || !__builtin_constant_p (__n)
00281          || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
00282        return __fread_chk (__ptr, __bos0 (__ptr), __size, __n, __stream);
00283 
00284       if (__size * __n > __bos0 (__ptr))
00285        return __fread_chk_warn (__ptr, __bos0 (__ptr), __size, __n, __stream);
00286     }
00287   return __fread_alias (__ptr, __size, __n, __stream);
00288 }
00289 
00290 #ifdef __USE_GNU
00291 extern char *__fgets_unlocked_chk (char *__restrict __s, size_t __size,
00292                                int __n, FILE *__restrict __stream) __wur;
00293 extern char *__REDIRECT (__fgets_unlocked_alias,
00294                       (char *__restrict __s, int __n,
00295                        FILE *__restrict __stream), fgets_unlocked) __wur;
00296 extern char *__REDIRECT (__fgets_unlocked_chk_warn,
00297                       (char *__restrict __s, size_t __size, int __n,
00298                        FILE *__restrict __stream), __fgets_unlocked_chk)
00299      __wur __warnattr ("fgets_unlocked called with bigger size than length "
00300                      "of destination buffer");
00301 
00302 __extern_always_inline __wur char *
00303 fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream)
00304 {
00305   if (__bos (__s) != (size_t) -1)
00306     {
00307       if (!__builtin_constant_p (__n) || __n <= 0)
00308        return __fgets_unlocked_chk (__s, __bos (__s), __n, __stream);
00309 
00310       if ((size_t) __n > __bos (__s))
00311        return __fgets_unlocked_chk_warn (__s, __bos (__s), __n, __stream);
00312     }
00313   return __fgets_unlocked_alias (__s, __n, __stream);
00314 }
00315 #endif
00316 
00317 #ifdef __USE_MISC
00318 # undef fread_unlocked
00319 extern size_t __fread_unlocked_chk (void *__restrict __ptr, size_t __ptrlen,
00320                                 size_t __size, size_t __n,
00321                                 FILE *__restrict __stream) __wur;
00322 extern size_t __REDIRECT (__fread_unlocked_alias,
00323                        (void *__restrict __ptr, size_t __size,
00324                         size_t __n, FILE *__restrict __stream),
00325                        fread_unlocked) __wur;
00326 extern size_t __REDIRECT (__fread_unlocked_chk_warn,
00327                        (void *__restrict __ptr, size_t __ptrlen,
00328                         size_t __size, size_t __n,
00329                         FILE *__restrict __stream),
00330                        __fread_unlocked_chk)
00331      __wur __warnattr ("fread_unlocked called with bigger size * nmemb than "
00332                      "length of destination buffer");
00333 
00334 __extern_always_inline __wur size_t
00335 fread_unlocked (void *__restrict __ptr, size_t __size, size_t __n,
00336               FILE *__restrict __stream)
00337 {
00338   if (__bos0 (__ptr) != (size_t) -1)
00339     {
00340       if (!__builtin_constant_p (__size)
00341          || !__builtin_constant_p (__n)
00342          || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
00343        return __fread_unlocked_chk (__ptr, __bos0 (__ptr), __size, __n,
00344                                  __stream);
00345 
00346       if (__size * __n > __bos0 (__ptr))
00347        return __fread_unlocked_chk_warn (__ptr, __bos0 (__ptr), __size, __n,
00348                                      __stream);
00349     }
00350 
00351 # ifdef __USE_EXTERN_INLINES
00352   if (__builtin_constant_p (__size)
00353       && __builtin_constant_p (__n)
00354       && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
00355       && __size * __n <= 8)
00356     {
00357       size_t __cnt = __size * __n;
00358       char *__cptr = (char *) __ptr;
00359       if (__cnt == 0)
00360        return 0;
00361 
00362       for (; __cnt > 0; --__cnt)
00363        {
00364          int __c = _IO_getc_unlocked (__stream);
00365          if (__c == EOF)
00366            break;
00367          *__cptr++ = __c;
00368        }
00369       return (__cptr - (char *) __ptr) / __size;
00370     }
00371 # endif
00372   return __fread_unlocked_alias (__ptr, __size, __n, __stream);
00373 }
00374 #endif