1 /* Define ISO C stdio on top of C++ iostreams.
2 Copyright (C) 1991, 94, 95, 96, 97, 98 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
35 # define __need___va_list
38 # include <bits/types.h>
39 #endif /* Don't need FILE. */
43 #ifndef __FILE_defined
45 /* The opaque type of streams. */
46 typedef struct _IO_FILE
FILE;
48 # define __FILE_defined 1
49 #endif /* FILE not defined. */
53 #define _STDIO_USES_IOSTREAM
58 # define __STDIO_INLINE inline
60 # define __STDIO_INLINE extern __inline
63 /* The type of the second argument to `fgetpos' and `fsetpos'. */
64 #ifndef __USE_FILE_OFFSET64
65 typedef _G_fpos_t
fpos_t;
67 typedef _G_fpos64_t
fpos_t;
69 #ifdef __USE_LARGEFILE64
70 typedef _G_fpos64_t fpos64_t
;
73 /* Generate a unique file name (and possibly open it with mode "w+b"). */
74 extern char *__stdio_gen_tempname
__P ((char *__buf
, size_t __bufsize
,
83 /* Print out MESSAGE on the error output and abort. */
84 extern void __libc_fatal
__P ((__const
char *__message
))
85 __attribute__ ((__noreturn__
));
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. */
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. */
138 /* Remove file FILENAME. */
139 extern int remove
__P ((__const
char *__filename
));
140 /* Rename file OLD to NEW. */
141 extern int rename
__P ((__const
char *__old
, __const
char *__new
));
144 /* Create a temporary file and open it read/write. */
145 #ifndef __USE_FILE_OFFSET64
146 extern FILE *tmpfile
__P ((void));
149 extern FILE *__REDIRECT (tmpfile
, __P ((void)), tmpfile64
);
151 # define tmpfile tmpfile64
154 #ifdef __USE_LARGEFILE64
155 extern FILE *tmpfile64
__P ((void));
157 /* Generate a temporary filename. */
158 extern char *tmpnam
__P ((char *__s
));
161 /* This is the reentrant variant of `tmpnam'. The only difference is
162 that it does not allow S to be NULL. */
163 extern char *tmpnam_r
__P ((char *__s
));
167 #if defined __USE_SVID || defined __USE_XOPEN
168 /* Generate a unique temporary filename using up to five characters of PFX
169 if it is not NULL. The directory to put this file in is searched for
170 as follows: First the environment variable "TMPDIR" is checked.
171 If it contains the name of a writable directory, that directory is used.
172 If not and if DIR is not NULL, that value is checked. If that fails,
173 P_tmpdir is tried and finally "/tmp". The storage for the filename
174 is allocated by `malloc'. */
175 extern char *tempnam
__P ((__const
char *__dir
, __const
char *__pfx
));
180 extern int fclose
__P ((FILE *__stream
));
181 /* Flush STREAM, or all streams if STREAM is NULL. */
182 extern int fflush
__P ((FILE *__stream
));
185 /* Faster versions when locking is not required. */
186 extern int fclose_unlocked
__P ((FILE *__stream
));
187 extern int fflush_unlocked
__P ((FILE *__stream
));
191 /* Close all streams. */
192 extern int __fcloseall
__P ((void));
193 extern int fcloseall
__P ((void));
197 #ifndef __USE_FILE_OFFSET64
198 /* Open a file and create a new stream for it. */
199 extern FILE *fopen
__P ((__const
char *__restrict __filename
,
200 __const
char *__restrict __modes
));
201 /* Open a file, replacing an existing stream with it. */
202 extern FILE *freopen
__P ((__const
char *__restrict __filename
,
203 __const
char *__restrict __modes
,
204 FILE *__restrict __stream
));
207 extern FILE *__REDIRECT (fopen
, __P ((__const
char *__restrict __filename
,
208 __const
char *__restrict __modes
)),
210 extern FILE *__REDIRECT (freopen
, __P ((__const
char *__restrict __filename
,
211 __const
char *__restrict __modes
,
212 FILE *__restrict __stream
)),
215 # define fopen fopen64
216 # define freopen freopen64
219 #ifdef __USE_LARGEFILE64
220 extern FILE *fopen64
__P ((__const
char *__restrict __filename
,
221 __const
char *__restrict __modes
));
222 extern FILE *freopen64
__P ((__const
char *__restrict __filename
,
223 __const
char *__restrict __modes
,
224 FILE *__restrict __stream
));
228 /* Create a new stream that refers to an existing system file descriptor. */
229 extern FILE *fdopen
__P ((int __fd
, __const
char *__modes
));
233 /* Create a new stream that refers to the given magic cookie,
234 and uses the given functions for input and output. */
235 extern FILE *fopencookie
__P ((void *__magic_cookie
, __const
char *__modes
,
236 _IO_cookie_io_functions_t __io_funcs
));
238 /* Open a stream that writes into a malloc'd buffer that is expanded as
239 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
240 and the number of characters written on fflush or fclose. */
241 extern FILE *open_memstream
__P ((char **__bufloc
, size_t *__sizeloc
));
245 /* If BUF is NULL, make STREAM unbuffered.
246 Else make it use buffer BUF, of size BUFSIZ. */
247 extern void setbuf
__P ((FILE *__restrict __stream
, char *__restrict __buf
));
248 /* Make STREAM use buffering mode MODE.
249 If BUF is not NULL, use N bytes of it for buffering;
250 else allocate an internal buffer N bytes long. */
251 extern int setvbuf
__P ((FILE *__restrict __stream
, char *__restrict __buf
,
252 int __modes
, size_t __n
));
255 /* If BUF is NULL, make STREAM unbuffered.
256 Else make it use SIZE bytes of BUF for buffering. */
257 extern void setbuffer
__P ((FILE *__stream
, char *__buf
, size_t __size
));
259 /* Make STREAM line-buffered. */
260 extern void setlinebuf
__P ((FILE *__stream
));
264 /* Write formatted output to STREAM. */
265 extern int fprintf
__P ((FILE *__restrict __stream
,
266 __const
char *__restrict __format
, ...));
267 /* Write formatted output to stdout. */
268 extern int printf
__P ((__const
char *__restrict __format
, ...));
269 /* Write formatted output to S. */
270 extern int sprintf
__P ((char *__restrict __s
,
271 __const
char *__restrict __format
, ...));
273 /* Write formatted output to S from argument list ARG. */
274 extern int vfprintf
__P ((FILE *__restrict __s
,
275 __const
char *__restrict __format
,
277 /* Write formatted output to stdout from argument list ARG. */
278 extern int vprintf
__P ((__const
char *__restrict __format
,
280 /* Write formatted output to S from argument list ARG. */
281 extern int vsprintf
__P ((char *__restrict __s
,
282 __const
char *__restrict __format
,
287 vprintf (__const
char *__restrict __fmt
, _G_va_list __arg
)
289 return vfprintf (stdout
, __fmt
, __arg
);
291 #endif /* Optimizing. */
293 #if defined __USE_BSD || defined __USE_ISOC9X || defined __USE_UNIX98
294 /* Maximum chars of output to write in MAXLEN. */
295 extern int __snprintf
__P ((char *__restrict __s
, size_t __maxlen
,
296 __const
char *__restrict __format
, ...))
297 __attribute__ ((__format__ (__printf__
, 3, 4)));
298 extern int snprintf
__P ((char *__restrict __s
, size_t __maxlen
,
299 __const
char *__restrict __format
, ...))
300 __attribute__ ((__format__ (__printf__
, 3, 4)));
302 extern int __vsnprintf
__P ((char *__restrict __s
, size_t __maxlen
,
303 __const
char *__restrict __format
,
305 __attribute__ ((__format__ (__printf__
, 3, 0)));
306 extern int vsnprintf
__P ((char *__restrict __s
, size_t __maxlen
,
307 __const
char *__restrict __format
,
309 __attribute__ ((__format__ (__printf__
, 3, 0)));
313 /* Write formatted output to a string dynamically allocated with `malloc'.
314 Store the address of the string in *PTR. */
315 extern int vasprintf
__P ((char **__restrict __ptr
,
316 __const
char *__restrict __f
, _G_va_list __arg
))
317 __attribute__ ((__format__ (__printf__
, 2, 0)));
318 extern int __asprintf
__P ((char **__restrict __ptr
,
319 __const
char *__restrict __fmt
, ...))
320 __attribute__ ((__format__ (__printf__
, 2, 3)));
321 extern int asprintf
__P ((char **__restrict __ptr
,
322 __const
char *__restrict __fmt
, ...))
323 __attribute__ ((__format__ (__printf__
, 2, 3)));
325 /* Write formatted output to a file descriptor. */
326 extern int vdprintf
__P ((int __fd
, __const
char *__restrict __fmt
,
328 __attribute__ ((__format__ (__printf__
, 2, 0)));
329 extern int dprintf
__P ((int __fd
, __const
char *__restrict __fmt
, ...))
330 __attribute__ ((__format__ (__printf__
, 2, 3)));
334 /* Read formatted input from STREAM. */
335 extern int fscanf
__P ((FILE *__restrict __stream
,
336 __const
char *__restrict __format
, ...));
337 /* Read formatted input from stdin. */
338 extern int scanf
__P ((__const
char *__restrict __format
, ...));
339 /* Read formatted input from S. */
340 extern int sscanf
__P ((__const
char *__restrict __s
,
341 __const
char *__restrict __format
, ...));
344 /* Read formatted input from S into argument list ARG. */
345 extern int __vfscanf
__P ((FILE *__restrict __s
,
346 __const
char *__restrict __format
,
348 __attribute__ ((__format__ (__scanf__
, 2, 0)));
349 extern int vfscanf
__P ((FILE *__restrict __s
,
350 __const
char *__restrict __format
,
352 __attribute__ ((__format__ (__scanf__
, 2, 0)));
354 /* Read formatted input from stdin into argument list ARG. */
355 extern int __vscanf
__P ((__const
char *__restrict __format
,
357 __attribute__ ((__format__ (__scanf__
, 1, 0)));
358 extern int vscanf
__P ((__const
char *__restrict __format
, _G_va_list __arg
))
359 __attribute__ ((__format__ (__scanf__
, 1, 0)));
361 /* Read formatted input from S into argument list ARG. */
362 extern int __vsscanf
__P ((__const
char *__restrict __s
,
363 __const
char *__restrict __format
,
365 __attribute__ ((__format__ (__scanf__
, 2, 0)));
366 extern int vsscanf
__P ((__const
char *__restrict __s
,
367 __const
char *__restrict __format
,
369 __attribute__ ((__format__ (__scanf__
, 2, 0)));
370 #endif /* Use GNU. */
373 /* Read a character from STREAM. */
374 extern int fgetc
__P ((FILE *__stream
));
375 extern int getc
__P ((FILE *__stream
));
377 /* Read a character from stdin. */
378 extern int getchar
__P ((void));
380 /* The C standard explicitly says this is a macro, so we always do the
381 optimization for it. */
382 #define getc(_fp) _IO_getc (_fp)
388 return _IO_getc (stdin
);
390 #endif /* Optimizing. */
392 #if defined __USE_POSIX || defined __USE_MISC
393 /* These are defined in POSIX.1:1996. */
394 extern int getc_unlocked
__P ((FILE *__stream
));
395 extern int getchar_unlocked
__P ((void));
399 getc_unlocked (FILE *__fp
)
401 return _IO_getc_unlocked (__fp
);
405 getchar_unlocked (void)
407 return _IO_getc_unlocked (stdin
);
409 # endif /* Optimizing. */
410 #endif /* Use POSIX or MISC. */
413 /* Write a character to STREAM. */
414 extern int fputc
__P ((int __c
, FILE *__stream
));
415 extern int putc
__P ((int __c
, FILE *__stream
));
417 /* Write a character to stdout. */
418 extern int putchar
__P ((int __c
));
420 /* The C standard explicitly says this can be a macro,
421 so we always do the optimization for it. */
422 #define putc(_ch, _fp) _IO_putc (_ch, _fp)
428 return _IO_putc (__c
, stdout
);
433 /* Faster version when locking is not necessary. */
434 extern int fputc_unlocked
__P ((int __c
, FILE *__stream
));
438 fputc_unlocked (int __c
, FILE *__stream
)
440 return _IO_putc_unlocked (__c
, __stream
);
442 # endif /* Optimizing. */
443 #endif /* Use MISC. */
445 #if defined __USE_POSIX || defined __USE_MISC
446 /* These are defined in POSIX.1:1996. */
447 extern int putc_unlocked
__P ((int __c
, FILE *__stream
));
448 extern int putchar_unlocked
__P ((int __c
));
452 putc_unlocked (int __c
, FILE *__stream
)
454 return _IO_putc_unlocked (__c
, __stream
);
458 putchar_unlocked (int __c
)
460 return _IO_putc_unlocked (__c
, stdout
);
462 # endif /* Optimizing. */
463 #endif /* Use POSIX or MISc. */
466 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
467 /* Get a word (int) from STREAM. */
468 extern int getw
__P ((FILE *__stream
));
470 /* Write a word (int) to STREAM. */
471 extern int putw
__P ((int __w
, FILE *__stream
));
475 /* Get a newline-terminated string of finite length from STREAM. */
476 extern char *fgets
__P ((char *__restrict __s
, int __n
,
477 FILE *__restrict __stream
));
479 /* Get a newline-terminated string from stdin, removing the newline.
480 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
481 extern char *gets
__P ((char *__s
));
485 /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
486 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
487 NULL), pointing to *N characters of space. It is realloc'd as
488 necessary. Returns the number of characters read (not including the
489 null terminator), or -1 on error or EOF. */
490 _IO_ssize_t __getdelim
__P ((char **__lineptr
, size_t *__n
,
491 int __delimiter
, FILE *__stream
));
492 _IO_ssize_t getdelim
__P ((char **__lineptr
, size_t *__n
,
493 int __delimiter
, FILE *__stream
));
495 /* Like `getdelim', but reads up to a newline. */
496 _IO_ssize_t __getline
__P ((char **__lineptr
, size_t *__n
, FILE *__stream
));
497 _IO_ssize_t getline
__P ((char **__lineptr
, size_t *__n
, FILE *__stream
));
500 __STDIO_INLINE _IO_ssize_t
501 getline (char **__lineptr
, size_t *__n
, FILE *__stream
)
503 return __getdelim (__lineptr
, __n
, '\n', __stream
);
505 # endif /* Optimizing. */
509 /* Write a string to STREAM. */
510 extern int fputs
__P ((__const
char *__restrict __s
,
511 FILE *__restrict __stream
));
512 /* Write a string, followed by a newline, to stdout. */
513 extern int puts
__P ((__const
char *__s
));
516 /* Push a character back onto the input buffer of STREAM. */
517 extern int ungetc
__P ((int __c
, FILE *__stream
));
520 /* Read chunks of generic data from STREAM. */
521 extern size_t fread
__P ((void *__restrict __ptr
, size_t __size
,
522 size_t __n
, FILE *__restrict __stream
));
523 /* Write chunks of generic data to STREAM. */
524 extern size_t fwrite
__P ((__const
void *__restrict __ptr
, size_t __size
,
525 size_t __n
, FILE *__restrict __s
));
528 /* Faster versions when locking is not necessary. */
529 extern size_t fread_unlocked
__P ((void *__restrict __ptr
, size_t __size
,
530 size_t __n
, FILE *__restrict __stream
));
531 extern size_t fwrite_unlocked
__P ((__const
void *__restrict __ptr
,
532 size_t __size
, size_t __n
,
533 FILE *__restrict __stream
));
537 /* Seek to a certain position on STREAM. */
538 extern int fseek
__P ((FILE *__stream
, long int __off
, int __whence
));
539 /* Return the current position of STREAM. */
540 extern long int ftell
__P ((FILE *__stream
));
541 /* Rewind to the beginning of STREAM. */
542 extern void rewind
__P ((FILE *__stream
));
544 #if (defined __USE_LARGEFILE || defined __USE_LARGEFILE64 \
545 || defined __USE_FILE_OFFSET64)
546 /* The Single Unix Specification, Version 2, specifies an alternative,
547 more adequate interface for the two functions above which deal with
548 file offset. `long int' is not the right type. These definitions
549 are originally defined in the Large File Support API. */
551 /* Types needed in these functions. */
553 # ifndef __USE_FILE_OFFSET64
554 typedef __off_t off_t
;
556 typedef __off64_t off_t
;
561 # if defined __USE_LARGEFILE64 && !defined off64_t
562 typedef __off64_t off64_t
;
563 # define off64_t off64_t
567 # ifndef __USE_FILE_OFFSET64
568 /* Seek to a certain position on STREAM. */
569 extern int fseeko
__P ((FILE *__stream
, __off_t __off
, int __whence
));
570 /* Return the current position of STREAM. */
571 extern __off_t ftello
__P ((FILE *__stream
));
572 /* Get STREAM's position. */
573 extern int fgetpos
__P ((FILE *__restrict __stream
,
574 fpos_t *__restrict __pos
));
575 /* Set STREAM's position. */
576 extern int fsetpos
__P ((FILE *__stream
, __const
fpos_t *__pos
));
579 extern int __REDIRECT (fseeko
,
580 __P ((FILE *__stream
, __off64_t __off
, int __whence
)),
582 extern __off64_t
__REDIRECT (ftello
, __P ((FILE *__stream
)), ftello64
);
583 extern int __REDIRECT (fgetpos
, __P ((FILE *__restrict __stream
,
584 fpos_t *__restrict __pos
)), fgetpos64
);
585 extern int __REDIRECT (fsetpos
, __P ((FILE *__stream
, __const
fpos_t *__pos
)),
588 # define fseeko fseeko64
589 # define ftello ftello64
590 # define fgetpos fgetpos64
591 # define fsetpos fsetpos64
595 # ifdef __USE_LARGEFILE64
596 extern int fseeko64
__P ((FILE *__stream
, __off64_t __off
, int __whence
));
597 extern __off64_t ftello64
__P ((FILE *__stream
));
598 extern int fgetpos64
__P ((FILE *__restrict __stream
,
599 fpos64_t
*__restrict __pos
));
600 extern int fsetpos64
__P ((FILE *__stream
, __const fpos64_t
*__pos
));
604 /* Clear the error and EOF indicators for STREAM. */
605 extern void clearerr
__P ((FILE *__stream
));
606 /* Return the EOF indicator for STREAM. */
607 extern int feof
__P ((FILE *__stream
));
608 /* Return the error indicator for STREAM. */
609 extern int ferror
__P ((FILE *__stream
));
612 /* Faster versions when locking is not required. */
613 extern void clearerr_unlocked
__P ((FILE *__stream
));
614 extern int feof_unlocked
__P ((FILE *__stream
));
615 extern int ferror_unlocked
__P ((FILE *__stream
));
619 feof_unlocked (FILE *__stream
)
621 return _IO_feof_unlocked (__stream
);
625 ferror_unlocked (FILE *__stream
)
627 return _IO_ferror_unlocked (__stream
);
629 # endif /* Optimizing. */
633 /* Print a message describing the meaning of the value of errno. */
634 extern void perror
__P ((__const
char *__s
));
636 /* These variables normally should not be used directly. The `strerror'
637 function provides all the needed functionality. */
640 extern __const
char *__const sys_errlist
[];
643 extern int _sys_nerr
;
644 extern __const
char *__const _sys_errlist
[];
649 /* Return the system file descriptor for STREAM. */
650 extern int fileno
__P ((FILE *__stream
));
651 #endif /* Use POSIX. */
654 /* Faster version when locking is not required. */
655 extern int fileno_unlocked
__P ((FILE *__stream
));
659 #if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \
661 /* Create a new stream connected to a pipe running the given command. */
662 extern FILE *popen
__P ((__const
char *__command
, __const
char *__modes
));
664 /* Close a stream opened by popen and return the status of its child. */
665 extern int pclose
__P ((FILE *__stream
));
670 /* Return the name of the controlling terminal. */
671 extern char *ctermid
__P ((char *__s
));
672 #endif /* Use POSIX. */
676 /* Return the name of the current user. */
677 extern char *cuserid
__P ((char *__s
));
678 #endif /* Use X/Open. */
682 struct obstack
; /* See <obstack.h>. */
684 /* Write formatted output to an obstack. */
685 extern int obstack_printf
__P ((struct obstack
*__obstack
,
686 __const
char *__format
, ...));
687 extern int obstack_vprintf
__P ((struct obstack
*__obstack
,
688 __const
char *__format
,
690 #endif /* Use GNU. */
693 #if defined __USE_POSIX || defined __USE_MISC
694 /* These are defined in POSIX.1:1996. */
696 /* Acquire ownership of STREAM. */
697 extern void flockfile
__P ((FILE *__stream
));
699 /* Try to acquire ownership of STREAM but do not block if it is not
701 extern int ftrylockfile
__P ((FILE *__stream
));
703 /* Relinquish the ownership granted for STREAM. */
704 extern void funlockfile
__P ((FILE *__stream
));
705 #endif /* POSIX || misc */
707 #if defined __USE_XOPEN && !defined __USE_GNU
708 /* The X/Open standard requires some functions and variables to be
709 declared here which do not belong into this header. But we have to
710 follow. In GNU mode we don't do this nonsense. */
712 /* For more information on these symbols look in <getopt.h>. */
718 extern int getopt
__P ((int __argc
, char *__const
*__argv
,
719 __const
char *__shortopts
));
724 /* Define helper macro. */
725 #undef __STDIO_INLINE
727 #endif /* <stdio.h> included. */
729 #endif /* !_STDIO_H */