1 /* Define ISO C stdio on top of C++ iostreams.
2 Copyright (C) 1991, 1994-1999, 2000, 2001 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>
26 #if !defined __need_FILE && !defined __need___FILE
28 # include <features.h>
32 # define __need_size_t
36 # include <bits/types.h>
38 # define __need___FILE
39 #endif /* Don't need FILE. */
42 #if !defined __FILE_defined && defined __need_FILE
44 /* The opaque type of streams. This is the definition used elsewhere. */
45 typedef struct _IO_FILE
FILE;
47 # define __FILE_defined 1
48 #endif /* FILE not defined. */
52 #if !defined ____FILE_defined && defined __need___FILE
54 /* The opaque type of streams. This is the definition used elsewhere. */
55 typedef struct _IO_FILE __FILE
;
57 # define ____FILE_defined 1
58 #endif /* __FILE not defined. */
63 #define _STDIO_USES_IOSTREAM
69 # ifndef _VA_LIST_DEFINED
70 typedef _G_va_list
va_list;
71 # define _VA_LIST_DEFINED
78 /* The type of the second argument to `fgetpos' and `fsetpos'. */
79 #ifndef __USE_FILE_OFFSET64
80 typedef _G_fpos_t
fpos_t;
82 typedef _G_fpos64_t
fpos_t;
84 #ifdef __USE_LARGEFILE64
85 typedef _G_fpos64_t fpos64_t
;
88 /* The possibilities for the third argument to `setvbuf'. */
89 #define _IOFBF 0 /* Fully buffered. */
90 #define _IOLBF 1 /* Line buffered. */
91 #define _IONBF 2 /* No buffering. */
94 /* Default buffer size. */
96 # define BUFSIZ _IO_BUFSIZ
100 /* End of file character.
101 Some things throughout the library rely on this being -1. */
107 /* The possibilities for the third argument to `fseek'.
108 These values should not be changed. */
109 #define SEEK_SET 0 /* Seek from beginning of file. */
110 #define SEEK_CUR 1 /* Seek from current position. */
111 #define SEEK_END 2 /* Seek from end of file. */
114 #if defined __USE_SVID || defined __USE_XOPEN
115 /* Default path prefix for `tempnam' and `tmpnam'. */
116 # define P_tmpdir "/tmp"
121 L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
122 TMP_MAX The minimum number of unique filenames generated by tmpnam
123 (and tempnam when it uses tmpnam's name space),
124 or tempnam (the two are separate).
125 L_ctermid How long an array to pass to `ctermid'.
126 L_cuserid How long an array to pass to `cuserid'.
127 FOPEN_MAX Minimum number of files that can be open at once.
128 FILENAME_MAX Maximum length of a filename. */
129 #include <bits/stdio_lim.h>
132 /* Standard streams. */
133 extern FILE *stdin
; /* Standard input stream. */
134 extern FILE *stdout
; /* Standard output stream. */
135 extern FILE *stderr
; /* Standard error output stream. */
136 /* C89/C99 say they're macros. Make them happy. */
138 #define stdout stdout
139 #define stderr stderr
141 /* Remove file FILENAME. */
142 extern int remove (__const
char *__filename
) __THROW
;
143 /* Rename file OLD to NEW. */
144 extern int rename (__const
char *__old
, __const
char *__new
) __THROW
;
147 /* Create a temporary file and open it read/write. */
148 #ifndef __USE_FILE_OFFSET64
149 extern FILE *tmpfile (void) __THROW
;
152 extern FILE *__REDIRECT (tmpfile
, (void) __THROW
, tmpfile64
);
154 # define tmpfile tmpfile64
157 #ifdef __USE_LARGEFILE64
158 extern FILE *tmpfile64 (void) __THROW
;
160 /* Generate a temporary filename. */
161 extern char *tmpnam (char *__s
) __THROW
;
164 /* This is the reentrant variant of `tmpnam'. The only difference is
165 that it does not allow S to be NULL. */
166 extern char *tmpnam_r (char *__s
) __THROW
;
170 #if defined __USE_SVID || defined __USE_XOPEN
171 /* Generate a unique temporary filename using up to five characters of PFX
172 if it is not NULL. The directory to put this file in is searched for
173 as follows: First the environment variable "TMPDIR" is checked.
174 If it contains the name of a writable directory, that directory is used.
175 If not and if DIR is not NULL, that value is checked. If that fails,
176 P_tmpdir is tried and finally "/tmp". The storage for the filename
177 is allocated by `malloc'. */
178 extern char *tempnam (__const
char *__dir
, __const
char *__pfx
)
179 __THROW __attribute_malloc__
;
184 extern int fclose (FILE *__stream
) __THROW
;
185 /* Flush STREAM, or all streams if STREAM is NULL. */
186 extern int fflush (FILE *__stream
) __THROW
;
189 /* Faster versions when locking is not required. */
190 extern int fflush_unlocked (FILE *__stream
) __THROW
;
194 /* Close all streams. */
195 extern int fcloseall (void) __THROW
;
199 #ifndef __USE_FILE_OFFSET64
200 /* Open a file and create a new stream for it. */
201 extern FILE *fopen (__const
char *__restrict __filename
,
202 __const
char *__restrict __modes
) __THROW
;
203 /* Open a file, replacing an existing stream with it. */
204 extern FILE *freopen (__const
char *__restrict __filename
,
205 __const
char *__restrict __modes
,
206 FILE *__restrict __stream
) __THROW
;
209 extern FILE *__REDIRECT (fopen
, (__const
char *__restrict __filename
,
210 __const
char *__restrict __modes
) __THROW
,
212 extern FILE *__REDIRECT (freopen
, (__const
char *__restrict __filename
,
213 __const
char *__restrict __modes
,
214 FILE *__restrict __stream
) __THROW
,
217 # define fopen fopen64
218 # define freopen freopen64
221 #ifdef __USE_LARGEFILE64
222 extern FILE *fopen64 (__const
char *__restrict __filename
,
223 __const
char *__restrict __modes
) __THROW
;
224 extern FILE *freopen64 (__const
char *__restrict __filename
,
225 __const
char *__restrict __modes
,
226 FILE *__restrict __stream
) __THROW
;
230 /* Create a new stream that refers to an existing system file descriptor. */
231 extern FILE *fdopen (int __fd
, __const
char *__modes
) __THROW
;
235 /* Create a new stream that refers to the given magic cookie,
236 and uses the given functions for input and output. */
237 extern FILE *fopencookie (void *__restrict __magic_cookie
,
238 __const
char *__restrict __modes
,
239 _IO_cookie_io_functions_t __io_funcs
) __THROW
;
241 /* Create a new stream that refers to a memory buffer. */
242 extern FILE *fmemopen (void *__s
, size_t __len
, __const
char *__modes
) __THROW
;
244 /* Open a stream that writes into a malloc'd buffer that is expanded as
245 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
246 and the number of characters written on fflush or fclose. */
247 extern FILE *open_memstream (char **__restrict __bufloc
,
248 size_t *__restrict __sizeloc
) __THROW
;
252 /* If BUF is NULL, make STREAM unbuffered.
253 Else make it use buffer BUF, of size BUFSIZ. */
254 extern void setbuf (FILE *__restrict __stream
, char *__restrict __buf
) __THROW
;
255 /* Make STREAM use buffering mode MODE.
256 If BUF is not NULL, use N bytes of it for buffering;
257 else allocate an internal buffer N bytes long. */
258 extern int setvbuf (FILE *__restrict __stream
, char *__restrict __buf
,
259 int __modes
, size_t __n
) __THROW
;
262 /* If BUF is NULL, make STREAM unbuffered.
263 Else make it use SIZE bytes of BUF for buffering. */
264 extern void setbuffer (FILE *__restrict __stream
, char *__restrict __buf
,
265 size_t __size
) __THROW
;
267 /* Make STREAM line-buffered. */
268 extern void setlinebuf (FILE *__stream
) __THROW
;
272 /* Write formatted output to STREAM. */
273 extern int fprintf (FILE *__restrict __stream
,
274 __const
char *__restrict __format
, ...) __THROW
;
275 /* Write formatted output to stdout. */
276 extern int printf (__const
char *__restrict __format
, ...) __THROW
;
277 /* Write formatted output to S. */
278 extern int sprintf (char *__restrict __s
,
279 __const
char *__restrict __format
, ...) __THROW
;
281 /* Write formatted output to S from argument list ARG. */
282 extern int vfprintf (FILE *__restrict __s
, __const
char *__restrict __format
,
283 _G_va_list __arg
) __THROW
;
284 /* Write formatted output to stdout from argument list ARG. */
285 extern int vprintf (__const
char *__restrict __format
, _G_va_list __arg
)
287 /* Write formatted output to S from argument list ARG. */
288 extern int vsprintf (char *__restrict __s
, __const
char *__restrict __format
,
289 _G_va_list __arg
) __THROW
;
291 #if defined __USE_BSD || defined __USE_ISOC99 || defined __USE_UNIX98
292 /* Maximum chars of output to write in MAXLEN. */
293 extern int snprintf (char *__restrict __s
, size_t __maxlen
,
294 __const
char *__restrict __format
, ...)
295 __THROW
__attribute__ ((__format__ (__printf__
, 3, 4)));
297 extern int vsnprintf (char *__restrict __s
, size_t __maxlen
,
298 __const
char *__restrict __format
, _G_va_list __arg
)
299 __THROW
__attribute__ ((__format__ (__printf__
, 3, 0)));
303 /* Write formatted output to a string dynamically allocated with `malloc'.
304 Store the address of the string in *PTR. */
305 extern int vasprintf (char **__restrict __ptr
, __const
char *__restrict __f
,
307 __THROW
__attribute__ ((__format__ (__printf__
, 2, 0)));
308 extern int __asprintf (char **__restrict __ptr
,
309 __const
char *__restrict __fmt
, ...)
310 __THROW
__attribute__ ((__format__ (__printf__
, 2, 3)));
311 extern int asprintf (char **__restrict __ptr
,
312 __const
char *__restrict __fmt
, ...)
313 __THROW
__attribute__ ((__format__ (__printf__
, 2, 3)));
315 /* Write formatted output to a file descriptor. */
316 extern int vdprintf (int __fd
, __const
char *__restrict __fmt
,
318 __THROW
__attribute__ ((__format__ (__printf__
, 2, 0)));
319 extern int dprintf (int __fd
, __const
char *__restrict __fmt
, ...)
320 __THROW
__attribute__ ((__format__ (__printf__
, 2, 3)));
324 /* Read formatted input from STREAM. */
325 extern int fscanf (FILE *__restrict __stream
,
326 __const
char *__restrict __format
, ...) __THROW
;
327 /* Read formatted input from stdin. */
328 extern int scanf (__const
char *__restrict __format
, ...) __THROW
;
329 /* Read formatted input from S. */
330 extern int sscanf (__const
char *__restrict __s
,
331 __const
char *__restrict __format
, ...) __THROW
;
334 /* Read formatted input from S into argument list ARG. */
335 extern int vfscanf (FILE *__restrict __s
, __const
char *__restrict __format
,
337 __THROW
__attribute__ ((__format__ (__scanf__
, 2, 0)));
339 /* Read formatted input from stdin into argument list ARG. */
340 extern int vscanf (__const
char *__restrict __format
, _G_va_list __arg
)
341 __THROW
__attribute__ ((__format__ (__scanf__
, 1, 0)));
343 /* Read formatted input from S into argument list ARG. */
344 extern int vsscanf (__const
char *__restrict __s
,
345 __const
char *__restrict __format
, _G_va_list __arg
)
346 __THROW
__attribute__ ((__format__ (__scanf__
, 2, 0)));
347 #endif /* Use ISO C9x. */
350 /* Read a character from STREAM. */
351 extern int fgetc (FILE *__stream
) __THROW
;
352 extern int getc (FILE *__stream
) __THROW
;
354 /* Read a character from stdin. */
355 extern int getchar (void) __THROW
;
357 /* The C standard explicitly says this is a macro, so we always do the
358 optimization for it. */
359 #define getc(_fp) _IO_getc (_fp)
361 #if defined __USE_POSIX || defined __USE_MISC
362 /* These are defined in POSIX.1:1996. */
363 extern int getc_unlocked (FILE *__stream
) __THROW
;
364 extern int getchar_unlocked (void) __THROW
;
365 #endif /* Use POSIX or MISC. */
368 /* Faster version when locking is not necessary. */
369 extern int fgetc_unlocked (FILE *__stream
) __THROW
;
370 #endif /* Use MISC. */
373 /* Write a character to STREAM. */
374 extern int fputc (int __c
, FILE *__stream
) __THROW
;
375 extern int putc (int __c
, FILE *__stream
) __THROW
;
377 /* Write a character to stdout. */
378 extern int putchar (int __c
) __THROW
;
380 /* The C standard explicitly says this can be a macro,
381 so we always do the optimization for it. */
382 #define putc(_ch, _fp) _IO_putc (_ch, _fp)
385 /* Faster version when locking is not necessary. */
386 extern int fputc_unlocked (int __c
, FILE *__stream
) __THROW
;
387 #endif /* Use MISC. */
389 #if defined __USE_POSIX || defined __USE_MISC
390 /* These are defined in POSIX.1:1996. */
391 extern int putc_unlocked (int __c
, FILE *__stream
) __THROW
;
392 extern int putchar_unlocked (int __c
) __THROW
;
393 #endif /* Use POSIX or MISC. */
396 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
397 /* Get a word (int) from STREAM. */
398 extern int getw (FILE *__stream
) __THROW
;
400 /* Write a word (int) to STREAM. */
401 extern int putw (int __w
, FILE *__stream
) __THROW
;
405 /* Get a newline-terminated string of finite length from STREAM. */
406 extern char *fgets (char *__restrict __s
, int __n
, FILE *__restrict __stream
)
410 /* This function does the same as `fgets' but does not lock the stream. */
411 extern char *fgets_unlocked (char *__restrict __s
, int __n
,
412 FILE *__restrict __stream
) __THROW
;
415 /* Get a newline-terminated string from stdin, removing the newline.
416 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
417 extern char *gets (char *__s
) __THROW
;
421 /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
422 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
423 NULL), pointing to *N characters of space. It is realloc'd as
424 necessary. Returns the number of characters read (not including the
425 null terminator), or -1 on error or EOF. */
426 extern _IO_ssize_t
__getdelim (char **__restrict __lineptr
,
427 size_t *__restrict __n
, int __delimiter
,
428 FILE *__restrict __stream
) __THROW
;
429 extern _IO_ssize_t
getdelim (char **__restrict __lineptr
,
430 size_t *__restrict __n
, int __delimiter
,
431 FILE *__restrict __stream
) __THROW
;
433 /* Like `getdelim', but reads up to a newline. */
434 extern _IO_ssize_t
getline (char **__restrict __lineptr
,
435 size_t *__restrict __n
,
436 FILE *__restrict __stream
) __THROW
;
440 /* Write a string to STREAM. */
441 extern int fputs (__const
char *__restrict __s
, FILE *__restrict __stream
)
445 /* This function does the same as `fputs' but does not lock the stream. */
446 extern int fputs_unlocked (__const
char *__restrict __s
,
447 FILE *__restrict __stream
) __THROW
;
450 /* Write a string, followed by a newline, to stdout. */
451 extern int puts (__const
char *__s
) __THROW
;
454 /* Push a character back onto the input buffer of STREAM. */
455 extern int ungetc (int __c
, FILE *__stream
) __THROW
;
458 /* Read chunks of generic data from STREAM. */
459 extern size_t fread (void *__restrict __ptr
, size_t __size
,
460 size_t __n
, FILE *__restrict __stream
) __THROW
;
461 /* Write chunks of generic data to STREAM. */
462 extern size_t fwrite (__const
void *__restrict __ptr
, size_t __size
,
463 size_t __n
, FILE *__restrict __s
) __THROW
;
466 /* Faster versions when locking is not necessary. */
467 extern size_t fread_unlocked (void *__restrict __ptr
, size_t __size
,
468 size_t __n
, FILE *__restrict __stream
) __THROW
;
469 extern size_t fwrite_unlocked (__const
void *__restrict __ptr
, size_t __size
,
470 size_t __n
, FILE *__restrict __stream
) __THROW
;
474 /* Seek to a certain position on STREAM. */
475 extern int fseek (FILE *__stream
, long int __off
, int __whence
) __THROW
;
476 /* Return the current position of STREAM. */
477 extern long int ftell (FILE *__stream
) __THROW
;
478 /* Rewind to the beginning of STREAM. */
479 extern void rewind (FILE *__stream
) __THROW
;
481 /* The Single Unix Specification, Version 2, specifies an alternative,
482 more adequate interface for the two functions above which deal with
483 file offset. `long int' is not the right type. These definitions
484 are originally defined in the Large File Support API. */
486 #ifndef __USE_FILE_OFFSET64
487 # ifdef __USE_LARGEFILE
488 /* Seek to a certain position on STREAM. */
489 extern int fseeko (FILE *__stream
, __off_t __off
, int __whence
) __THROW
;
490 /* Return the current position of STREAM. */
491 extern __off_t
ftello (FILE *__stream
) __THROW
;
494 /* Get STREAM's position. */
495 extern int fgetpos (FILE *__restrict __stream
, fpos_t *__restrict __pos
)
497 /* Set STREAM's position. */
498 extern int fsetpos (FILE *__stream
, __const
fpos_t *__pos
) __THROW
;
501 # ifdef __USE_LARGEFILE
502 extern int __REDIRECT (fseeko
,
503 (FILE *__stream
, __off64_t __off
, int __whence
) __THROW
,
505 extern __off64_t
__REDIRECT (ftello
, (FILE *__stream
) __THROW
, ftello64
);
507 extern int __REDIRECT (fgetpos
, (FILE *__restrict __stream
,
508 fpos_t *__restrict __pos
) __THROW
, fgetpos64
);
509 extern int __REDIRECT (fsetpos
,
510 (FILE *__stream
, __const
fpos_t *__pos
) __THROW
,
513 # ifdef __USE_LARGEFILE
514 # define fseeko fseeko64
515 # define ftello ftello64
517 # define fgetpos fgetpos64
518 # define fsetpos fsetpos64
522 #ifdef __USE_LARGEFILE64
523 extern int fseeko64 (FILE *__stream
, __off64_t __off
, int __whence
) __THROW
;
524 extern __off64_t
ftello64 (FILE *__stream
) __THROW
;
525 extern int fgetpos64 (FILE *__restrict __stream
, fpos64_t
*__restrict __pos
)
527 extern int fsetpos64 (FILE *__stream
, __const fpos64_t
*__pos
) __THROW
;
530 /* Clear the error and EOF indicators for STREAM. */
531 extern void clearerr (FILE *__stream
) __THROW
;
532 /* Return the EOF indicator for STREAM. */
533 extern int feof (FILE *__stream
) __THROW
;
534 /* Return the error indicator for STREAM. */
535 extern int ferror (FILE *__stream
) __THROW
;
538 /* Faster versions when locking is not required. */
539 extern void clearerr_unlocked (FILE *__stream
) __THROW
;
540 extern int feof_unlocked (FILE *__stream
) __THROW
;
541 extern int ferror_unlocked (FILE *__stream
) __THROW
;
545 /* Print a message describing the meaning of the value of errno. */
546 extern void perror (__const
char *__s
) __THROW
;
548 /* These variables normally should not be used directly. The `strerror'
549 function provides all the needed functionality. */
552 extern __const
char *__const sys_errlist
[];
555 extern int _sys_nerr
;
556 extern __const
char *__const _sys_errlist
[];
561 /* Return the system file descriptor for STREAM. */
562 extern int fileno (FILE *__stream
) __THROW
;
563 #endif /* Use POSIX. */
566 /* Faster version when locking is not required. */
567 extern int fileno_unlocked (FILE *__stream
) __THROW
;
571 #if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \
573 /* Create a new stream connected to a pipe running the given command. */
574 extern FILE *popen (__const
char *__command
, __const
char *__modes
) __THROW
;
576 /* Close a stream opened by popen and return the status of its child. */
577 extern int pclose (FILE *__stream
) __THROW
;
582 /* Return the name of the controlling terminal. */
583 extern char *ctermid (char *__s
) __THROW
;
584 #endif /* Use POSIX. */
588 /* Return the name of the current user. */
589 extern char *cuserid (char *__s
) __THROW
;
590 #endif /* Use X/Open, but not issue 6. */
594 struct obstack
; /* See <obstack.h>. */
596 /* Write formatted output to an obstack. */
597 extern int obstack_printf (struct obstack
*__restrict __obstack
,
598 __const
char *__restrict __format
, ...) __THROW
;
599 extern int obstack_vprintf (struct obstack
*__restrict __obstack
,
600 __const
char *__restrict __format
,
601 _G_va_list __args
) __THROW
;
602 #endif /* Use GNU. */
605 #if defined __USE_POSIX || defined __USE_MISC
606 /* These are defined in POSIX.1:1996. */
608 /* Acquire ownership of STREAM. */
609 extern void flockfile (FILE *__stream
) __THROW
;
611 /* Try to acquire ownership of STREAM but do not block if it is not
613 extern int ftrylockfile (FILE *__stream
) __THROW
;
615 /* Relinquish the ownership granted for STREAM. */
616 extern void funlockfile (FILE *__stream
) __THROW
;
617 #endif /* POSIX || misc */
619 #if defined __USE_XOPEN && !defined __USE_XOPEN2K && !defined __USE_GNU
620 /* The X/Open standard requires some functions and variables to be
621 declared here which do not belong into this header. But we have to
622 follow. In GNU mode we don't do this nonsense. */
623 # define __need_getopt
625 #endif /* X/Open, but not issue 6 and not for GNU. */
627 /* If we are compiling with optimizing read this file. It contains
628 several optimizing inline functions and macros. */
629 #ifdef __USE_EXTERN_INLINES
630 # include <bits/stdio.h>
635 #endif /* <stdio.h> included. */
637 #endif /* !_STDIO_H */