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@
42 #if (@GNULIB_FSEEKO@ || @GNULIB_FTELLO@ || @GNULIB_GETDELIM@ \
43 || @GNULIB_GETLINE@ || defined GNULIB_POSIXCHECK)
44 /* Get off_t and ssize_t. */
45 # include <sys/types.h>
49 /* This feature is available in gcc versions 2.5 and later. */
50 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
51 # define __attribute__(Spec) /* empty */
53 /* The __-protected variants of `format' and `printf' attributes
54 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
55 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
56 # define __format__ format
57 # define __printf__ printf
62 /* The definition of GL_LINK_WARNING is copied here. */
64 /* The definition of _GL_ARG_NONNULL is copied here. */
72 # if @REPLACE_DPRINTF@
73 # define dprintf rpl_dprintf
75 # if @REPLACE_DPRINTF@ || !@HAVE_DPRINTF@
76 extern int dprintf (int fd, const char *format, ...)
77 __attribute__ ((__format__ (__printf__, 2, 3))) _GL_ARG_NONNULL ((2));
79 #elif defined GNULIB_POSIXCHECK
82 (GL_LINK_WARNING ("dprintf is unportable - " \
83 "use gnulib module dprintf for portability"), \
89 # define fclose rpl_fclose
90 /* Close STREAM and its underlying file descriptor. */
91 extern int fclose (FILE *stream) _GL_ARG_NONNULL ((1));
93 #elif defined GNULIB_POSIXCHECK
96 (GL_LINK_WARNING ("fclose is not always POSIX compliant - " \
97 "use gnulib module fclose for portable " \
98 "POSIX compliance"), \
103 # if @REPLACE_FFLUSH@
104 # define fflush rpl_fflush
105 /* Flush all pending data on STREAM according to POSIX rules. Both
106 output and seekable input streams are supported.
107 Note! LOSS OF DATA can occur if fflush is applied on an input stream
108 that is _not_seekable_ or on an update stream that is _not_seekable_
109 and in which the most recent operation was input. Seekability can
110 be tested with lseek(fileno(fp),0,SEEK_CUR). */
111 extern int fflush (FILE *gl_stream);
113 #elif defined GNULIB_POSIXCHECK
116 (GL_LINK_WARNING ("fflush is not always POSIX compliant - " \
117 "use gnulib module fflush for portable " \
118 "POSIX compliance"), \
125 # define fopen rpl_fopen
126 extern FILE * fopen (const char *filename, const char *mode)
127 _GL_ARG_NONNULL ((1, 2));
129 #elif defined GNULIB_POSIXCHECK
131 # define fopen(f,m) \
132 (GL_LINK_WARNING ("fopen on Win32 platforms is not POSIX compatible - " \
133 "use gnulib module fopen for portability"), \
137 #if @GNULIB_FPRINTF_POSIX@
138 # if @REPLACE_FPRINTF@
139 # define fprintf rpl_fprintf
140 extern int fprintf (FILE *fp, const char *format, ...)
141 __attribute__ ((__format__ (__printf__, 2, 3)))
142 _GL_ARG_NONNULL ((1, 2));
144 #elif @GNULIB_FPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
145 # define fprintf rpl_fprintf
146 extern int fprintf (FILE *fp, const char *format, ...)
147 __attribute__ ((__format__ (__printf__, 2, 3)))
148 _GL_ARG_NONNULL ((1, 2));
149 #elif defined GNULIB_POSIXCHECK
152 (GL_LINK_WARNING ("fprintf is not always POSIX compliant - " \
153 "use gnulib module fprintf-posix for portable " \
154 "POSIX compliance"), \
159 # if @REPLACE_FPURGE@
160 # define fpurge rpl_fpurge
162 # if @REPLACE_FPURGE@ || !@HAVE_DECL_FPURGE@
163 /* Discard all pending buffered I/O data on STREAM.
164 STREAM must not be wide-character oriented.
165 When discarding pending output, the file position is set back to where it
166 was before the write calls. When discarding pending input, the file
167 position is advanced to match the end of the previously read input.
168 Return 0 if successful. Upon error, return -1 and set errno. */
169 extern int fpurge (FILE *gl_stream) _GL_ARG_NONNULL ((1));
171 #elif defined GNULIB_POSIXCHECK
174 (GL_LINK_WARNING ("fpurge is not always present - " \
175 "use gnulib module fpurge for portability"), \
179 #if @GNULIB_FPUTC@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
181 # define fputc rpl_fputc
182 extern int fputc (int c, FILE *stream) _GL_ARG_NONNULL ((2));
185 #if @GNULIB_FPUTS@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
187 # define fputs rpl_fputs
188 extern int fputs (const char *string, FILE *stream) _GL_ARG_NONNULL ((1, 2));
192 # if @REPLACE_FREOPEN@
194 # define freopen rpl_freopen
195 extern FILE * freopen (const char *filename, const char *mode, FILE *stream)
196 _GL_ARG_NONNULL ((2, 3));
198 #elif defined GNULIB_POSIXCHECK
200 # define freopen(f,m,s) \
201 (GL_LINK_WARNING ("freopen on Win32 platforms is not POSIX compatible - " \
202 "use gnulib module freopen for portability"), \
206 #if @GNULIB_FSEEK@ && @REPLACE_FSEEK@
207 extern int rpl_fseek (FILE *fp, long offset, int whence) _GL_ARG_NONNULL ((1));
209 # if defined GNULIB_POSIXCHECK
210 # define fseek(f,o,w) \
211 (GL_LINK_WARNING ("fseek cannot handle files larger than 4 GB " \
212 "on 32-bit platforms - " \
213 "use fseeko function for handling of large files"), \
216 # define fseek rpl_fseek
218 #elif defined GNULIB_POSIXCHECK
220 # define fseek(f,o,w) \
221 (GL_LINK_WARNING ("fseek cannot handle files larger than 4 GB " \
222 "on 32-bit platforms - " \
223 "use fseeko function for handling of large files"), \
229 # if @REPLACE_FSEEKO@
230 /* Provide fseek, fseeko functions that are aware of a preceding
231 fflush(), and which detect pipes. */
232 # define fseeko rpl_fseeko
233 extern int fseeko (FILE *fp, off_t offset, int whence) _GL_ARG_NONNULL ((1));
236 # define fseek(f,o,w) \
237 (GL_LINK_WARNING ("fseek cannot handle files larger than 4 GB " \
238 "on 32-bit platforms - " \
239 "use fseeko function for handling of large files"), \
243 #elif defined GNULIB_POSIXCHECK
245 # define fseeko(f,o,w) \
246 (GL_LINK_WARNING ("fseeko is unportable - " \
247 "use gnulib module fseeko for portability"), \
251 #if @GNULIB_FTELL@ && @REPLACE_FTELL@
252 extern long rpl_ftell (FILE *fp) _GL_ARG_NONNULL ((1));
254 # if GNULIB_POSIXCHECK
256 (GL_LINK_WARNING ("ftell cannot handle files larger than 4 GB " \
257 "on 32-bit platforms - " \
258 "use ftello function for handling of large files"), \
261 # define ftell rpl_ftell
263 #elif defined GNULIB_POSIXCHECK
266 (GL_LINK_WARNING ("ftell cannot handle files larger than 4 GB " \
267 "on 32-bit platforms - " \
268 "use ftello function for handling of large files"), \
274 # if @REPLACE_FTELLO@
275 # define ftello rpl_ftello
276 extern off_t ftello (FILE *fp) _GL_ARG_NONNULL ((1));
280 (GL_LINK_WARNING ("ftell cannot handle files larger than 4 GB " \
281 "on 32-bit platforms - " \
282 "use ftello function for handling of large files"), \
286 #elif defined GNULIB_POSIXCHECK
289 (GL_LINK_WARNING ("ftello is unportable - " \
290 "use gnulib module ftello for portability"), \
294 #if @GNULIB_FWRITE@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
296 # define fwrite rpl_fwrite
297 extern size_t fwrite (const void *ptr, size_t s, size_t n, FILE *stream)
298 _GL_ARG_NONNULL ((1, 4));
301 #if @GNULIB_GETDELIM@
302 # if !@HAVE_DECL_GETDELIM@
303 /* Read input, up to (and including) the next occurrence of DELIMITER, from
304 STREAM, store it in *LINEPTR (and NUL-terminate it).
305 *LINEPTR is a pointer returned from malloc (or NULL), pointing to *LINESIZE
306 bytes of space. It is realloc'd as necessary.
307 Return the number of bytes read and stored at *LINEPTR (not including the
308 NUL terminator), or -1 on error or EOF. */
309 extern ssize_t getdelim (char **lineptr, size_t *linesize, int delimiter,
311 _GL_ARG_NONNULL ((1, 2, 4));
313 #elif defined GNULIB_POSIXCHECK
315 # define getdelim(l, s, d, f) \
316 (GL_LINK_WARNING ("getdelim is unportable - " \
317 "use gnulib module getdelim for portability"), \
318 getdelim (l, s, d, f))
322 # if @REPLACE_GETLINE@
324 # define getline rpl_getline
326 # if !@HAVE_DECL_GETLINE@ || @REPLACE_GETLINE@
327 /* Read a line, up to (and including) the next newline, from STREAM, store it
328 in *LINEPTR (and NUL-terminate it).
329 *LINEPTR is a pointer returned from malloc (or NULL), pointing to *LINESIZE
330 bytes of space. It is realloc'd as necessary.
331 Return the number of bytes read and stored at *LINEPTR (not including the
332 NUL terminator), or -1 on error or EOF. */
333 extern ssize_t getline (char **lineptr, size_t *linesize, FILE *stream)
334 _GL_ARG_NONNULL ((1, 2, 3));
336 #elif defined GNULIB_POSIXCHECK
338 # define getline(l, s, f) \
339 (GL_LINK_WARNING ("getline is unportable - " \
340 "use gnulib module getline for portability"), \
344 #if @GNULIB_OBSTACK_PRINTF@
345 # if @REPLACE_OBSTACK_PRINTF@
346 # define obstack_printf rpl_osbtack_printf
347 # define obstack_vprintf rpl_obstack_vprintf
349 # if @REPLACE_OBSTACK_PRINTF@ || !@HAVE_DECL_OBSTACK_PRINTF@
351 /* Grow an obstack with formatted output. Return the number of
352 bytes added to OBS. No trailing nul byte is added, and the
353 object should be closed with obstack_finish before use. Upon
354 memory allocation error, call obstack_alloc_failed_handler. Upon
355 other error, return -1. */
356 extern int obstack_printf (struct obstack *obs, const char *format, ...)
357 __attribute__ ((__format__ (__printf__, 2, 3))) _GL_ARG_NONNULL ((1, 2));
358 extern int obstack_vprintf (struct obstack *obs, const char *format,
360 __attribute__ ((__format__ (__printf__, 2, 0))) _GL_ARG_NONNULL ((1, 2));
365 # if @REPLACE_PERROR@
366 # define perror rpl_perror
367 /* Print a message to standard error, describing the value of ERRNO,
368 (if STRING is not NULL and not empty) prefixed with STRING and ": ",
369 and terminated with a newline. */
370 extern void perror (const char *string);
372 #elif defined GNULIB_POSIXCHECK
375 (GL_LINK_WARNING ("perror is not always POSIX compliant - " \
376 "use gnulib module perror for portability"), \
383 # define popen rpl_popen
384 extern FILE *popen (const char *cmd, const char *mode)
385 _GL_ARG_NONNULL ((1, 2));
387 #elif defined GNULIB_POSIXCHECK
389 # define popen(c,m) \
390 (GL_LINK_WARNING ("popen is buggy on some platforms - " \
391 "use gnulib module popen or pipe for more portability"), \
395 #if @GNULIB_PRINTF_POSIX@
396 # if @REPLACE_PRINTF@
397 /* Don't break __attribute__((format(printf,M,N))). */
398 # define printf __printf__
399 extern int printf (const char *format, ...)
400 __attribute__ ((__format__ (__printf__, 1, 2))) _GL_ARG_NONNULL ((1));
402 #elif @GNULIB_PRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
403 /* Don't break __attribute__((format(printf,M,N))). */
404 # define printf __printf__
405 extern int printf (const char *format, ...)
406 __attribute__ ((__format__ (__printf__, 1, 2))) _GL_ARG_NONNULL ((1));
407 #elif defined GNULIB_POSIXCHECK
410 (GL_LINK_WARNING ("printf is not always POSIX compliant - " \
411 "use gnulib module printf-posix for portable " \
412 "POSIX compliance"), \
414 /* Don't break __attribute__((format(printf,M,N))). */
415 # define format(kind,m,n) format (__##kind##__, m, n)
416 # define __format__(kind,m,n) __format__ (__##kind##__, m, n)
417 # define ____printf____ __printf__
418 # define ____scanf____ __scanf__
419 # define ____strftime____ __strftime__
420 # define ____strfmon____ __strfmon__
423 #if @GNULIB_PUTC@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
425 # define putc rpl_fputc
426 extern int putc (int c, FILE *stream) _GL_ARG_NONNULL ((2));
429 #if @GNULIB_PUTCHAR@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
431 # define putchar rpl_putchar
432 extern int putchar (int c);
435 #if @GNULIB_PUTS@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
437 # define puts rpl_puts
438 extern int puts (const char *string) _GL_ARG_NONNULL ((1));
442 # if @REPLACE_REMOVE@
444 # define remove rpl_remove
445 extern int remove (const char *name) _GL_ARG_NONNULL ((1));
447 #elif defined GNULIB_POSIXCHECK
450 (GL_LINK_WARNING ("remove cannot handle directories on some platforms - " \
451 "use gnulib module remove for more portability"), \
456 # if @REPLACE_RENAME@
458 # define rename rpl_rename
459 extern int rename (const char *old_filename, const char *new_filename)
460 _GL_ARG_NONNULL ((1, 2));
462 #elif defined GNULIB_POSIXCHECK
464 # define rename(o,n) \
465 (GL_LINK_WARNING ("rename is buggy on some platforms - " \
466 "use gnulib module rename for more portability"), \
470 #if @GNULIB_RENAMEAT@
471 # if @REPLACE_RENAMEAT@
473 # define renameat rpl_renameat
475 # if !@HAVE_RENAMEAT@ || @REPLACE_RENAMEAT@
476 extern int renameat (int fd1, char const *file1, int fd2, char const *file2)
477 _GL_ARG_NONNULL ((2, 4));
479 #elif defined GNULIB_POSIXCHECK
481 # define renameat(d1,f1,d2,f2) \
482 (GL_LINK_WARNING ("renameat is not portable - " \
483 "use gnulib module renameat for portability"), \
484 renameat (d1, f1, d2, f2))
487 #if @GNULIB_SNPRINTF@
488 # if @REPLACE_SNPRINTF@
489 # define snprintf rpl_snprintf
491 # if @REPLACE_SNPRINTF@ || !@HAVE_DECL_SNPRINTF@
492 extern int snprintf (char *str, size_t size, const char *format, ...)
493 __attribute__ ((__format__ (__printf__, 3, 4)))
494 _GL_ARG_NONNULL ((3));
496 #elif defined GNULIB_POSIXCHECK
499 (GL_LINK_WARNING ("snprintf is unportable - " \
500 "use gnulib module snprintf for portability"), \
504 #if @GNULIB_SPRINTF_POSIX@
505 # if @REPLACE_SPRINTF@
506 # define sprintf rpl_sprintf
507 extern int sprintf (char *str, const char *format, ...)
508 __attribute__ ((__format__ (__printf__, 2, 3)))
509 _GL_ARG_NONNULL ((1, 2));
511 #elif defined GNULIB_POSIXCHECK
514 (GL_LINK_WARNING ("sprintf is not always POSIX compliant - " \
515 "use gnulib module sprintf-posix for portable " \
516 "POSIX compliance"), \
520 #if @GNULIB_VASPRINTF@
521 # if @REPLACE_VASPRINTF@
522 # define asprintf rpl_asprintf
523 # define vasprintf rpl_vasprintf
525 # if @REPLACE_VASPRINTF@ || !@HAVE_VASPRINTF@
526 /* Write formatted output to a string dynamically allocated with malloc().
527 If the memory allocation succeeds, store the address of the string in
528 *RESULT and return the number of resulting bytes, excluding the trailing
529 NUL. Upon memory allocation error, or some other error, return -1. */
530 extern int asprintf (char **result, const char *format, ...)
531 __attribute__ ((__format__ (__printf__, 2, 3))) _GL_ARG_NONNULL ((1, 2));
532 extern int vasprintf (char **result, const char *format, va_list args)
533 __attribute__ ((__format__ (__printf__, 2, 0))) _GL_ARG_NONNULL ((1, 2));
537 #if @GNULIB_VDPRINTF@
538 # if @REPLACE_VDPRINTF@
539 # define vdprintf rpl_vdprintf
541 # if @REPLACE_VDPRINTF@ || !@HAVE_VDPRINTF@
542 extern int vdprintf (int fd, const char *format, va_list args)
543 __attribute__ ((__format__ (__printf__, 2, 0))) _GL_ARG_NONNULL ((2));
545 #elif defined GNULIB_POSIXCHECK
547 # define vdprintf(d,f,a) \
548 (GL_LINK_WARNING ("vdprintf is unportable - " \
549 "use gnulib module vdprintf for portability"), \
553 #if @GNULIB_VFPRINTF_POSIX@
554 # if @REPLACE_VFPRINTF@
555 # define vfprintf rpl_vfprintf
556 extern int vfprintf (FILE *fp, const char *format, va_list args)
557 __attribute__ ((__format__ (__printf__, 2, 0)))
558 _GL_ARG_NONNULL ((1, 2));
560 #elif @GNULIB_VFPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
561 # define vfprintf rpl_vfprintf
562 extern int vfprintf (FILE *fp, const char *format, va_list args)
563 __attribute__ ((__format__ (__printf__, 2, 0)))
564 _GL_ARG_NONNULL ((1, 2));
565 #elif defined GNULIB_POSIXCHECK
567 # define vfprintf(s,f,a) \
568 (GL_LINK_WARNING ("vfprintf is not always POSIX compliant - " \
569 "use gnulib module vfprintf-posix for portable " \
570 "POSIX compliance"), \
574 #if @GNULIB_VPRINTF_POSIX@
575 # if @REPLACE_VPRINTF@
576 # define vprintf rpl_vprintf
577 extern int vprintf (const char *format, va_list args)
578 __attribute__ ((__format__ (__printf__, 1, 0))) _GL_ARG_NONNULL ((1));
580 #elif @GNULIB_VPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
581 # define vprintf rpl_vprintf
582 extern int vprintf (const char *format, va_list args)
583 __attribute__ ((__format__ (__printf__, 1, 0))) _GL_ARG_NONNULL ((1));
584 #elif defined GNULIB_POSIXCHECK
586 # define vprintf(f,a) \
587 (GL_LINK_WARNING ("vprintf is not always POSIX compliant - " \
588 "use gnulib module vprintf-posix for portable " \
589 "POSIX compliance"), \
593 #if @GNULIB_VSNPRINTF@
594 # if @REPLACE_VSNPRINTF@
595 # define vsnprintf rpl_vsnprintf
597 # if @REPLACE_VSNPRINTF@ || !@HAVE_DECL_VSNPRINTF@
598 extern int vsnprintf (char *str, size_t size, const char *format, va_list args)
599 __attribute__ ((__format__ (__printf__, 3, 0)))
600 _GL_ARG_NONNULL ((3));
602 #elif defined GNULIB_POSIXCHECK
604 # define vsnprintf(b,s,f,a) \
605 (GL_LINK_WARNING ("vsnprintf is unportable - " \
606 "use gnulib module vsnprintf for portability"), \
607 vsnprintf (b, s, f, a))
610 #if @GNULIB_VSPRINTF_POSIX@
611 # if @REPLACE_VSPRINTF@
612 # define vsprintf rpl_vsprintf
613 extern int vsprintf (char *str, const char *format, va_list args)
614 __attribute__ ((__format__ (__printf__, 2, 0)))
615 _GL_ARG_NONNULL ((1, 2));
617 #elif defined GNULIB_POSIXCHECK
619 # define vsprintf(b,f,a) \
620 (GL_LINK_WARNING ("vsprintf is not always POSIX compliant - " \
621 "use gnulib module vsprintf-posix for portable " \
622 "POSIX compliance"), \
630 #endif /* _GL_STDIO_H */
631 #endif /* _GL_STDIO_H */