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