1 /* Define ISO C stdio on top of C++ iostreams.
2 Copyright (C) 1991,1994-1999,2000,01,02 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * ISO C99 Standard: 7.19 Input/output <stdio.h>
28 #if !defined __need_FILE && !defined __need___FILE
30 # include <features.h>
34 # define __need_size_t
38 # include <bits/types.h>
40 # define __need___FILE
41 #endif /* Don't need FILE. */
44 #if !defined __FILE_defined && defined __need_FILE
47 /* The opaque type of streams. This is the definition used elsewhere. */
48 typedef struct _IO_FILE
FILE;
50 #if defined __USE_LARGEFILE64 || defined __USE_SVID || defined __USE_POSIX \
51 || defined __USE_BSD || defined __USE_ISOC99 || defined __USE_XOPEN \
52 || defined __USE_POSIX2
53 __USING_NAMESPACE_STD(FILE)
56 # define __FILE_defined 1
57 #endif /* FILE not defined. */
61 #if !defined ____FILE_defined && defined __need___FILE
63 /* The opaque type of streams. This is the definition used elsewhere. */
64 typedef struct _IO_FILE __FILE
;
66 # define ____FILE_defined 1
67 #endif /* __FILE not defined. */
73 * [zooey]: operations on cin/cout/cerr and stdin/stdout/stderr should support
74 * being mixed on a character by character basis:
75 * printf("this is "); cout << "a string" << endl;
76 * should yield "this is a string" on a single line.
77 * This never worked on BeOS, as the supposedly unique stream-symbols
78 * existed in libroot as well as in libstdc++.
79 * Since we do no longer have this problem, activating
80 * _STDIO_USES_IOSTREAM does no harm.
82 #define _STDIO_USES_IOSTREAM
88 # ifndef _VA_LIST_DEFINED
89 typedef _G_va_list
va_list;
90 # define _VA_LIST_DEFINED
97 /* The type of the second argument to `fgetpos' and `fsetpos'. */
99 #ifndef __USE_FILE_OFFSET64
100 typedef _G_fpos_t
fpos_t;
102 typedef _G_fpos64_t
fpos_t;
105 #ifdef __USE_LARGEFILE64
106 typedef _G_fpos64_t fpos64_t
;
109 /* The possibilities for the third argument to `setvbuf'. */
110 #define _IOFBF 0 /* Fully buffered. */
111 #define _IOLBF 1 /* Line buffered. */
112 #define _IONBF 2 /* No buffering. */
115 /* Default buffer size. */
117 # define BUFSIZ _IO_BUFSIZ
121 /* End of file character.
122 Some things throughout the library rely on this being -1. */
128 /* The possibilities for the third argument to `fseek'.
129 These values should not be changed. */
130 #define SEEK_SET 0 /* Seek from beginning of file. */
131 #define SEEK_CUR 1 /* Seek from current position. */
132 #define SEEK_END 2 /* Seek from end of file. */
135 #if defined __USE_SVID || defined __USE_XOPEN
136 /* Default path prefix for `tempnam' and `tmpnam'. */
137 # define P_tmpdir "/tmp"
142 L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
143 TMP_MAX The minimum number of unique filenames generated by tmpnam
144 (and tempnam when it uses tmpnam's name space),
145 or tempnam (the two are separate).
146 L_ctermid How long an array to pass to `ctermid'.
147 L_cuserid How long an array to pass to `cuserid'.
148 FOPEN_MAX Minimum number of files that can be open at once.
149 FILENAME_MAX Maximum length of a filename. */
150 #include <bits/stdio_lim.h>
153 /* Standard streams. */
154 extern struct _IO_FILE
*stdin
; /* Standard input stream. */
155 extern struct _IO_FILE
*stdout
; /* Standard output stream. */
156 extern struct _IO_FILE
*stderr
; /* Standard error output stream. */
157 /* C89/C99 say they're macros. Make them happy. */
159 #define stdout stdout
160 #define stderr stderr
162 __BEGIN_NAMESPACE_STD
163 /* Remove file FILENAME. */
164 extern int remove (__const
char *__filename
) __THROW
;
165 /* Rename file OLD to NEW. */
166 extern int rename (__const
char *__old
, __const
char *__new
) __THROW
;
170 __BEGIN_NAMESPACE_STD
171 /* Create a temporary file and open it read/write. */
172 #ifndef __USE_FILE_OFFSET64
173 extern FILE *tmpfile (void) __THROW
;
176 extern FILE *__REDIRECT (tmpfile
, (void) __THROW
, tmpfile64
);
178 # define tmpfile tmpfile64
182 /* Generate a temporary filename. */
183 extern char *tmpnam (char *__s
) __THROW
;
186 #ifdef __USE_LARGEFILE64
187 extern FILE *tmpfile64 (void) __THROW
;
191 /* This is the reentrant variant of `tmpnam'. The only difference is
192 that it does not allow S to be NULL. */
193 extern char *tmpnam_r (char *__s
) __THROW
;
197 #if defined __USE_SVID || defined __USE_XOPEN
198 /* Generate a unique temporary filename using up to five characters of PFX
199 if it is not NULL. The directory to put this file in is searched for
200 as follows: First the environment variable "TMPDIR" is checked.
201 If it contains the name of a writable directory, that directory is used.
202 If not and if DIR is not NULL, that value is checked. If that fails,
203 P_tmpdir is tried and finally "/tmp". The storage for the filename
204 is allocated by `malloc'. */
205 extern char *tempnam (__const
char *__dir
, __const
char *__pfx
)
206 __THROW __attribute_malloc__
;
210 __BEGIN_NAMESPACE_STD
212 extern int fclose (FILE *__stream
) __THROW
;
213 /* Flush STREAM, or all streams if STREAM is NULL. */
214 extern int fflush (FILE *__stream
) __THROW
;
218 /* Faster versions when locking is not required. */
219 extern int fflush_unlocked (FILE *__stream
) __THROW
;
223 /* Close all streams. */
224 extern int fcloseall (void) __THROW
;
228 __BEGIN_NAMESPACE_STD
229 #ifndef __USE_FILE_OFFSET64
230 /* Open a file and create a new stream for it. */
231 extern FILE *fopen (__const
char *__restrict __filename
,
232 __const
char *__restrict __modes
) __THROW
;
233 /* Open a file, replacing an existing stream with it. */
234 extern FILE *freopen (__const
char *__restrict __filename
,
235 __const
char *__restrict __modes
,
236 FILE *__restrict __stream
) __THROW
;
239 extern FILE *__REDIRECT (fopen
, (__const
char *__restrict __filename
,
240 __const
char *__restrict __modes
) __THROW
,
242 extern FILE *__REDIRECT (freopen
, (__const
char *__restrict __filename
,
243 __const
char *__restrict __modes
,
244 FILE *__restrict __stream
) __THROW
,
247 # define fopen fopen64
248 # define freopen freopen64
252 #ifdef __USE_LARGEFILE64
253 extern FILE *fopen64 (__const
char *__restrict __filename
,
254 __const
char *__restrict __modes
) __THROW
;
255 extern FILE *freopen64 (__const
char *__restrict __filename
,
256 __const
char *__restrict __modes
,
257 FILE *__restrict __stream
) __THROW
;
261 /* Create a new stream that refers to an existing system file descriptor. */
262 extern FILE *fdopen (int __fd
, __const
char *__modes
) __THROW
;
266 /* Create a new stream that refers to the given magic cookie,
267 and uses the given functions for input and output. */
268 extern FILE *fopencookie (void *__restrict __magic_cookie
,
269 __const
char *__restrict __modes
,
270 _IO_cookie_io_functions_t __io_funcs
) __THROW
;
272 /* Create a new stream that refers to a memory buffer. */
273 extern FILE *fmemopen (void *__s
, size_t __len
, __const
char *__modes
) __THROW
;
275 /* Open a stream that writes into a malloc'd buffer that is expanded as
276 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
277 and the number of characters written on fflush or fclose. */
278 extern FILE *open_memstream (char **__restrict __bufloc
,
279 size_t *__restrict __sizeloc
) __THROW
;
283 __BEGIN_NAMESPACE_STD
284 /* If BUF is NULL, make STREAM unbuffered.
285 Else make it use buffer BUF, of size BUFSIZ. */
286 extern void setbuf (FILE *__restrict __stream
, char *__restrict __buf
) __THROW
;
287 /* Make STREAM use buffering mode MODE.
288 If BUF is not NULL, use N bytes of it for buffering;
289 else allocate an internal buffer N bytes long. */
290 extern int setvbuf (FILE *__restrict __stream
, char *__restrict __buf
,
291 int __modes
, size_t __n
) __THROW
;
295 /* If BUF is NULL, make STREAM unbuffered.
296 Else make it use SIZE bytes of BUF for buffering. */
297 extern void setbuffer (FILE *__restrict __stream
, char *__restrict __buf
,
298 size_t __size
) __THROW
;
300 /* Make STREAM line-buffered. */
301 extern void setlinebuf (FILE *__stream
) __THROW
;
305 __BEGIN_NAMESPACE_STD
306 /* Write formatted output to STREAM. */
307 extern int fprintf (FILE *__restrict __stream
,
308 __const
char *__restrict __format
, ...) __THROW
;
309 /* Write formatted output to stdout. */
310 extern int printf (__const
char *__restrict __format
, ...) __THROW
;
311 /* Write formatted output to S. */
312 extern int sprintf (char *__restrict __s
,
313 __const
char *__restrict __format
, ...) __THROW
;
315 /* Write formatted output to S from argument list ARG. */
316 extern int vfprintf (FILE *__restrict __s
, __const
char *__restrict __format
,
317 _G_va_list __arg
) __THROW
;
318 /* Write formatted output to stdout from argument list ARG. */
319 extern int vprintf (__const
char *__restrict __format
, _G_va_list __arg
)
321 /* Write formatted output to S from argument list ARG. */
322 extern int vsprintf (char *__restrict __s
, __const
char *__restrict __format
,
323 _G_va_list __arg
) __THROW
;
326 #if defined __USE_BSD || defined __USE_ISOC99 || defined __USE_UNIX98
327 __BEGIN_NAMESPACE_C99
328 /* Maximum chars of output to write in MAXLEN. */
329 extern int snprintf (char *__restrict __s
, size_t __maxlen
,
330 __const
char *__restrict __format
, ...)
331 __THROW
__attribute__ ((__format__ (__printf__
, 3, 4)));
333 extern int vsnprintf (char *__restrict __s
, size_t __maxlen
,
334 __const
char *__restrict __format
, _G_va_list __arg
)
335 __THROW
__attribute__ ((__format__ (__printf__
, 3, 0)));
340 /* Write formatted output to a string dynamically allocated with `malloc'.
341 Store the address of the string in *PTR. */
342 extern int vasprintf (char **__restrict __ptr
, __const
char *__restrict __f
,
344 __THROW
__attribute__ ((__format__ (__printf__
, 2, 0)));
345 extern int __asprintf (char **__restrict __ptr
,
346 __const
char *__restrict __fmt
, ...)
347 __THROW
__attribute__ ((__format__ (__printf__
, 2, 3)));
348 extern int asprintf (char **__restrict __ptr
,
349 __const
char *__restrict __fmt
, ...)
350 __THROW
__attribute__ ((__format__ (__printf__
, 2, 3)));
352 /* Write formatted output to a file descriptor. */
353 extern int vdprintf (int __fd
, __const
char *__restrict __fmt
,
355 __THROW
__attribute__ ((__format__ (__printf__
, 2, 0)));
356 extern int dprintf (int __fd
, __const
char *__restrict __fmt
, ...)
357 __THROW
__attribute__ ((__format__ (__printf__
, 2, 3)));
361 __BEGIN_NAMESPACE_STD
362 /* Read formatted input from STREAM. */
363 extern int fscanf (FILE *__restrict __stream
,
364 __const
char *__restrict __format
, ...) __THROW
;
365 /* Read formatted input from stdin. */
366 extern int scanf (__const
char *__restrict __format
, ...) __THROW
;
367 /* Read formatted input from S. */
368 extern int sscanf (__const
char *__restrict __s
,
369 __const
char *__restrict __format
, ...) __THROW
;
373 __BEGIN_NAMESPACE_C99
374 /* Read formatted input from S into argument list ARG. */
375 extern int vfscanf (FILE *__restrict __s
, __const
char *__restrict __format
,
377 __THROW
__attribute__ ((__format__ (__scanf__
, 2, 0)));
379 /* Read formatted input from stdin into argument list ARG. */
380 extern int vscanf (__const
char *__restrict __format
, _G_va_list __arg
)
381 __THROW
__attribute__ ((__format__ (__scanf__
, 1, 0)));
383 /* Read formatted input from S into argument list ARG. */
384 extern int vsscanf (__const
char *__restrict __s
,
385 __const
char *__restrict __format
, _G_va_list __arg
)
386 __THROW
__attribute__ ((__format__ (__scanf__
, 2, 0)));
388 #endif /* Use ISO C9x. */
391 __BEGIN_NAMESPACE_STD
392 /* Read a character from STREAM. */
393 extern int fgetc (FILE *__stream
) __THROW
;
394 extern int getc (FILE *__stream
) __THROW
;
396 /* Read a character from stdin. */
397 extern int getchar (void) __THROW
;
400 /* The C standard explicitly says this is a macro, so we always do the
401 optimization for it. */
402 #define getc(_fp) _IO_getc (_fp)
404 #if defined __USE_POSIX || defined __USE_MISC
405 /* These are defined in POSIX.1:1996. */
406 extern int getc_unlocked (FILE *__stream
) __THROW
;
407 extern int getchar_unlocked (void) __THROW
;
408 #endif /* Use POSIX or MISC. */
411 /* Faster version when locking is not necessary. */
412 extern int fgetc_unlocked (FILE *__stream
) __THROW
;
413 #endif /* Use MISC. */
416 __BEGIN_NAMESPACE_STD
417 /* Write a character to STREAM. */
418 extern int fputc (int __c
, FILE *__stream
) __THROW
;
419 extern int putc (int __c
, FILE *__stream
) __THROW
;
421 /* Write a character to stdout. */
422 extern int putchar (int __c
) __THROW
;
425 /* The C standard explicitly says this can be a macro,
426 so we always do the optimization for it. */
427 #define putc(_ch, _fp) _IO_putc (_ch, _fp)
430 /* Faster version when locking is not necessary. */
431 extern int fputc_unlocked (int __c
, FILE *__stream
) __THROW
;
432 #endif /* Use MISC. */
434 #if defined __USE_POSIX || defined __USE_MISC
435 /* These are defined in POSIX.1:1996. */
436 extern int putc_unlocked (int __c
, FILE *__stream
) __THROW
;
437 extern int putchar_unlocked (int __c
) __THROW
;
438 #endif /* Use POSIX or MISC. */
441 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
442 /* Get a word (int) from STREAM. */
443 extern int getw (FILE *__stream
) __THROW
;
445 /* Write a word (int) to STREAM. */
446 extern int putw (int __w
, FILE *__stream
) __THROW
;
450 __BEGIN_NAMESPACE_STD
451 /* Get a newline-terminated string of finite length from STREAM. */
452 extern char *fgets (char *__restrict __s
, int __n
, FILE *__restrict __stream
)
455 /* Get a newline-terminated string from stdin, removing the newline.
456 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
457 extern char *gets (char *__s
) __THROW
;
461 /* This function does the same as `fgets' but does not lock the stream. */
462 extern char *fgets_unlocked (char *__restrict __s
, int __n
,
463 FILE *__restrict __stream
) __THROW
;
468 /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
469 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
470 NULL), pointing to *N characters of space. It is realloc'd as
471 necessary. Returns the number of characters read (not including the
472 null terminator), or -1 on error or EOF. */
473 extern _IO_ssize_t
__getdelim (char **__restrict __lineptr
,
474 size_t *__restrict __n
, int __delimiter
,
475 FILE *__restrict __stream
) __THROW
;
476 extern _IO_ssize_t
getdelim (char **__restrict __lineptr
,
477 size_t *__restrict __n
, int __delimiter
,
478 FILE *__restrict __stream
) __THROW
;
480 /* Like `getdelim', but reads up to a newline. */
481 extern _IO_ssize_t
getline (char **__restrict __lineptr
,
482 size_t *__restrict __n
,
483 FILE *__restrict __stream
) __THROW
;
487 __BEGIN_NAMESPACE_STD
488 /* Write a string to STREAM. */
489 extern int fputs (__const
char *__restrict __s
, FILE *__restrict __stream
)
492 /* Write a string, followed by a newline, to stdout. */
493 extern int puts (__const
char *__s
) __THROW
;
496 /* Push a character back onto the input buffer of STREAM. */
497 extern int ungetc (int __c
, FILE *__stream
) __THROW
;
500 /* Read chunks of generic data from STREAM. */
501 extern size_t fread (void *__restrict __ptr
, size_t __size
,
502 size_t __n
, FILE *__restrict __stream
) __THROW
;
503 /* Write chunks of generic data to STREAM. */
504 extern size_t fwrite (__const
void *__restrict __ptr
, size_t __size
,
505 size_t __n
, FILE *__restrict __s
) __THROW
;
509 /* This function does the same as `fputs' but does not lock the stream. */
510 extern int fputs_unlocked (__const
char *__restrict __s
,
511 FILE *__restrict __stream
) __THROW
;
515 /* Faster versions when locking is not necessary. */
516 extern size_t fread_unlocked (void *__restrict __ptr
, size_t __size
,
517 size_t __n
, FILE *__restrict __stream
) __THROW
;
518 extern size_t fwrite_unlocked (__const
void *__restrict __ptr
, size_t __size
,
519 size_t __n
, FILE *__restrict __stream
) __THROW
;
523 __BEGIN_NAMESPACE_STD
524 /* Seek to a certain position on STREAM. */
525 extern int fseek (FILE *__stream
, long int __off
, int __whence
) __THROW
;
526 /* Return the current position of STREAM. */
527 extern long int ftell (FILE *__stream
) __THROW
;
528 /* Rewind to the beginning of STREAM. */
529 extern void rewind (FILE *__stream
) __THROW
;
532 /* The Single Unix Specification, Version 2, specifies an alternative,
533 more adequate interface for the two functions above which deal with
534 file offset. `long int' is not the right type. These definitions
535 are originally defined in the Large File Support API. */
537 #ifdef __USE_LARGEFILE
538 # ifndef __USE_FILE_OFFSET64
539 /* Seek to a certain position on STREAM. */
540 extern int fseeko (FILE *__stream
, off_t __off
, int __whence
) __THROW
;
541 /* Return the current position of STREAM. */
542 extern off_t
ftello (FILE *__stream
) __THROW
;
545 extern int __REDIRECT (fseeko
,
546 (FILE *__stream
, __off64_t __off
, int __whence
) __THROW
,
548 extern __off64_t
__REDIRECT (ftello
, (FILE *__stream
) __THROW
, ftello64
);
550 # define fseeko fseeko64
551 # define ftello ftello64
556 __BEGIN_NAMESPACE_STD
557 #ifndef __USE_FILE_OFFSET64
558 /* Get STREAM's position. */
559 extern int fgetpos (FILE *__restrict __stream
, fpos_t *__restrict __pos
)
561 /* Set STREAM's position. */
562 extern int fsetpos (FILE *__stream
, __const
fpos_t *__pos
) __THROW
;
565 extern int __REDIRECT (fgetpos
, (FILE *__restrict __stream
,
566 fpos_t *__restrict __pos
) __THROW
, fgetpos64
);
567 extern int __REDIRECT (fsetpos
,
568 (FILE *__stream
, __const
fpos_t *__pos
) __THROW
,
571 # define fgetpos fgetpos64
572 # define fsetpos fsetpos64
577 #ifdef __USE_LARGEFILE64
578 extern int fseeko64 (FILE *__stream
, __off64_t __off
, int __whence
) __THROW
;
579 extern __off64_t
ftello64 (FILE *__stream
) __THROW
;
580 extern int fgetpos64 (FILE *__restrict __stream
, fpos64_t
*__restrict __pos
)
582 extern int fsetpos64 (FILE *__stream
, __const fpos64_t
*__pos
) __THROW
;
585 __BEGIN_NAMESPACE_STD
586 /* Clear the error and EOF indicators for STREAM. */
587 extern void clearerr (FILE *__stream
) __THROW
;
588 /* Return the EOF indicator for STREAM. */
589 extern int feof (FILE *__stream
) __THROW
;
590 /* Return the error indicator for STREAM. */
591 extern int ferror (FILE *__stream
) __THROW
;
595 /* Faster versions when locking is not required. */
596 extern void clearerr_unlocked (FILE *__stream
) __THROW
;
597 extern int feof_unlocked (FILE *__stream
) __THROW
;
598 extern int ferror_unlocked (FILE *__stream
) __THROW
;
602 __BEGIN_NAMESPACE_STD
603 /* Print a message describing the meaning of the value of errno. */
604 extern void perror (__const
char *__s
) __THROW
;
607 /* Provide the declarations for `sys_errlist' and `sys_nerr' if they
608 are available on this system. Even if available, these variables
609 should not be used directly. The `strerror' function provides
610 all the necessary functionality. */
611 #include <bits/sys_errlist.h>
615 /* Return the system file descriptor for STREAM. */
616 extern int fileno (FILE *__stream
) __THROW
;
617 #endif /* Use POSIX. */
620 /* Faster version when locking is not required. */
621 extern int fileno_unlocked (FILE *__stream
) __THROW
;
625 #if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \
627 /* Create a new stream connected to a pipe running the given command. */
628 extern FILE *popen (__const
char *__command
, __const
char *__modes
) __THROW
;
630 /* Close a stream opened by popen and return the status of its child. */
631 extern int pclose (FILE *__stream
) __THROW
;
636 /* Return the name of the controlling terminal. */
637 extern char *ctermid (char *__s
) __THROW
;
638 #endif /* Use POSIX. */
642 /* Return the name of the current user. */
643 extern char *cuserid (char *__s
) __THROW
;
644 #endif /* Use X/Open, but not issue 6. */
648 struct obstack
; /* See <obstack.h>. */
650 /* Write formatted output to an obstack. */
651 extern int obstack_printf (struct obstack
*__restrict __obstack
,
652 __const
char *__restrict __format
, ...)
653 __THROW
__attribute__ ((__format__ (__printf__
, 2, 3)));
654 extern int obstack_vprintf (struct obstack
*__restrict __obstack
,
655 __const
char *__restrict __format
,
657 __THROW
__attribute__ ((__format__ (__printf__
, 2, 0)));
658 #endif /* Use GNU. */
661 #if defined __USE_POSIX || defined __USE_MISC
662 /* These are defined in POSIX.1:1996. */
664 /* Acquire ownership of STREAM. */
665 extern void flockfile (FILE *__stream
) __THROW
;
667 /* Try to acquire ownership of STREAM but do not block if it is not
669 extern int ftrylockfile (FILE *__stream
) __THROW
;
671 /* Relinquish the ownership granted for STREAM. */
672 extern void funlockfile (FILE *__stream
) __THROW
;
673 #endif /* POSIX || misc */
675 #if defined __USE_XOPEN && !defined __USE_XOPEN2K && !defined __USE_GNU
676 /* The X/Open standard requires some functions and variables to be
677 declared here which do not belong into this header. But we have to
678 follow. In GNU mode we don't do this nonsense. */
679 # define __need_getopt
681 #endif /* X/Open, but not issue 6 and not for GNU. */
683 /* If we are compiling with optimizing read this file. It contains
684 several optimizing inline functions and macros. */
685 #ifdef __USE_EXTERN_INLINES
686 # include <bits/stdio.h>
691 #endif /* <stdio.h> included. */
693 #endif /* !_STDIO_H */