1 /* A GNU-like <stdio.h>.
3 Copyright (C) 2004, 2007-2009 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@ && @REPLACE_FSEEKO@) \
43 || (@GNULIB_FTELLO@ && @REPLACE_FTELLO@) \
44 || (@GNULIB_GETDELIM@ && !@HAVE_DECL_GETDELIM@) \
45 || (@GNULIB_GETLINE@ && (!@HAVE_DECL_GETLINE@ || @REPLACE_GETLINE@))
46 /* Get off_t and ssize_t. */
47 # include <sys/types.h>
51 /* This feature is available in gcc versions 2.5 and later. */
52 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
53 # define __attribute__(Spec) /* empty */
55 /* The __-protected variants of `format' and `printf' attributes
56 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
57 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
58 # define __format__ format
59 # define __printf__ printf
64 /* The definition of GL_LINK_WARNING is copied here. */
66 /* The definition of _GL_ARG_NONNULL is copied here. */
74 # if @REPLACE_DPRINTF@
75 # define dprintf rpl_dprintf
77 # if @REPLACE_DPRINTF@ || !@HAVE_DPRINTF@
78 extern int dprintf (int fd, const char *format, ...)
79 __attribute__ ((__format__ (__printf__, 2, 3))) _GL_ARG_NONNULL ((2));
81 #elif defined GNULIB_POSIXCHECK
83 # define dprintf(d,f,a) \
84 (GL_LINK_WARNING ("dprintf is unportable - " \
85 "use gnulib module dprintf for portability"), \
91 # define fclose rpl_fclose
92 /* Close STREAM and its underlying file descriptor. */
93 extern int fclose (FILE *stream) _GL_ARG_NONNULL ((1));
95 #elif defined GNULIB_POSIXCHECK
98 (GL_LINK_WARNING ("fclose is not always POSIX compliant - " \
99 "use gnulib module fclose for portable " \
100 "POSIX compliance"), \
105 # if @REPLACE_FFLUSH@
106 # define fflush rpl_fflush
107 /* Flush all pending data on STREAM according to POSIX rules. Both
108 output and seekable input streams are supported.
109 Note! LOSS OF DATA can occur if fflush is applied on an input stream
110 that is _not_seekable_ or on an update stream that is _not_seekable_
111 and in which the most recent operation was input. Seekability can
112 be tested with lseek(fileno(fp),0,SEEK_CUR). */
113 extern int fflush (FILE *gl_stream);
115 #elif defined GNULIB_POSIXCHECK
118 (GL_LINK_WARNING ("fflush is not always POSIX compliant - " \
119 "use gnulib module fflush for portable " \
120 "POSIX compliance"), \
127 # define fopen rpl_fopen
128 extern FILE * fopen (const char *filename, const char *mode)
129 _GL_ARG_NONNULL ((1, 2));
131 #elif defined GNULIB_POSIXCHECK
133 # define fopen(f,m) \
134 (GL_LINK_WARNING ("fopen on Win32 platforms is not POSIX compatible - " \
135 "use gnulib module fopen for portability"), \
139 #if @GNULIB_FPRINTF_POSIX@
140 # if @REPLACE_FPRINTF@
141 # define fprintf rpl_fprintf
142 extern int fprintf (FILE *fp, const char *format, ...)
143 __attribute__ ((__format__ (__printf__, 2, 3)))
144 _GL_ARG_NONNULL ((1, 2));
146 #elif @GNULIB_FPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
147 # define fprintf rpl_fprintf
148 extern int fprintf (FILE *fp, const char *format, ...)
149 __attribute__ ((__format__ (__printf__, 2, 3)))
150 _GL_ARG_NONNULL ((1, 2));
151 #elif defined GNULIB_POSIXCHECK
154 (GL_LINK_WARNING ("fprintf is not always POSIX compliant - " \
155 "use gnulib module fprintf-posix for portable " \
156 "POSIX compliance"), \
161 # if @REPLACE_FPURGE@
162 # define fpurge rpl_fpurge
164 # if @REPLACE_FPURGE@ || !@HAVE_DECL_FPURGE@
165 /* Discard all pending buffered I/O data on STREAM.
166 STREAM must not be wide-character oriented.
167 When discarding pending output, the file position is set back to where it
168 was before the write calls. When discarding pending input, the file
169 position is advanced to match the end of the previously read input.
170 Return 0 if successful. Upon error, return -1 and set errno. */
171 extern int fpurge (FILE *gl_stream) _GL_ARG_NONNULL ((1));
173 #elif defined GNULIB_POSIXCHECK
176 (GL_LINK_WARNING ("fpurge is not always present - " \
177 "use gnulib module fpurge for portability"), \
181 #if @GNULIB_FPUTC@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
183 # define fputc rpl_fputc
184 extern int fputc (int c, FILE *stream) _GL_ARG_NONNULL ((2));
187 #if @GNULIB_FPUTS@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
189 # define fputs rpl_fputs
190 extern int fputs (const char *string, FILE *stream) _GL_ARG_NONNULL ((1, 2));
194 # if @REPLACE_FREOPEN@
196 # define freopen rpl_freopen
197 extern FILE * freopen (const char *filename, const char *mode, FILE *stream)
198 _GL_ARG_NONNULL ((2, 3));
200 #elif defined GNULIB_POSIXCHECK
202 # define freopen(f,m,s) \
203 (GL_LINK_WARNING ("freopen on Win32 platforms is not POSIX compatible - " \
204 "use gnulib module freopen for portability"), \
208 #if @GNULIB_FSEEK@ && @REPLACE_FSEEK@
209 extern int rpl_fseek (FILE *fp, long offset, int whence) _GL_ARG_NONNULL ((1));
211 # if defined GNULIB_POSIXCHECK
212 # define fseek(f,o,w) \
213 (GL_LINK_WARNING ("fseek cannot handle files larger than 4 GB " \
214 "on 32-bit platforms - " \
215 "use fseeko function for handling of large files"), \
218 # define fseek rpl_fseek
220 #elif defined GNULIB_POSIXCHECK
222 # define fseek(f,o,w) \
223 (GL_LINK_WARNING ("fseek cannot handle files larger than 4 GB " \
224 "on 32-bit platforms - " \
225 "use fseeko function for handling of large files"), \
231 # if @REPLACE_FSEEKO@
232 /* Provide fseek, fseeko functions that are aware of a preceding
233 fflush(), and which detect pipes. */
234 # define fseeko rpl_fseeko
235 extern int fseeko (FILE *fp, off_t offset, int whence) _GL_ARG_NONNULL ((1));
238 # define fseek(f,o,w) \
239 (GL_LINK_WARNING ("fseek cannot handle files larger than 4 GB " \
240 "on 32-bit platforms - " \
241 "use fseeko function for handling of large files"), \
245 #elif defined GNULIB_POSIXCHECK
247 # define fseeko(f,o,w) \
248 (GL_LINK_WARNING ("fseeko is unportable - " \
249 "use gnulib module fseeko for portability"), \
253 #if @GNULIB_FTELL@ && @REPLACE_FTELL@
254 extern long rpl_ftell (FILE *fp) _GL_ARG_NONNULL ((1));
256 # if GNULIB_POSIXCHECK
258 (GL_LINK_WARNING ("ftell cannot handle files larger than 4 GB " \
259 "on 32-bit platforms - " \
260 "use ftello function for handling of large files"), \
263 # define ftell rpl_ftell
265 #elif defined GNULIB_POSIXCHECK
268 (GL_LINK_WARNING ("ftell cannot handle files larger than 4 GB " \
269 "on 32-bit platforms - " \
270 "use ftello function for handling of large files"), \
276 # if @REPLACE_FTELLO@
277 # define ftello rpl_ftello
278 extern off_t ftello (FILE *fp) _GL_ARG_NONNULL ((1));
282 (GL_LINK_WARNING ("ftell cannot handle files larger than 4 GB " \
283 "on 32-bit platforms - " \
284 "use ftello function for handling of large files"), \
288 #elif defined GNULIB_POSIXCHECK
291 (GL_LINK_WARNING ("ftello is unportable - " \
292 "use gnulib module ftello for portability"), \
296 #if @GNULIB_FWRITE@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
298 # define fwrite rpl_fwrite
299 extern size_t fwrite (const void *ptr, size_t s, size_t n, FILE *stream)
300 _GL_ARG_NONNULL ((1, 4));
303 #if @GNULIB_GETDELIM@
304 # if !@HAVE_DECL_GETDELIM@
305 /* Read input, up to (and including) the next occurrence of DELIMITER, from
306 STREAM, store it in *LINEPTR (and NUL-terminate it).
307 *LINEPTR is a pointer returned from malloc (or NULL), pointing to *LINESIZE
308 bytes of space. It is realloc'd as necessary.
309 Return the number of bytes read and stored at *LINEPTR (not including the
310 NUL terminator), or -1 on error or EOF. */
311 extern ssize_t getdelim (char **lineptr, size_t *linesize, int delimiter,
313 _GL_ARG_NONNULL ((1, 2, 4));
315 #elif defined GNULIB_POSIXCHECK
317 # define getdelim(l, s, d, f) \
318 (GL_LINK_WARNING ("getdelim is unportable - " \
319 "use gnulib module getdelim for portability"), \
320 getdelim (l, s, d, f))
324 # if @REPLACE_GETLINE@
326 # define getline rpl_getline
328 # if !@HAVE_DECL_GETLINE@ || @REPLACE_GETLINE@
329 /* Read a line, up to (and including) the next newline, from STREAM, store it
330 in *LINEPTR (and NUL-terminate it).
331 *LINEPTR is a pointer returned from malloc (or NULL), pointing to *LINESIZE
332 bytes of space. It is realloc'd as necessary.
333 Return the number of bytes read and stored at *LINEPTR (not including the
334 NUL terminator), or -1 on error or EOF. */
335 extern ssize_t getline (char **lineptr, size_t *linesize, FILE *stream)
336 _GL_ARG_NONNULL ((1, 2, 3));
338 #elif defined GNULIB_POSIXCHECK
340 # define getline(l, s, f) \
341 (GL_LINK_WARNING ("getline is unportable - " \
342 "use gnulib module getline for portability"), \
346 #if @GNULIB_OBSTACK_PRINTF@
347 # if @REPLACE_OBSTACK_PRINTF@
348 # define obstack_printf rpl_osbtack_printf
349 # define obstack_vprintf rpl_obstack_vprintf
351 # if @REPLACE_OBSTACK_PRINTF@ || !@HAVE_DECL_OBSTACK_PRINTF@
353 /* Grow an obstack with formatted output. Return the number of
354 bytes added to OBS. No trailing nul byte is added, and the
355 object should be closed with obstack_finish before use. Upon
356 memory allocation error, call obstack_alloc_failed_handler. Upon
357 other error, return -1. */
358 extern int obstack_printf (struct obstack *obs, const char *format, ...)
359 __attribute__ ((__format__ (__printf__, 2, 3))) _GL_ARG_NONNULL ((1, 2));
360 extern int obstack_vprintf (struct obstack *obs, const char *format,
362 __attribute__ ((__format__ (__printf__, 2, 0))) _GL_ARG_NONNULL ((1, 2));
367 # if @REPLACE_PERROR@
368 # define perror rpl_perror
369 /* Print a message to standard error, describing the value of ERRNO,
370 (if STRING is not NULL and not empty) prefixed with STRING and ": ",
371 and terminated with a newline. */
372 extern void perror (const char *string);
374 #elif defined GNULIB_POSIXCHECK
377 (GL_LINK_WARNING ("perror is not always POSIX compliant - " \
378 "use gnulib module perror for portability"), \
385 # define popen rpl_popen
386 extern FILE *popen (const char *cmd, const char *mode)
387 _GL_ARG_NONNULL ((1, 2));
389 #elif defined GNULIB_POSIXCHECK
391 # define popen(c,m) \
392 (GL_LINK_WARNING ("popen is buggy on some platforms - " \
393 "use gnulib module popen or pipe for more portability"), \
397 #if @GNULIB_PRINTF_POSIX@
398 # if @REPLACE_PRINTF@
399 /* Don't break __attribute__((format(printf,M,N))). */
400 # define printf __printf__
401 extern int printf (const char *format, ...)
402 __attribute__ ((__format__ (__printf__, 1, 2))) _GL_ARG_NONNULL ((1));
404 #elif @GNULIB_PRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
405 /* Don't break __attribute__((format(printf,M,N))). */
406 # define printf __printf__
407 extern int printf (const char *format, ...)
408 __attribute__ ((__format__ (__printf__, 1, 2))) _GL_ARG_NONNULL ((1));
409 #elif defined GNULIB_POSIXCHECK
412 (GL_LINK_WARNING ("printf is not always POSIX compliant - " \
413 "use gnulib module printf-posix for portable " \
414 "POSIX compliance"), \
416 /* Don't break __attribute__((format(printf,M,N))). */
417 # define format(kind,m,n) format (__##kind##__, m, n)
418 # define __format__(kind,m,n) __format__ (__##kind##__, m, n)
419 # define ____printf____ __printf__
420 # define ____scanf____ __scanf__
421 # define ____strftime____ __strftime__
422 # define ____strfmon____ __strfmon__
425 #if @GNULIB_PUTC@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
427 # define putc rpl_fputc
428 extern int putc (int c, FILE *stream) _GL_ARG_NONNULL ((2));
431 #if @GNULIB_PUTCHAR@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
433 # define putchar rpl_putchar
434 extern int putchar (int c);
437 #if @GNULIB_PUTS@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
439 # define puts rpl_puts
440 extern int puts (const char *string) _GL_ARG_NONNULL ((1));
444 # if @REPLACE_REMOVE@
446 # define remove rpl_remove
447 extern int remove (const char *name) _GL_ARG_NONNULL ((1));
449 #elif defined GNULIB_POSIXCHECK
452 (GL_LINK_WARNING ("remove cannot handle directories on some platforms - " \
453 "use gnulib module remove for more portability"), \
458 # if @REPLACE_RENAME@
460 # define rename rpl_rename
461 extern int rename (const char *old_filename, const char *new_filename)
462 _GL_ARG_NONNULL ((1, 2));
464 #elif defined GNULIB_POSIXCHECK
466 # define rename(o,n) \
467 (GL_LINK_WARNING ("rename is buggy on some platforms - " \
468 "use gnulib module rename for more portability"), \
472 #if @GNULIB_RENAMEAT@
473 # if @REPLACE_RENAMEAT@
475 # define renameat rpl_renameat
477 # if !@HAVE_RENAMEAT@ || @REPLACE_RENAMEAT@
478 extern int renameat (int fd1, char const *file1, int fd2, char const *file2)
479 _GL_ARG_NONNULL ((2, 4));
481 #elif defined GNULIB_POSIXCHECK
483 # define renameat(d1,f1,d2,f2) \
484 (GL_LINK_WARNING ("renameat is not portable - " \
485 "use gnulib module renameat for portability"), \
486 renameat (d1, f1, d2, f2))
489 #if @GNULIB_SNPRINTF@
490 # if @REPLACE_SNPRINTF@
491 # define snprintf rpl_snprintf
493 # if @REPLACE_SNPRINTF@ || !@HAVE_DECL_SNPRINTF@
494 extern int snprintf (char *str, size_t size, const char *format, ...)
495 __attribute__ ((__format__ (__printf__, 3, 4)))
496 _GL_ARG_NONNULL ((3));
498 #elif defined GNULIB_POSIXCHECK
501 (GL_LINK_WARNING ("snprintf is unportable - " \
502 "use gnulib module snprintf for portability"), \
506 #if @GNULIB_SPRINTF_POSIX@
507 # if @REPLACE_SPRINTF@
508 # define sprintf rpl_sprintf
509 extern int sprintf (char *str, const char *format, ...)
510 __attribute__ ((__format__ (__printf__, 2, 3)))
511 _GL_ARG_NONNULL ((1, 2));
513 #elif defined GNULIB_POSIXCHECK
516 (GL_LINK_WARNING ("sprintf is not always POSIX compliant - " \
517 "use gnulib module sprintf-posix for portable " \
518 "POSIX compliance"), \
522 #if @GNULIB_VASPRINTF@
523 # if @REPLACE_VASPRINTF@
524 # define asprintf rpl_asprintf
525 # define vasprintf rpl_vasprintf
527 # if @REPLACE_VASPRINTF@ || !@HAVE_VASPRINTF@
528 /* Write formatted output to a string dynamically allocated with malloc().
529 If the memory allocation succeeds, store the address of the string in
530 *RESULT and return the number of resulting bytes, excluding the trailing
531 NUL. Upon memory allocation error, or some other error, return -1. */
532 extern int asprintf (char **result, const char *format, ...)
533 __attribute__ ((__format__ (__printf__, 2, 3))) _GL_ARG_NONNULL ((1, 2));
534 extern int vasprintf (char **result, const char *format, va_list args)
535 __attribute__ ((__format__ (__printf__, 2, 0))) _GL_ARG_NONNULL ((1, 2));
539 #if @GNULIB_VDPRINTF@
540 # if @REPLACE_VDPRINTF@
541 # define vdprintf rpl_vdprintf
543 # if @REPLACE_VDPRINTF@ || !@HAVE_VDPRINTF@
544 extern int vdprintf (int fd, const char *format, va_list args)
545 __attribute__ ((__format__ (__printf__, 2, 0))) _GL_ARG_NONNULL ((2));
547 #elif defined GNULIB_POSIXCHECK
549 # define vdprintf(d,f,a) \
550 (GL_LINK_WARNING ("vdprintf is unportable - " \
551 "use gnulib module vdprintf for portability"), \
555 #if @GNULIB_VFPRINTF_POSIX@
556 # if @REPLACE_VFPRINTF@
557 # define vfprintf rpl_vfprintf
558 extern int vfprintf (FILE *fp, const char *format, va_list args)
559 __attribute__ ((__format__ (__printf__, 2, 0)))
560 _GL_ARG_NONNULL ((1, 2));
562 #elif @GNULIB_VFPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
563 # define vfprintf rpl_vfprintf
564 extern int vfprintf (FILE *fp, const char *format, va_list args)
565 __attribute__ ((__format__ (__printf__, 2, 0)))
566 _GL_ARG_NONNULL ((1, 2));
567 #elif defined GNULIB_POSIXCHECK
569 # define vfprintf(s,f,a) \
570 (GL_LINK_WARNING ("vfprintf is not always POSIX compliant - " \
571 "use gnulib module vfprintf-posix for portable " \
572 "POSIX compliance"), \
576 #if @GNULIB_VPRINTF_POSIX@
577 # if @REPLACE_VPRINTF@
578 # define vprintf rpl_vprintf
579 extern int vprintf (const char *format, va_list args)
580 __attribute__ ((__format__ (__printf__, 1, 0))) _GL_ARG_NONNULL ((1));
582 #elif @GNULIB_VPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
583 # define vprintf rpl_vprintf
584 extern int vprintf (const char *format, va_list args)
585 __attribute__ ((__format__ (__printf__, 1, 0))) _GL_ARG_NONNULL ((1));
586 #elif defined GNULIB_POSIXCHECK
588 # define vprintf(f,a) \
589 (GL_LINK_WARNING ("vprintf is not always POSIX compliant - " \
590 "use gnulib module vprintf-posix for portable " \
591 "POSIX compliance"), \
595 #if @GNULIB_VSNPRINTF@
596 # if @REPLACE_VSNPRINTF@
597 # define vsnprintf rpl_vsnprintf
599 # if @REPLACE_VSNPRINTF@ || !@HAVE_DECL_VSNPRINTF@
600 extern int vsnprintf (char *str, size_t size, const char *format, va_list args)
601 __attribute__ ((__format__ (__printf__, 3, 0)))
602 _GL_ARG_NONNULL ((3));
604 #elif defined GNULIB_POSIXCHECK
606 # define vsnprintf(b,s,f,a) \
607 (GL_LINK_WARNING ("vsnprintf is unportable - " \
608 "use gnulib module vsnprintf for portability"), \
609 vsnprintf (b, s, f, a))
612 #if @GNULIB_VSPRINTF_POSIX@
613 # if @REPLACE_VSPRINTF@
614 # define vsprintf rpl_vsprintf
615 extern int vsprintf (char *str, const char *format, va_list args)
616 __attribute__ ((__format__ (__printf__, 2, 0)))
617 _GL_ARG_NONNULL ((1, 2));
619 #elif defined GNULIB_POSIXCHECK
621 # define vsprintf(b,f,a) \
622 (GL_LINK_WARNING ("vsprintf is not always POSIX compliant - " \
623 "use gnulib module vsprintf-posix for portable " \
624 "POSIX compliance"), \
632 #endif /* _GL_STDIO_H */
633 #endif /* _GL_STDIO_H */