1/* A GNU-like <stdio.h>.
2
3   Copyright (C) 2004, 2007-2009 Free Software Foundation, Inc.
4
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 3, or (at your option)
8   any later version.
9
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.
14
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.  */
18
19#if __GNUC__ >= 3
20@PRAGMA_SYSTEM_HEADER@
21#endif
22
23#if defined __need_FILE || defined __need___FILE
24/* Special invocation convention inside glibc header files.  */
25
26#@INCLUDE_NEXT@ @NEXT_STDIO_H@
27
28#else
29/* Normal invocation convention.  */
30
31#ifndef _GL_STDIO_H
32
33/* The include_next requires a split double-inclusion guard.  */
34#@INCLUDE_NEXT@ @NEXT_STDIO_H@
35
36#ifndef _GL_STDIO_H
37#define _GL_STDIO_H
38
39#include <stdarg.h>
40#include <stddef.h>
41
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>
48#endif
49
50#ifndef __attribute__
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 */
54# endif
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
60# endif
61#endif
62
63
64/* The definition of GL_LINK_WARNING is copied here.  */
65
66
67#ifdef __cplusplus
68extern "C" {
69#endif
70
71
72#if @GNULIB_FPRINTF_POSIX@
73# if @REPLACE_FPRINTF@
74#  define fprintf rpl_fprintf
75extern int fprintf (FILE *fp, const char *format, ...)
76       __attribute__ ((__format__ (__printf__, 2, 3)));
77# endif
78#elif @GNULIB_FPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
79# define fprintf rpl_fprintf
80extern int fprintf (FILE *fp, const char *format, ...)
81       __attribute__ ((__format__ (__printf__, 2, 3)));
82#elif defined GNULIB_POSIXCHECK
83# undef fprintf
84# define fprintf \
85    (GL_LINK_WARNING ("fprintf is not always POSIX compliant - " \
86                      "use gnulib module fprintf-posix for portable " \
87                      "POSIX compliance"), \
88     fprintf)
89#endif
90
91#if @GNULIB_VFPRINTF_POSIX@
92# if @REPLACE_VFPRINTF@
93#  define vfprintf rpl_vfprintf
94extern int vfprintf (FILE *fp, const char *format, va_list args)
95       __attribute__ ((__format__ (__printf__, 2, 0)));
96# endif
97#elif @GNULIB_VFPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
98# define vfprintf rpl_vfprintf
99extern int vfprintf (FILE *fp, const char *format, va_list args)
100       __attribute__ ((__format__ (__printf__, 2, 0)));
101#elif defined GNULIB_POSIXCHECK
102# undef vfprintf
103# define vfprintf(s,f,a) \
104    (GL_LINK_WARNING ("vfprintf is not always POSIX compliant - " \
105                      "use gnulib module vfprintf-posix for portable " \
106                      "POSIX compliance"), \
107     vfprintf (s, f, a))
108#endif
109
110#if @GNULIB_PRINTF_POSIX@
111# if @REPLACE_PRINTF@
112/* Don't break __attribute__((format(printf,M,N))).  */
113#  define printf __printf__
114extern int printf (const char *format, ...)
115       __attribute__ ((__format__ (__printf__, 1, 2)));
116# endif
117#elif @GNULIB_PRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
118/* Don't break __attribute__((format(printf,M,N))).  */
119# define printf __printf__
120extern int printf (const char *format, ...)
121       __attribute__ ((__format__ (__printf__, 1, 2)));
122#elif defined GNULIB_POSIXCHECK
123# undef printf
124# define printf \
125    (GL_LINK_WARNING ("printf is not always POSIX compliant - " \
126                      "use gnulib module printf-posix for portable " \
127                      "POSIX compliance"), \
128     printf)
129/* Don't break __attribute__((format(printf,M,N))).  */
130# define format(kind,m,n) format (__##kind##__, m, n)
131# define __format__(kind,m,n) __format__ (__##kind##__, m, n)
132# define ____printf____ __printf__
133# define ____scanf____ __scanf__
134# define ____strftime____ __strftime__
135# define ____strfmon____ __strfmon__
136#endif
137
138#if @GNULIB_VPRINTF_POSIX@
139# if @REPLACE_VPRINTF@
140#  define vprintf rpl_vprintf
141extern int vprintf (const char *format, va_list args)
142       __attribute__ ((__format__ (__printf__, 1, 0)));
143# endif
144#elif @GNULIB_VPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
145# define vprintf rpl_vprintf
146extern int vprintf (const char *format, va_list args)
147       __attribute__ ((__format__ (__printf__, 1, 0)));
148#elif defined GNULIB_POSIXCHECK
149# undef vprintf
150# define vprintf(f,a) \
151    (GL_LINK_WARNING ("vprintf is not always POSIX compliant - " \
152                      "use gnulib module vprintf-posix for portable " \
153                      "POSIX compliance"), \
154     vprintf (f, a))
155#endif
156
157#if @GNULIB_SNPRINTF@
158# if @REPLACE_SNPRINTF@
159#  define snprintf rpl_snprintf
160# endif
161# if @REPLACE_SNPRINTF@ || !@HAVE_DECL_SNPRINTF@
162extern int snprintf (char *str, size_t size, const char *format, ...)
163       __attribute__ ((__format__ (__printf__, 3, 4)));
164# endif
165#elif defined GNULIB_POSIXCHECK
166# undef snprintf
167# define snprintf \
168    (GL_LINK_WARNING ("snprintf is unportable - " \
169                      "use gnulib module snprintf for portability"), \
170     snprintf)
171#endif
172
173#if @GNULIB_VSNPRINTF@
174# if @REPLACE_VSNPRINTF@
175#  define vsnprintf rpl_vsnprintf
176# endif
177# if @REPLACE_VSNPRINTF@ || !@HAVE_DECL_VSNPRINTF@
178extern int vsnprintf (char *str, size_t size, const char *format, va_list args)
179       __attribute__ ((__format__ (__printf__, 3, 0)));
180# endif
181#elif defined GNULIB_POSIXCHECK
182# undef vsnprintf
183# define vsnprintf(b,s,f,a) \
184    (GL_LINK_WARNING ("vsnprintf is unportable - " \
185                      "use gnulib module vsnprintf for portability"), \
186     vsnprintf (b, s, f, a))
187#endif
188
189#if @GNULIB_SPRINTF_POSIX@
190# if @REPLACE_SPRINTF@
191#  define sprintf rpl_sprintf
192extern int sprintf (char *str, const char *format, ...)
193       __attribute__ ((__format__ (__printf__, 2, 3)));
194# endif
195#elif defined GNULIB_POSIXCHECK
196# undef sprintf
197# define sprintf \
198    (GL_LINK_WARNING ("sprintf is not always POSIX compliant - " \
199                      "use gnulib module sprintf-posix for portable " \
200                      "POSIX compliance"), \
201     sprintf)
202#endif
203
204#if @GNULIB_VSPRINTF_POSIX@
205# if @REPLACE_VSPRINTF@
206#  define vsprintf rpl_vsprintf
207extern int vsprintf (char *str, const char *format, va_list args)
208       __attribute__ ((__format__ (__printf__, 2, 0)));
209# endif
210#elif defined GNULIB_POSIXCHECK
211# undef vsprintf
212# define vsprintf(b,f,a) \
213    (GL_LINK_WARNING ("vsprintf is not always POSIX compliant - " \
214                      "use gnulib module vsprintf-posix for portable " \
215                      "POSIX compliance"), \
216     vsprintf (b, f, a))
217#endif
218
219#if @GNULIB_DPRINTF@
220# if @REPLACE_DPRINTF@
221#  define dprintf rpl_dprintf
222# endif
223# if @REPLACE_DPRINTF@ || !@HAVE_DPRINTF@
224extern int dprintf (int fd, const char *format, ...)
225       __attribute__ ((__format__ (__printf__, 2, 3)));
226# endif
227#elif defined GNULIB_POSIXCHECK
228# undef dprintf
229# define dprintf(d,f,a) \
230    (GL_LINK_WARNING ("dprintf is unportable - " \
231                      "use gnulib module dprintf for portability"), \
232     dprintf (d, f, a))
233#endif
234
235#if @GNULIB_VDPRINTF@
236# if @REPLACE_VDPRINTF@
237#  define vdprintf rpl_vdprintf
238# endif
239# if @REPLACE_VDPRINTF@ || !@HAVE_VDPRINTF@
240extern int vdprintf (int fd, const char *format, va_list args)
241       __attribute__ ((__format__ (__printf__, 2, 0)));
242# endif
243#elif defined GNULIB_POSIXCHECK
244# undef vdprintf
245# define vdprintf(d,f,a) \
246    (GL_LINK_WARNING ("vdprintf is unportable - " \
247                      "use gnulib module vdprintf for portability"), \
248     vdprintf (d, f, a))
249#endif
250
251#if @GNULIB_VASPRINTF@
252# if @REPLACE_VASPRINTF@
253#  define asprintf rpl_asprintf
254#  define vasprintf rpl_vasprintf
255# endif
256# if @REPLACE_VASPRINTF@ || !@HAVE_VASPRINTF@
257  /* Write formatted output to a string dynamically allocated with malloc().
258     If the memory allocation succeeds, store the address of the string in
259     *RESULT and return the number of resulting bytes, excluding the trailing
260     NUL.  Upon memory allocation error, or some other error, return -1.  */
261  extern int asprintf (char **result, const char *format, ...)
262    __attribute__ ((__format__ (__printf__, 2, 3)));
263  extern int vasprintf (char **result, const char *format, va_list args)
264    __attribute__ ((__format__ (__printf__, 2, 0)));
265# endif
266#endif
267
268#if @GNULIB_OBSTACK_PRINTF@
269# if @REPLACE_OBSTACK_PRINTF@
270#  define obstack_printf rpl_osbtack_printf
271#  define obstack_vprintf rpl_obstack_vprintf
272# endif
273# if @REPLACE_OBSTACK_PRINTF@ || !@HAVE_DECL_OBSTACK_PRINTF@
274  struct obstack;
275  /* Grow an obstack with formatted output.  Return the number of
276     bytes added to OBS.  No trailing nul byte is added, and the
277     object should be closed with obstack_finish before use.  Upon
278     memory allocation error, call obstack_alloc_failed_handler.  Upon
279     other error, return -1.  */
280  extern int obstack_printf (struct obstack *obs, const char *format, ...)
281    __attribute__ ((__format__ (__printf__, 2, 3)));
282  extern int obstack_vprintf (struct obstack *obs, const char *format,
283			      va_list args)
284    __attribute__ ((__format__ (__printf__, 2, 0)));
285# endif
286#endif
287
288#if @GNULIB_FOPEN@
289# if @REPLACE_FOPEN@
290#  undef fopen
291#  define fopen rpl_fopen
292extern FILE * fopen (const char *filename, const char *mode);
293# endif
294#elif defined GNULIB_POSIXCHECK
295# undef fopen
296# define fopen(f,m) \
297   (GL_LINK_WARNING ("fopen on Win32 platforms is not POSIX compatible - " \
298                     "use gnulib module fopen for portability"), \
299    fopen (f, m))
300#endif
301
302#if @GNULIB_FREOPEN@
303# if @REPLACE_FREOPEN@
304#  undef freopen
305#  define freopen rpl_freopen
306extern FILE * freopen (const char *filename, const char *mode, FILE *stream);
307# endif
308#elif defined GNULIB_POSIXCHECK
309# undef freopen
310# define freopen(f,m,s) \
311   (GL_LINK_WARNING ("freopen on Win32 platforms is not POSIX compatible - " \
312                     "use gnulib module freopen for portability"), \
313    freopen (f, m, s))
314#endif
315
316#if @GNULIB_FSEEKO@
317# if @REPLACE_FSEEKO@
318/* Provide fseek, fseeko functions that are aware of a preceding
319   fflush(), and which detect pipes.  */
320#  define fseeko rpl_fseeko
321extern int fseeko (FILE *fp, off_t offset, int whence);
322#  define fseek(fp, offset, whence) fseeko (fp, (off_t)(offset), whence)
323# endif
324#elif defined GNULIB_POSIXCHECK
325# undef fseeko
326# define fseeko(f,o,w) \
327   (GL_LINK_WARNING ("fseeko is unportable - " \
328                     "use gnulib module fseeko for portability"), \
329    fseeko (f, o, w))
330#endif
331
332#if @GNULIB_FSEEK@ && @REPLACE_FSEEK@
333extern int rpl_fseek (FILE *fp, long offset, int whence);
334# undef fseek
335# if defined GNULIB_POSIXCHECK
336#  define fseek(f,o,w) \
337     (GL_LINK_WARNING ("fseek cannot handle files larger than 4 GB " \
338                       "on 32-bit platforms - " \
339                       "use fseeko function for handling of large files"), \
340      rpl_fseek (f, o, w))
341# else
342#  define fseek rpl_fseek
343# endif
344#elif defined GNULIB_POSIXCHECK
345# ifndef fseek
346#  define fseek(f,o,w) \
347     (GL_LINK_WARNING ("fseek cannot handle files larger than 4 GB " \
348                       "on 32-bit platforms - " \
349                       "use fseeko function for handling of large files"), \
350      fseek (f, o, w))
351# endif
352#endif
353
354#if @GNULIB_FTELLO@
355# if @REPLACE_FTELLO@
356#  define ftello rpl_ftello
357extern off_t ftello (FILE *fp);
358#  define ftell(fp) ftello (fp)
359# endif
360#elif defined GNULIB_POSIXCHECK
361# undef ftello
362# define ftello(f) \
363   (GL_LINK_WARNING ("ftello is unportable - " \
364                     "use gnulib module ftello for portability"), \
365    ftello (f))
366#endif
367
368#if @GNULIB_FTELL@ && @REPLACE_FTELL@
369extern long rpl_ftell (FILE *fp);
370# undef ftell
371# if GNULIB_POSIXCHECK
372#  define ftell(f) \
373     (GL_LINK_WARNING ("ftell cannot handle files larger than 4 GB " \
374                       "on 32-bit platforms - " \
375                       "use ftello function for handling of large files"), \
376      rpl_ftell (f))
377# else
378#  define ftell rpl_ftell
379# endif
380#elif defined GNULIB_POSIXCHECK
381# ifndef ftell
382#  define ftell(f) \
383     (GL_LINK_WARNING ("ftell cannot handle files larger than 4 GB " \
384                       "on 32-bit platforms - " \
385                       "use ftello function for handling of large files"), \
386      ftell (f))
387# endif
388#endif
389
390#if @GNULIB_FFLUSH@
391# if @REPLACE_FFLUSH@
392#  define fflush rpl_fflush
393  /* Flush all pending data on STREAM according to POSIX rules.  Both
394     output and seekable input streams are supported.
395     Note! LOSS OF DATA can occur if fflush is applied on an input stream
396     that is _not_seekable_ or on an update stream that is _not_seekable_
397     and in which the most recent operation was input.  Seekability can
398     be tested with lseek(fileno(fp),0,SEEK_CUR).  */
399  extern int fflush (FILE *gl_stream);
400# endif
401#elif defined GNULIB_POSIXCHECK
402# undef fflush
403# define fflush(f) \
404   (GL_LINK_WARNING ("fflush is not always POSIX compliant - " \
405                     "use gnulib module fflush for portable " \
406                     "POSIX compliance"), \
407    fflush (f))
408#endif
409
410#if @GNULIB_FPURGE@
411# if @REPLACE_FPURGE@
412#  define fpurge rpl_fpurge
413# endif
414# if @REPLACE_FPURGE@ || !@HAVE_DECL_FPURGE@
415  /* Discard all pending buffered I/O data on STREAM.
416     STREAM must not be wide-character oriented.
417     When discarding pending output, the file position is set back to where it
418     was before the write calls.  When discarding pending input, the file
419     position is advanced to match the end of the previously read input.
420     Return 0 if successful.  Upon error, return -1 and set errno.  */
421  extern int fpurge (FILE *gl_stream);
422# endif
423#elif defined GNULIB_POSIXCHECK
424# undef fpurge
425# define fpurge(f) \
426   (GL_LINK_WARNING ("fpurge is not always present - " \
427                     "use gnulib module fpurge for portability"), \
428    fpurge (f))
429#endif
430
431#if @GNULIB_FCLOSE@
432# if @REPLACE_FCLOSE@
433#  define fclose rpl_fclose
434  /* Close STREAM and its underlying file descriptor.  */
435extern int fclose (FILE *stream);
436# endif
437#elif defined GNULIB_POSIXCHECK
438# undef fclose
439# define fclose(f) \
440   (GL_LINK_WARNING ("fclose is not always POSIX compliant - " \
441                     "use gnulib module fclose for portable " \
442                     "POSIX compliance"), \
443    fclose (f))
444#endif
445
446#if @GNULIB_FPUTC@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
447# undef fputc
448# define fputc rpl_fputc
449extern int fputc (int c, FILE *stream);
450#endif
451
452#if @GNULIB_PUTC@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
453# undef putc
454# define putc rpl_fputc
455extern int putc (int c, FILE *stream);
456#endif
457
458#if @GNULIB_PUTCHAR@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
459# undef putchar
460# define putchar rpl_putchar
461extern int putchar (int c);
462#endif
463
464#if @GNULIB_FPUTS@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
465# undef fputs
466# define fputs rpl_fputs
467extern int fputs (const char *string, FILE *stream);
468#endif
469
470#if @GNULIB_PUTS@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
471# undef puts
472# define puts rpl_puts
473extern int puts (const char *string);
474#endif
475
476#if @GNULIB_FWRITE@ && @REPLACE_STDIO_WRITE_FUNCS@ && @GNULIB_STDIO_H_SIGPIPE@
477# undef fwrite
478# define fwrite rpl_fwrite
479extern size_t fwrite (const void *ptr, size_t s, size_t n, FILE *stream);
480#endif
481
482#if @GNULIB_POPEN@
483# if @REPLACE_POPEN@
484#  undef popen
485#  define popen rpl_popen
486extern FILE *popen (const char *cmd, const char *mode);
487# endif
488#elif defined GNULIB_POSIXCHECK
489# undef popen
490# define popen(c,m) \
491   (GL_LINK_WARNING ("popen is buggy on some platforms - " \
492                     "use gnulib module popen or pipe for more portability"), \
493    popen (c, m))
494#endif
495
496#if @GNULIB_GETDELIM@
497# if !@HAVE_DECL_GETDELIM@
498/* Read input, up to (and including) the next occurrence of DELIMITER, from
499   STREAM, store it in *LINEPTR (and NUL-terminate it).
500   *LINEPTR is a pointer returned from malloc (or NULL), pointing to *LINESIZE
501   bytes of space.  It is realloc'd as necessary.
502   Return the number of bytes read and stored at *LINEPTR (not including the
503   NUL terminator), or -1 on error or EOF.  */
504extern ssize_t getdelim (char **lineptr, size_t *linesize, int delimiter,
505			 FILE *stream);
506# endif
507#elif defined GNULIB_POSIXCHECK
508# undef getdelim
509# define getdelim(l, s, d, f)					    \
510  (GL_LINK_WARNING ("getdelim is unportable - "			    \
511		    "use gnulib module getdelim for portability"),  \
512   getdelim (l, s, d, f))
513#endif
514
515#if @GNULIB_GETLINE@
516# if @REPLACE_GETLINE@
517#  undef getline
518#  define getline rpl_getline
519# endif
520# if !@HAVE_DECL_GETLINE@ || @REPLACE_GETLINE@
521/* Read a line, up to (and including) the next newline, from STREAM, store it
522   in *LINEPTR (and NUL-terminate it).
523   *LINEPTR is a pointer returned from malloc (or NULL), pointing to *LINESIZE
524   bytes of space.  It is realloc'd as necessary.
525   Return the number of bytes read and stored at *LINEPTR (not including the
526   NUL terminator), or -1 on error or EOF.  */
527extern ssize_t getline (char **lineptr, size_t *linesize, FILE *stream);
528# endif
529#elif defined GNULIB_POSIXCHECK
530# undef getline
531# define getline(l, s, f)						\
532  (GL_LINK_WARNING ("getline is unportable - "				\
533		    "use gnulib module getline for portability"),	\
534   getline (l, s, f))
535#endif
536
537#if @GNULIB_PERROR@
538# if @REPLACE_PERROR@
539#  define perror rpl_perror
540/* Print a message to standard error, describing the value of ERRNO,
541   (if STRING is not NULL and not empty) prefixed with STRING and ": ",
542   and terminated with a newline.  */
543extern void perror (const char *string);
544# endif
545#elif defined GNULIB_POSIXCHECK
546# undef perror
547# define perror(s) \
548    (GL_LINK_WARNING ("perror is not always POSIX compliant - " \
549                      "use gnulib module perror for portability"), \
550     perror (s))
551#endif
552
553#ifdef __cplusplus
554}
555#endif
556
557#endif /* _GL_STDIO_H */
558#endif /* _GL_STDIO_H */
559#endif
560