1 /* Define ISO C stdio on top of C++ iostreams.
2 Copyright (C) 1991, 1994-1999, 2000 Free Software Foundation, Inc.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
20 * ISO C Standard: 4.9 INPUT/OUTPUT <stdio.h>
27 # include <features.h>
31 # define __need_size_t
36 # define __need___va_list
40 # include <bits/types.h>
41 #endif /* Don't need FILE. */
45 #ifndef __FILE_defined
47 /* The opaque type of streams. */
48 typedef struct _IO_FILE
FILE;
50 # define __FILE_defined 1
51 #endif /* FILE not defined. */
55 #define _STDIO_USES_IOSTREAM
59 /* The type of the second argument to `fgetpos' and `fsetpos'. */
60 #ifndef __USE_FILE_OFFSET64
61 typedef _G_fpos_t
fpos_t;
63 typedef _G_fpos64_t
fpos_t;
65 #ifdef __USE_LARGEFILE64
66 typedef _G_fpos64_t fpos64_t
;
69 /* The possibilities for the third argument to `setvbuf'. */
70 #define _IOFBF 0 /* Fully buffered. */
71 #define _IOLBF 1 /* Line buffered. */
72 #define _IONBF 2 /* No buffering. */
75 /* Default buffer size. */
77 # define BUFSIZ _IO_BUFSIZ
81 /* End of file character.
82 Some things throughout the library rely on this being -1. */
88 /* The possibilities for the third argument to `fseek'.
89 These values should not be changed. */
90 #define SEEK_SET 0 /* Seek from beginning of file. */
91 #define SEEK_CUR 1 /* Seek from current position. */
92 #define SEEK_END 2 /* Seek from end of file. */
95 #if defined __USE_SVID || defined __USE_XOPEN
96 /* Default path prefix for `tempnam' and `tmpnam'. */
97 # define P_tmpdir "/tmp"
102 L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
103 TMP_MAX The minimum number of unique filenames generated by tmpnam
104 (and tempnam when it uses tmpnam's name space),
105 or tempnam (the two are separate).
106 L_ctermid How long an array to pass to `ctermid'.
107 L_cuserid How long an array to pass to `cuserid'.
108 FOPEN_MAX Minimum number of files that can be open at once.
109 FILENAME_MAX Maximum length of a filename. */
110 #include <bits/stdio_lim.h>
113 /* Standard streams. */
114 extern FILE *stdin
; /* Standard input stream. */
115 extern FILE *stdout
; /* Standard output stream. */
116 extern FILE *stderr
; /* Standard error output stream. */
117 /* C89/C99 say they're macros. Make them happy. */
119 #define stdout stdout
120 #define stderr stderr
122 /* Remove file FILENAME. */
123 extern int remove (__const
char *__filename
) __THROW
;
124 /* Rename file OLD to NEW. */
125 extern int rename (__const
char *__old
, __const
char *__new
) __THROW
;
128 /* Create a temporary file and open it read/write. */
129 #ifndef __USE_FILE_OFFSET64
130 extern FILE *tmpfile (void) __THROW
;
133 extern FILE *__REDIRECT (tmpfile
, (void) __THROW
, tmpfile64
);
135 # define tmpfile tmpfile64
138 #ifdef __USE_LARGEFILE64
139 extern FILE *tmpfile64 (void) __THROW
;
141 /* Generate a temporary filename. */
142 extern char *tmpnam (char *__s
) __THROW
;
145 /* This is the reentrant variant of `tmpnam'. The only difference is
146 that it does not allow S to be NULL. */
147 extern char *tmpnam_r (char *__s
) __THROW
;
151 #if defined __USE_SVID || defined __USE_XOPEN
152 /* Generate a unique temporary filename using up to five characters of PFX
153 if it is not NULL. The directory to put this file in is searched for
154 as follows: First the environment variable "TMPDIR" is checked.
155 If it contains the name of a writable directory, that directory is used.
156 If not and if DIR is not NULL, that value is checked. If that fails,
157 P_tmpdir is tried and finally "/tmp". The storage for the filename
158 is allocated by `malloc'. */
159 extern char *tempnam (__const
char *__dir
, __const
char *__pfx
)
160 __THROW __attribute_malloc__
;
165 extern int fclose (FILE *__stream
) __THROW
;
166 /* Flush STREAM, or all streams if STREAM is NULL. */
167 extern int fflush (FILE *__stream
) __THROW
;
170 /* Faster versions when locking is not required. */
171 extern int fflush_unlocked (FILE *__stream
) __THROW
;
175 /* Close all streams. */
176 extern int fcloseall (void) __THROW
;
180 #ifndef __USE_FILE_OFFSET64
181 /* Open a file and create a new stream for it. */
182 extern FILE *fopen (__const
char *__restrict __filename
,
183 __const
char *__restrict __modes
) __THROW
;
184 /* Open a file, replacing an existing stream with it. */
185 extern FILE *freopen (__const
char *__restrict __filename
,
186 __const
char *__restrict __modes
,
187 FILE *__restrict __stream
) __THROW
;
190 extern FILE *__REDIRECT (fopen
, (__const
char *__restrict __filename
,
191 __const
char *__restrict __modes
) __THROW
,
193 extern FILE *__REDIRECT (freopen
, (__const
char *__restrict __filename
,
194 __const
char *__restrict __modes
,
195 FILE *__restrict __stream
) __THROW
,
198 # define fopen fopen64
199 # define freopen freopen64
202 #ifdef __USE_LARGEFILE64
203 extern FILE *fopen64 (__const
char *__restrict __filename
,
204 __const
char *__restrict __modes
) __THROW
;
205 extern FILE *freopen64 (__const
char *__restrict __filename
,
206 __const
char *__restrict __modes
,
207 FILE *__restrict __stream
) __THROW
;
211 /* Create a new stream that refers to an existing system file descriptor. */
212 extern FILE *fdopen (int __fd
, __const
char *__modes
) __THROW
;
216 /* Create a new stream that refers to the given magic cookie,
217 and uses the given functions for input and output. */
218 extern FILE *fopencookie (void *__restrict __magic_cookie
,
219 __const
char *__restrict __modes
,
220 _IO_cookie_io_functions_t __io_funcs
) __THROW
;
222 /* Create a new stream that refers to a memory buffer. */
223 extern FILE *fmemopen (void *__s
, size_t __len
, __const
char *__modes
) __THROW
;
225 /* Open a stream that writes into a malloc'd buffer that is expanded as
226 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
227 and the number of characters written on fflush or fclose. */
228 extern FILE *open_memstream (char **__restrict __bufloc
,
229 size_t *__restrict __sizeloc
) __THROW
;
233 /* If BUF is NULL, make STREAM unbuffered.
234 Else make it use buffer BUF, of size BUFSIZ. */
235 extern void setbuf (FILE *__restrict __stream
, char *__restrict __buf
) __THROW
;
236 /* Make STREAM use buffering mode MODE.
237 If BUF is not NULL, use N bytes of it for buffering;
238 else allocate an internal buffer N bytes long. */
239 extern int setvbuf (FILE *__restrict __stream
, char *__restrict __buf
,
240 int __modes
, size_t __n
) __THROW
;
243 /* If BUF is NULL, make STREAM unbuffered.
244 Else make it use SIZE bytes of BUF for buffering. */
245 extern void setbuffer (FILE *__restrict __stream
, char *__restrict __buf
,
246 size_t __size
) __THROW
;
248 /* Make STREAM line-buffered. */
249 extern void setlinebuf (FILE *__stream
) __THROW
;
253 /* Write formatted output to STREAM. */
254 extern int fprintf (FILE *__restrict __stream
,
255 __const
char *__restrict __format
, ...) __THROW
;
256 /* Write formatted output to stdout. */
257 extern int printf (__const
char *__restrict __format
, ...) __THROW
;
258 /* Write formatted output to S. */
259 extern int sprintf (char *__restrict __s
,
260 __const
char *__restrict __format
, ...) __THROW
;
262 /* Write formatted output to S from argument list ARG. */
263 extern int vfprintf (FILE *__restrict __s
, __const
char *__restrict __format
,
264 _G_va_list __arg
) __THROW
;
265 /* Write formatted output to stdout from argument list ARG. */
266 extern int vprintf (__const
char *__restrict __format
, _G_va_list __arg
)
268 /* Write formatted output to S from argument list ARG. */
269 extern int vsprintf (char *__restrict __s
, __const
char *__restrict __format
,
270 _G_va_list __arg
) __THROW
;
272 #if defined __USE_BSD || defined __USE_ISOC99 || defined __USE_UNIX98
273 /* Maximum chars of output to write in MAXLEN. */
274 extern int snprintf (char *__restrict __s
, size_t __maxlen
,
275 __const
char *__restrict __format
, ...)
276 __THROW
__attribute__ ((__format__ (__printf__
, 3, 4)));
278 extern int __vsnprintf (char *__restrict __s
, size_t __maxlen
,
279 __const
char *__restrict __format
, _G_va_list __arg
)
280 __THROW
__attribute__ ((__format__ (__printf__
, 3, 0)));
281 extern int vsnprintf (char *__restrict __s
, size_t __maxlen
,
282 __const
char *__restrict __format
, _G_va_list __arg
)
283 __THROW
__attribute__ ((__format__ (__printf__
, 3, 0)));
287 /* Write formatted output to a string dynamically allocated with `malloc'.
288 Store the address of the string in *PTR. */
289 extern int vasprintf (char **__restrict __ptr
, __const
char *__restrict __f
,
291 __THROW
__attribute__ ((__format__ (__printf__
, 2, 0)));
292 extern int __asprintf (char **__restrict __ptr
,
293 __const
char *__restrict __fmt
, ...)
294 __THROW
__attribute__ ((__format__ (__printf__
, 2, 3)));
295 extern int asprintf (char **__restrict __ptr
,
296 __const
char *__restrict __fmt
, ...)
297 __THROW
__attribute__ ((__format__ (__printf__
, 2, 3)));
299 /* Write formatted output to a file descriptor. */
300 extern int vdprintf (int __fd
, __const
char *__restrict __fmt
,
302 __THROW
__attribute__ ((__format__ (__printf__
, 2, 0)));
303 extern int dprintf (int __fd
, __const
char *__restrict __fmt
, ...)
304 __THROW
__attribute__ ((__format__ (__printf__
, 2, 3)));
308 /* Read formatted input from STREAM. */
309 extern int fscanf (FILE *__restrict __stream
,
310 __const
char *__restrict __format
, ...) __THROW
;
311 /* Read formatted input from stdin. */
312 extern int scanf (__const
char *__restrict __format
, ...) __THROW
;
313 /* Read formatted input from S. */
314 extern int sscanf (__const
char *__restrict __s
,
315 __const
char *__restrict __format
, ...) __THROW
;
318 /* Read formatted input from S into argument list ARG. */
319 extern int vfscanf (FILE *__restrict __s
, __const
char *__restrict __format
,
321 __THROW
__attribute__ ((__format__ (__scanf__
, 2, 0)));
323 /* Read formatted input from stdin into argument list ARG. */
324 extern int vscanf (__const
char *__restrict __format
, _G_va_list __arg
)
325 __THROW
__attribute__ ((__format__ (__scanf__
, 1, 0)));
327 /* Read formatted input from S into argument list ARG. */
328 extern int vsscanf (__const
char *__restrict __s
,
329 __const
char *__restrict __format
, _G_va_list __arg
)
330 __THROW
__attribute__ ((__format__ (__scanf__
, 2, 0)));
331 #endif /* Use ISO C9x. */
334 /* Read a character from STREAM. */
335 extern int fgetc (FILE *__stream
) __THROW
;
336 extern int getc (FILE *__stream
) __THROW
;
338 /* Read a character from stdin. */
339 extern int getchar (void) __THROW
;
341 /* The C standard explicitly says this is a macro, so we always do the
342 optimization for it. */
343 #define getc(_fp) _IO_getc (_fp)
345 #if defined __USE_POSIX || defined __USE_MISC
346 /* These are defined in POSIX.1:1996. */
347 extern int getc_unlocked (FILE *__stream
) __THROW
;
348 extern int getchar_unlocked (void) __THROW
;
349 #endif /* Use POSIX or MISC. */
352 /* Faster version when locking is not necessary. */
353 extern int fgetc_unlocked (FILE *__stream
) __THROW
;
354 #endif /* Use MISC. */
357 /* Write a character to STREAM. */
358 extern int fputc (int __c
, FILE *__stream
) __THROW
;
359 extern int putc (int __c
, FILE *__stream
) __THROW
;
361 /* Write a character to stdout. */
362 extern int putchar (int __c
) __THROW
;
364 /* The C standard explicitly says this can be a macro,
365 so we always do the optimization for it. */
366 #define putc(_ch, _fp) _IO_putc (_ch, _fp)
369 /* Faster version when locking is not necessary. */
370 extern int fputc_unlocked (int __c
, FILE *__stream
) __THROW
;
371 #endif /* Use MISC. */
373 #if defined __USE_POSIX || defined __USE_MISC
374 /* These are defined in POSIX.1:1996. */
375 extern int putc_unlocked (int __c
, FILE *__stream
) __THROW
;
376 extern int putchar_unlocked (int __c
) __THROW
;
377 #endif /* Use POSIX or MISC. */
380 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
381 /* Get a word (int) from STREAM. */
382 extern int getw (FILE *__stream
) __THROW
;
384 /* Write a word (int) to STREAM. */
385 extern int putw (int __w
, FILE *__stream
) __THROW
;
389 /* Get a newline-terminated string of finite length from STREAM. */
390 extern char *fgets (char *__restrict __s
, int __n
, FILE *__restrict __stream
)
394 /* This function does the same as `fgets' but does not lock the stream. */
395 extern char *fgets_unlocked (char *__restrict __s
, int __n
,
396 FILE *__restrict __stream
) __THROW
;
399 /* Get a newline-terminated string from stdin, removing the newline.
400 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
401 extern char *gets (char *__s
) __THROW
;
405 /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
406 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
407 NULL), pointing to *N characters of space. It is realloc'd as
408 necessary. Returns the number of characters read (not including the
409 null terminator), or -1 on error or EOF. */
410 extern _IO_ssize_t
__getdelim (char **__restrict __lineptr
,
411 size_t *__restrict __n
, int __delimiter
,
412 FILE *__restrict __stream
) __THROW
;
413 extern _IO_ssize_t
getdelim (char **__restrict __lineptr
,
414 size_t *__restrict __n
, int __delimiter
,
415 FILE *__restrict __stream
) __THROW
;
417 /* Like `getdelim', but reads up to a newline. */
418 extern _IO_ssize_t
getline (char **__restrict __lineptr
,
419 size_t *__restrict __n
,
420 FILE *__restrict __stream
) __THROW
;
424 /* Write a string to STREAM. */
425 extern int fputs (__const
char *__restrict __s
, FILE *__restrict __stream
)
429 /* This function does the same as `fputs' but does not lock the stream. */
430 extern int fputs_unlocked (__const
char *__restrict __s
,
431 FILE *__restrict __stream
) __THROW
;
434 /* Write a string, followed by a newline, to stdout. */
435 extern int puts (__const
char *__s
) __THROW
;
438 /* Push a character back onto the input buffer of STREAM. */
439 extern int ungetc (int __c
, FILE *__stream
) __THROW
;
442 /* Read chunks of generic data from STREAM. */
443 extern size_t fread (void *__restrict __ptr
, size_t __size
,
444 size_t __n
, FILE *__restrict __stream
) __THROW
;
445 /* Write chunks of generic data to STREAM. */
446 extern size_t fwrite (__const
void *__restrict __ptr
, size_t __size
,
447 size_t __n
, FILE *__restrict __s
) __THROW
;
450 /* Faster versions when locking is not necessary. */
451 extern size_t fread_unlocked (void *__restrict __ptr
, size_t __size
,
452 size_t __n
, FILE *__restrict __stream
) __THROW
;
453 extern size_t fwrite_unlocked (__const
void *__restrict __ptr
, size_t __size
,
454 size_t __n
, FILE *__restrict __stream
) __THROW
;
458 /* Seek to a certain position on STREAM. */
459 extern int fseek (FILE *__stream
, long int __off
, int __whence
) __THROW
;
460 /* Return the current position of STREAM. */
461 extern long int ftell (FILE *__stream
) __THROW
;
462 /* Rewind to the beginning of STREAM. */
463 extern void rewind (FILE *__stream
) __THROW
;
465 /* The Single Unix Specification, Version 2, specifies an alternative,
466 more adequate interface for the two functions above which deal with
467 file offset. `long int' is not the right type. These definitions
468 are originally defined in the Large File Support API. */
470 /* Types needed in these functions. */
471 #ifndef __off_t_defined
472 # ifndef __USE_FILE_OFFSET64
473 typedef __off_t off_t
;
475 typedef __off64_t off_t
;
477 # define __off_t_defined
480 #if defined __USE_LARGEFILE64 && !defined __off64_t_defined
481 typedef __off64_t off64_t
;
482 # define __off64_t_defined
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 optizing inline functions and macros. */
629 #ifdef __USE_EXTERN_INLINES
630 # include <bits/stdio.h>
635 #endif /* <stdio.h> included. */
637 #endif /* !_STDIO_H */