1 /* A GNU-like <stdio.h>.
3 Copyright (C) 2004, 2007-2010 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 @PRAGMA_SYSTEM_HEADER@
23 #if defined __need_FILE || defined __need___FILE
24 /* Special invocation convention inside glibc header files. */
26 #@INCLUDE_NEXT@ @NEXT_STDIO_H@
29 /* Normal invocation convention. */
33 /* The include_next requires a split double-inclusion guard. */
34 #@INCLUDE_NEXT@ @NEXT_STDIO_H@
39 /* Get va_list. Needed on many systems, including glibc 2.8. */
44 /* Get off_t and ssize_t. Needed on many systems, including glibc 2.8. */
45 #include <sys/types.h>
48 /* This feature is available in gcc versions 2.5 and later. */
49 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
50 # define __attribute__(Spec) /* empty */
52 /* The __-protected variants of `format' and `printf' attributes
53 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
54 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
55 # define __format__ format
56 # define __printf__ printf
61 /* The definition of GL_LINK_WARNING is copied here. */
63 /* The definition of _GL_ARG_NONNULL is copied here. */
71 # if @REPLACE_DPRINTF@
72 # define dprintf rpl_dprintf
74 # if @REPLACE_DPRINTF@ || !@HAVE_DPRINTF@
75 extern int dprintf (int fd, const char *format, ...)
76 __attribute__ ((__format__ (__printf__, 2, 3))) _GL_ARG_NONNULL ((2));
78 #elif defined GNULIB_POSIXCHECK
81 (GL_LINK_WARNING ("dprintf is unportable - " \
82 "use gnulib module dprintf for portability"), \
88 # define fclose rpl_fclose
89 /* Close STREAM and its underlying file descriptor. */
90 extern int fclose (FILE *stream) _GL_ARG_NONNULL ((1));
92 #elif defined GNULIB_POSIXCHECK
95 (GL_LINK_WARNING ("fclose is not always POSIX compliant - " \
96 "use gnulib module fclose for portable " \
97 "POSIX compliance"), \
102 # if @REPLACE_FFLUSH@
103 # define fflush rpl_fflush
104 /* Flush all pending data on STREAM according to POSIX rules. Both
105 output and seekable input streams are supported.
106 Note! LOSS OF DATA can occur if fflush is applied on an input stream
107 that is _not_seekable_ or on an update stream that is _not_seekable_
108 and in which the most recent operation was input. Seekability can
109 be tested with lseek(fileno(fp),0,SEEK_CUR). */
110 extern int fflush (FILE *gl_stream);
112 #elif defined GNULIB_POSIXCHECK
115 (GL_LINK_WARNING ("fflush is not always POSIX compliant - " \
116 "use gnulib module fflush for portable " \
117 "POSIX compliance"), \
124 # define fopen rpl_fopen
125 extern FILE * fopen (const char *filename, const char *mode)
126 _GL_ARG_NONNULL ((1, 2));
128 #elif defined GNULIB_POSIXCHECK
130 # define fopen(f,m) \
131 (GL_LINK_WARNING ("fopen on Win32 platforms is not POSIX compatible - " \
132 "use gnulib module fopen for portability"), \
136 #if @GNULIB_FPRINTF_POSIX@
137 # if @REPLACE_FPRINTF@
138 # define fprintf rpl_fprintf
139 extern int fprintf (FILE *fp, const char *format, ...)
140 __attribute__ ((__format__ (__printf__, 2, 3)))
141 _GL_ARG_NONNULL ((1, 2));
143 #elif @GNULIB_FPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
144 # define fprintf rpl_fprintf
145 extern int fprintf (FILE *fp, const char *format, ...)
146 __attribute__ ((__format__ (__printf__, 2, 3)))
147 _GL_ARG_NONNULL ((1, 2));
148 #elif defined GNULIB_POSIXCHECK
151 (GL_LINK_WARNING ("fprintf is not always POSIX compliant - " \
152 "use gnulib module fprintf-posix for portable " \
153 "POSIX compliance"), \
158 # if @REPLACE_FPURGE@
159 # define fpurge rpl_fpurge
161 # if @REPLACE_FPURGE@ || !@HAVE_DECL_FPURGE@
162 /* Discard all pending buffered I/O data on STREAM.
163 STREAM must not be wide-character oriented.
164 When discarding pending output, the file position is set back to where it
165 was before the write calls. When discarding pending input, the file
166 position is advanced to match the end of the previously read input.
167 Return 0 if successful. Upon error, return -1 and set errno. */
168 extern int fpurge (FILE *gl_stream) _GL_ARG_NONNULL ((1));
170 #elif defined GNULIB_POSIXCHECK
173 (GL_LINK_WARNING ("fpurge is not always present - " \
174 "use gnulib module fpurge for portability"), \
178 #if @GNULIB_FPUTC@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
180 # define fputc rpl_fputc
181 extern int fputc (int c, FILE *stream) _GL_ARG_NONNULL ((2));
184 #if @GNULIB_FPUTS@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
186 # define fputs rpl_fputs
187 extern int fputs (const char *string, FILE *stream) _GL_ARG_NONNULL ((1, 2));
191 # if @REPLACE_FREOPEN@
193 # define freopen rpl_freopen
194 extern FILE * freopen (const char *filename, const char *mode, FILE *stream)
195 _GL_ARG_NONNULL ((2, 3));
197 #elif defined GNULIB_POSIXCHECK
199 # define freopen(f,m,s) \
200 (GL_LINK_WARNING ("freopen on Win32 platforms is not POSIX compatible - " \
201 "use gnulib module freopen for portability"), \
205 #if @GNULIB_FSEEK@ && @REPLACE_FSEEK@
206 extern int rpl_fseek (FILE *fp, long offset, int whence) _GL_ARG_NONNULL ((1));
208 # if defined GNULIB_POSIXCHECK
209 # define fseek(f,o,w) \
210 (GL_LINK_WARNING ("fseek cannot handle files larger than 4 GB " \
211 "on 32-bit platforms - " \
212 "use fseeko function for handling of large files"), \
215 # define fseek rpl_fseek
217 #elif defined GNULIB_POSIXCHECK
219 # define fseek(f,o,w) \
220 (GL_LINK_WARNING ("fseek cannot handle files larger than 4 GB " \
221 "on 32-bit platforms - " \
222 "use fseeko function for handling of large files"), \
228 # if @REPLACE_FSEEKO@
229 /* Provide fseek, fseeko functions that are aware of a preceding
230 fflush(), and which detect pipes. */
231 # define fseeko rpl_fseeko
232 extern int fseeko (FILE *fp, off_t offset, int whence) _GL_ARG_NONNULL ((1));
235 # define fseek(f,o,w) \
236 (GL_LINK_WARNING ("fseek cannot handle files larger than 4 GB " \
237 "on 32-bit platforms - " \
238 "use fseeko function for handling of large files"), \
242 #elif defined GNULIB_POSIXCHECK
244 # define fseeko(f,o,w) \
245 (GL_LINK_WARNING ("fseeko is unportable - " \
246 "use gnulib module fseeko for portability"), \
250 #if @GNULIB_FTELL@ && @REPLACE_FTELL@
251 extern long rpl_ftell (FILE *fp) _GL_ARG_NONNULL ((1));
253 # if GNULIB_POSIXCHECK
255 (GL_LINK_WARNING ("ftell cannot handle files larger than 4 GB " \
256 "on 32-bit platforms - " \
257 "use ftello function for handling of large files"), \
260 # define ftell rpl_ftell
262 #elif defined GNULIB_POSIXCHECK
265 (GL_LINK_WARNING ("ftell cannot handle files larger than 4 GB " \
266 "on 32-bit platforms - " \
267 "use ftello function for handling of large files"), \
273 # if @REPLACE_FTELLO@
274 # define ftello rpl_ftello
275 extern off_t ftello (FILE *fp) _GL_ARG_NONNULL ((1));
279 (GL_LINK_WARNING ("ftell cannot handle files larger than 4 GB " \
280 "on 32-bit platforms - " \
281 "use ftello function for handling of large files"), \
285 #elif defined GNULIB_POSIXCHECK
288 (GL_LINK_WARNING ("ftello is unportable - " \
289 "use gnulib module ftello for portability"), \
293 #if @GNULIB_FWRITE@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
295 # define fwrite rpl_fwrite
296 extern size_t fwrite (const void *ptr, size_t s, size_t n, FILE *stream)
297 _GL_ARG_NONNULL ((1, 4));
300 #if @GNULIB_GETDELIM@
301 # if !@HAVE_DECL_GETDELIM@
302 /* Read input, up to (and including) the next occurrence of DELIMITER, from
303 STREAM, store it in *LINEPTR (and NUL-terminate it).
304 *LINEPTR is a pointer returned from malloc (or NULL), pointing to *LINESIZE
305 bytes of space. It is realloc'd as necessary.
306 Return the number of bytes read and stored at *LINEPTR (not including the
307 NUL terminator), or -1 on error or EOF. */
308 extern ssize_t getdelim (char **lineptr, size_t *linesize, int delimiter,
310 _GL_ARG_NONNULL ((1, 2, 4));
312 #elif defined GNULIB_POSIXCHECK
314 # define getdelim(l, s, d, f) \
315 (GL_LINK_WARNING ("getdelim is unportable - " \
316 "use gnulib module getdelim for portability"), \
317 getdelim (l, s, d, f))
321 # if @REPLACE_GETLINE@
323 # define getline rpl_getline
325 # if !@HAVE_DECL_GETLINE@ || @REPLACE_GETLINE@
326 /* Read a line, up to (and including) the next newline, from STREAM, store it
327 in *LINEPTR (and NUL-terminate it).
328 *LINEPTR is a pointer returned from malloc (or NULL), pointing to *LINESIZE
329 bytes of space. It is realloc'd as necessary.
330 Return the number of bytes read and stored at *LINEPTR (not including the
331 NUL terminator), or -1 on error or EOF. */
332 extern ssize_t getline (char **lineptr, size_t *linesize, FILE *stream)
333 _GL_ARG_NONNULL ((1, 2, 3));
335 #elif defined GNULIB_POSIXCHECK
337 # define getline(l, s, f) \
338 (GL_LINK_WARNING ("getline is unportable - " \
339 "use gnulib module getline for portability"), \
343 #if @GNULIB_OBSTACK_PRINTF@
344 # if @REPLACE_OBSTACK_PRINTF@
345 # define obstack_printf rpl_osbtack_printf
346 # define obstack_vprintf rpl_obstack_vprintf
348 # if @REPLACE_OBSTACK_PRINTF@ || !@HAVE_DECL_OBSTACK_PRINTF@
350 /* Grow an obstack with formatted output. Return the number of
351 bytes added to OBS. No trailing nul byte is added, and the
352 object should be closed with obstack_finish before use. Upon
353 memory allocation error, call obstack_alloc_failed_handler. Upon
354 other error, return -1. */
355 extern int obstack_printf (struct obstack *obs, const char *format, ...)
356 __attribute__ ((__format__ (__printf__, 2, 3))) _GL_ARG_NONNULL ((1, 2));
357 extern int obstack_vprintf (struct obstack *obs, const char *format,
359 __attribute__ ((__format__ (__printf__, 2, 0))) _GL_ARG_NONNULL ((1, 2));
364 # if @REPLACE_PERROR@
365 # define perror rpl_perror
366 /* Print a message to standard error, describing the value of ERRNO,
367 (if STRING is not NULL and not empty) prefixed with STRING and ": ",
368 and terminated with a newline. */
369 extern void perror (const char *string);
371 #elif defined GNULIB_POSIXCHECK
374 (GL_LINK_WARNING ("perror is not always POSIX compliant - " \
375 "use gnulib module perror for portability"), \
382 # define popen rpl_popen
383 extern FILE *popen (const char *cmd, const char *mode)
384 _GL_ARG_NONNULL ((1, 2));
386 #elif defined GNULIB_POSIXCHECK
388 # define popen(c,m) \
389 (GL_LINK_WARNING ("popen is buggy on some platforms - " \
390 "use gnulib module popen or pipe for more portability"), \
394 #if @GNULIB_PRINTF_POSIX@
395 # if @REPLACE_PRINTF@
396 /* Don't break __attribute__((format(printf,M,N))). */
397 # define printf __printf__
398 extern int printf (const char *format, ...)
399 __attribute__ ((__format__ (__printf__, 1, 2))) _GL_ARG_NONNULL ((1));
401 #elif @GNULIB_PRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
402 /* Don't break __attribute__((format(printf,M,N))). */
403 # define printf __printf__
404 extern int printf (const char *format, ...)
405 __attribute__ ((__format__ (__printf__, 1, 2))) _GL_ARG_NONNULL ((1));
406 #elif defined GNULIB_POSIXCHECK
409 (GL_LINK_WARNING ("printf is not always POSIX compliant - " \
410 "use gnulib module printf-posix for portable " \
411 "POSIX compliance"), \
413 /* Don't break __attribute__((format(printf,M,N))). */
414 # define format(kind,m,n) format (__##kind##__, m, n)
415 # define __format__(kind,m,n) __format__ (__##kind##__, m, n)
416 # define ____printf____ __printf__
417 # define ____scanf____ __scanf__
418 # define ____strftime____ __strftime__
419 # define ____strfmon____ __strfmon__
422 #if @GNULIB_PUTC@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
424 # define putc rpl_fputc
425 extern int putc (int c, FILE *stream) _GL_ARG_NONNULL ((2));
428 #if @GNULIB_PUTCHAR@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
430 # define putchar rpl_putchar
431 extern int putchar (int c);
434 #if @GNULIB_PUTS@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
436 # define puts rpl_puts
437 extern int puts (const char *string) _GL_ARG_NONNULL ((1));
441 # if @REPLACE_REMOVE@
443 # define remove rpl_remove
444 extern int remove (const char *name) _GL_ARG_NONNULL ((1));
446 #elif defined GNULIB_POSIXCHECK
449 (GL_LINK_WARNING ("remove cannot handle directories on some platforms - " \
450 "use gnulib module remove for more portability"), \
455 # if @REPLACE_RENAME@
457 # define rename rpl_rename
458 extern int rename (const char *old_filename, const char *new_filename)
459 _GL_ARG_NONNULL ((1, 2));
461 #elif defined GNULIB_POSIXCHECK
463 # define rename(o,n) \
464 (GL_LINK_WARNING ("rename is buggy on some platforms - " \
465 "use gnulib module rename for more portability"), \
469 #if @GNULIB_RENAMEAT@
470 # if @REPLACE_RENAMEAT@
472 # define renameat rpl_renameat
474 # if !@HAVE_RENAMEAT@ || @REPLACE_RENAMEAT@
475 extern int renameat (int fd1, char const *file1, int fd2, char const *file2)
476 _GL_ARG_NONNULL ((2, 4));
478 #elif defined GNULIB_POSIXCHECK
480 # define renameat(d1,f1,d2,f2) \
481 (GL_LINK_WARNING ("renameat is not portable - " \
482 "use gnulib module renameat for portability"), \
483 renameat (d1, f1, d2, f2))
486 #if @GNULIB_SNPRINTF@
487 # if @REPLACE_SNPRINTF@
488 # define snprintf rpl_snprintf
490 # if @REPLACE_SNPRINTF@ || !@HAVE_DECL_SNPRINTF@
491 extern int snprintf (char *str, size_t size, const char *format, ...)
492 __attribute__ ((__format__ (__printf__, 3, 4)))
493 _GL_ARG_NONNULL ((3));
495 #elif defined GNULIB_POSIXCHECK
498 (GL_LINK_WARNING ("snprintf is unportable - " \
499 "use gnulib module snprintf for portability"), \
503 #if @GNULIB_SPRINTF_POSIX@
504 # if @REPLACE_SPRINTF@
505 # define sprintf rpl_sprintf
506 extern int sprintf (char *str, const char *format, ...)
507 __attribute__ ((__format__ (__printf__, 2, 3)))
508 _GL_ARG_NONNULL ((1, 2));
510 #elif defined GNULIB_POSIXCHECK
513 (GL_LINK_WARNING ("sprintf is not always POSIX compliant - " \
514 "use gnulib module sprintf-posix for portable " \
515 "POSIX compliance"), \
519 #if @GNULIB_VASPRINTF@
520 # if @REPLACE_VASPRINTF@
521 # define asprintf rpl_asprintf
522 # define vasprintf rpl_vasprintf
524 # if @REPLACE_VASPRINTF@ || !@HAVE_VASPRINTF@
525 /* Write formatted output to a string dynamically allocated with malloc().
526 If the memory allocation succeeds, store the address of the string in
527 *RESULT and return the number of resulting bytes, excluding the trailing
528 NUL. Upon memory allocation error, or some other error, return -1. */
529 extern int asprintf (char **result, const char *format, ...)
530 __attribute__ ((__format__ (__printf__, 2, 3))) _GL_ARG_NONNULL ((1, 2));
531 extern int vasprintf (char **result, const char *format, va_list args)
532 __attribute__ ((__format__ (__printf__, 2, 0))) _GL_ARG_NONNULL ((1, 2));
536 #if @GNULIB_VDPRINTF@
537 # if @REPLACE_VDPRINTF@
538 # define vdprintf rpl_vdprintf
540 # if @REPLACE_VDPRINTF@ || !@HAVE_VDPRINTF@
541 extern int vdprintf (int fd, const char *format, va_list args)
542 __attribute__ ((__format__ (__printf__, 2, 0))) _GL_ARG_NONNULL ((2));
544 #elif defined GNULIB_POSIXCHECK
546 # define vdprintf(d,f,a) \
547 (GL_LINK_WARNING ("vdprintf is unportable - " \
548 "use gnulib module vdprintf for portability"), \
552 #if @GNULIB_VFPRINTF_POSIX@
553 # if @REPLACE_VFPRINTF@
554 # define vfprintf rpl_vfprintf
555 extern int vfprintf (FILE *fp, const char *format, va_list args)
556 __attribute__ ((__format__ (__printf__, 2, 0)))
557 _GL_ARG_NONNULL ((1, 2));
559 #elif @GNULIB_VFPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
560 # define vfprintf rpl_vfprintf
561 extern int vfprintf (FILE *fp, const char *format, va_list args)
562 __attribute__ ((__format__ (__printf__, 2, 0)))
563 _GL_ARG_NONNULL ((1, 2));
564 #elif defined GNULIB_POSIXCHECK
566 # define vfprintf(s,f,a) \
567 (GL_LINK_WARNING ("vfprintf is not always POSIX compliant - " \
568 "use gnulib module vfprintf-posix for portable " \
569 "POSIX compliance"), \
573 #if @GNULIB_VPRINTF_POSIX@
574 # if @REPLACE_VPRINTF@
575 # define vprintf rpl_vprintf
576 extern int vprintf (const char *format, va_list args)
577 __attribute__ ((__format__ (__printf__, 1, 0))) _GL_ARG_NONNULL ((1));
579 #elif @GNULIB_VPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
580 # define vprintf rpl_vprintf
581 extern int vprintf (const char *format, va_list args)
582 __attribute__ ((__format__ (__printf__, 1, 0))) _GL_ARG_NONNULL ((1));
583 #elif defined GNULIB_POSIXCHECK
585 # define vprintf(f,a) \
586 (GL_LINK_WARNING ("vprintf is not always POSIX compliant - " \
587 "use gnulib module vprintf-posix for portable " \
588 "POSIX compliance"), \
592 #if @GNULIB_VSNPRINTF@
593 # if @REPLACE_VSNPRINTF@
594 # define vsnprintf rpl_vsnprintf
596 # if @REPLACE_VSNPRINTF@ || !@HAVE_DECL_VSNPRINTF@
597 extern int vsnprintf (char *str, size_t size, const char *format, va_list args)
598 __attribute__ ((__format__ (__printf__, 3, 0)))
599 _GL_ARG_NONNULL ((3));
601 #elif defined GNULIB_POSIXCHECK
603 # define vsnprintf(b,s,f,a) \
604 (GL_LINK_WARNING ("vsnprintf is unportable - " \
605 "use gnulib module vsnprintf for portability"), \
606 vsnprintf (b, s, f, a))
609 #if @GNULIB_VSPRINTF_POSIX@
610 # if @REPLACE_VSPRINTF@
611 # define vsprintf rpl_vsprintf
612 extern int vsprintf (char *str, const char *format, va_list args)
613 __attribute__ ((__format__ (__printf__, 2, 0)))
614 _GL_ARG_NONNULL ((1, 2));
616 #elif defined GNULIB_POSIXCHECK
618 # define vsprintf(b,f,a) \
619 (GL_LINK_WARNING ("vsprintf is not always POSIX compliant - " \
620 "use gnulib module vsprintf-posix for portable " \
621 "POSIX compliance"), \
629 #endif /* _GL_STDIO_H */
630 #endif /* _GL_STDIO_H */