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
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
60 # define __STDIO_INLINE inline
62 # define __STDIO_INLINE extern __inline
65 /* The type of the second argument to `fgetpos' and `fsetpos'. */
66 #ifndef __USE_FILE_OFFSET64
67 typedef _G_fpos_t
fpos_t;
69 typedef _G_fpos64_t
fpos_t;
71 #ifdef __USE_LARGEFILE64
72 typedef _G_fpos64_t fpos64_t
;
75 /* Generate a unique file name (and possibly open it). */
76 extern int __path_search
__P ((char *__tmpl
, size_t __tmpl_len
,
78 __const
char *__pfx
));
80 extern int __gen_tempname
__P ((char *__tmpl
, int __openit
, int __large_file
));
82 /* Print out MESSAGE on the error output and abort. */
83 extern void __libc_fatal
__P ((__const
char *__message
))
84 __attribute__ ((__noreturn__
));
87 /* The possibilities for the third argument to `setvbuf'. */
88 #define _IOFBF 0 /* Fully buffered. */
89 #define _IOLBF 1 /* Line buffered. */
90 #define _IONBF 2 /* No buffering. */
93 /* Default buffer size. */
95 # define BUFSIZ _IO_BUFSIZ
99 /* End of file character.
100 Some things throughout the library rely on this being -1. */
106 /* The possibilities for the third argument to `fseek'.
107 These values should not be changed. */
108 #define SEEK_SET 0 /* Seek from beginning of file. */
109 #define SEEK_CUR 1 /* Seek from current position. */
110 #define SEEK_END 2 /* Seek from end of file. */
113 #if defined __USE_SVID || defined __USE_XOPEN
114 /* Default path prefix for `tempnam' and `tmpnam'. */
115 # define P_tmpdir "/tmp"
120 L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
121 TMP_MAX The minimum number of unique filenames generated by tmpnam
122 (and tempnam when it uses tmpnam's name space),
123 or tempnam (the two are separate).
124 L_ctermid How long an array to pass to `ctermid'.
125 L_cuserid How long an array to pass to `cuserid'.
126 FOPEN_MAX Minimum number of files that can be open at once.
127 FILENAME_MAX Maximum length of a filename. */
128 #include <bits/stdio_lim.h>
131 /* Standard streams. */
132 extern FILE *stdin
; /* Standard input stream. */
133 extern FILE *stdout
; /* Standard output stream. */
134 extern FILE *stderr
; /* Standard error output stream. */
137 /* Remove file FILENAME. */
138 extern int remove
__P ((__const
char *__filename
));
139 /* Rename file OLD to NEW. */
140 extern int rename
__P ((__const
char *__old
, __const
char *__new
));
143 /* Create a temporary file and open it read/write. */
144 #ifndef __USE_FILE_OFFSET64
145 extern FILE *tmpfile
__P ((void));
148 extern FILE *__REDIRECT (tmpfile
, __P ((void)), tmpfile64
);
150 # define tmpfile tmpfile64
153 #ifdef __USE_LARGEFILE64
154 extern FILE *tmpfile64
__P ((void));
156 /* Generate a temporary filename. */
157 extern char *tmpnam
__P ((char *__s
));
160 /* This is the reentrant variant of `tmpnam'. The only difference is
161 that it does not allow S to be NULL. */
162 extern char *tmpnam_r
__P ((char *__s
));
166 #if defined __USE_SVID || defined __USE_XOPEN
167 /* Generate a unique temporary filename using up to five characters of PFX
168 if it is not NULL. The directory to put this file in is searched for
169 as follows: First the environment variable "TMPDIR" is checked.
170 If it contains the name of a writable directory, that directory is used.
171 If not and if DIR is not NULL, that value is checked. If that fails,
172 P_tmpdir is tried and finally "/tmp". The storage for the filename
173 is allocated by `malloc'. */
174 extern char *tempnam
__P ((__const
char *__dir
, __const
char *__pfx
));
179 extern int fclose
__P ((FILE *__stream
));
180 /* Flush STREAM, or all streams if STREAM is NULL. */
181 extern int fflush
__P ((FILE *__stream
));
184 /* Faster versions when locking is not required. */
185 extern int fflush_unlocked
__P ((FILE *__stream
));
189 /* Close all streams. */
190 extern int fcloseall
__P ((void));
194 #ifndef __USE_FILE_OFFSET64
195 /* Open a file and create a new stream for it. */
196 extern FILE *fopen
__P ((__const
char *__restrict __filename
,
197 __const
char *__restrict __modes
));
198 /* Open a file, replacing an existing stream with it. */
199 extern FILE *freopen
__P ((__const
char *__restrict __filename
,
200 __const
char *__restrict __modes
,
201 FILE *__restrict __stream
));
204 extern FILE *__REDIRECT (fopen
, __P ((__const
char *__restrict __filename
,
205 __const
char *__restrict __modes
)),
207 extern FILE *__REDIRECT (freopen
, __P ((__const
char *__restrict __filename
,
208 __const
char *__restrict __modes
,
209 FILE *__restrict __stream
)),
212 # define fopen fopen64
213 # define freopen freopen64
216 #ifdef __USE_LARGEFILE64
217 extern FILE *fopen64
__P ((__const
char *__restrict __filename
,
218 __const
char *__restrict __modes
));
219 extern FILE *freopen64
__P ((__const
char *__restrict __filename
,
220 __const
char *__restrict __modes
,
221 FILE *__restrict __stream
));
225 /* Create a new stream that refers to an existing system file descriptor. */
226 extern FILE *fdopen
__P ((int __fd
, __const
char *__modes
));
230 /* Create a new stream that refers to the given magic cookie,
231 and uses the given functions for input and output. */
232 extern FILE *fopencookie
__P ((void *__magic_cookie
, __const
char *__modes
,
233 _IO_cookie_io_functions_t __io_funcs
));
235 /* Open a stream that writes into a malloc'd buffer that is expanded as
236 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
237 and the number of characters written on fflush or fclose. */
238 extern FILE *open_memstream
__P ((char **__bufloc
, size_t *__sizeloc
));
242 /* If BUF is NULL, make STREAM unbuffered.
243 Else make it use buffer BUF, of size BUFSIZ. */
244 extern void setbuf
__P ((FILE *__restrict __stream
, char *__restrict __buf
));
245 /* Make STREAM use buffering mode MODE.
246 If BUF is not NULL, use N bytes of it for buffering;
247 else allocate an internal buffer N bytes long. */
248 extern int setvbuf
__P ((FILE *__restrict __stream
, char *__restrict __buf
,
249 int __modes
, size_t __n
));
252 /* If BUF is NULL, make STREAM unbuffered.
253 Else make it use SIZE bytes of BUF for buffering. */
254 extern void setbuffer
__P ((FILE *__stream
, char *__buf
, size_t __size
));
256 /* Make STREAM line-buffered. */
257 extern void setlinebuf
__P ((FILE *__stream
));
261 /* Write formatted output to STREAM. */
262 extern int fprintf
__P ((FILE *__restrict __stream
,
263 __const
char *__restrict __format
, ...));
264 /* Write formatted output to stdout. */
265 extern int printf
__P ((__const
char *__restrict __format
, ...));
266 /* Write formatted output to S. */
267 extern int sprintf
__P ((char *__restrict __s
,
268 __const
char *__restrict __format
, ...));
270 /* Write formatted output to S from argument list ARG. */
271 extern int vfprintf
__P ((FILE *__restrict __s
,
272 __const
char *__restrict __format
,
274 /* Write formatted output to stdout from argument list ARG. */
275 extern int vprintf
__P ((__const
char *__restrict __format
,
277 /* Write formatted output to S from argument list ARG. */
278 extern int vsprintf
__P ((char *__restrict __s
,
279 __const
char *__restrict __format
,
282 #ifdef __USE_EXTERN_INLINES
284 vprintf (__const
char *__restrict __fmt
, _G_va_list __arg
)
286 return vfprintf (stdout
, __fmt
, __arg
);
288 #endif /* Use extern inlines. */
290 #if defined __USE_BSD || defined __USE_ISOC9X || defined __USE_UNIX98
291 /* Maximum chars of output to write in MAXLEN. */
292 extern int snprintf
__P ((char *__restrict __s
, size_t __maxlen
,
293 __const
char *__restrict __format
, ...))
294 __attribute__ ((__format__ (__printf__
, 3, 4)));
296 extern int __vsnprintf
__P ((char *__restrict __s
, size_t __maxlen
,
297 __const
char *__restrict __format
,
299 __attribute__ ((__format__ (__printf__
, 3, 0)));
300 extern int vsnprintf
__P ((char *__restrict __s
, size_t __maxlen
,
301 __const
char *__restrict __format
,
303 __attribute__ ((__format__ (__printf__
, 3, 0)));
307 /* Write formatted output to a string dynamically allocated with `malloc'.
308 Store the address of the string in *PTR. */
309 extern int vasprintf
__P ((char **__restrict __ptr
,
310 __const
char *__restrict __f
, _G_va_list __arg
))
311 __attribute__ ((__format__ (__printf__
, 2, 0)));
312 extern int __asprintf
__P ((char **__restrict __ptr
,
313 __const
char *__restrict __fmt
, ...))
314 __attribute__ ((__format__ (__printf__
, 2, 3)));
315 extern int asprintf
__P ((char **__restrict __ptr
,
316 __const
char *__restrict __fmt
, ...))
317 __attribute__ ((__format__ (__printf__
, 2, 3)));
319 /* Write formatted output to a file descriptor. */
320 extern int vdprintf
__P ((int __fd
, __const
char *__restrict __fmt
,
322 __attribute__ ((__format__ (__printf__
, 2, 0)));
323 extern int dprintf
__P ((int __fd
, __const
char *__restrict __fmt
, ...))
324 __attribute__ ((__format__ (__printf__
, 2, 3)));
328 /* Read formatted input from STREAM. */
329 extern int fscanf
__P ((FILE *__restrict __stream
,
330 __const
char *__restrict __format
, ...));
331 /* Read formatted input from stdin. */
332 extern int scanf
__P ((__const
char *__restrict __format
, ...));
333 /* Read formatted input from S. */
334 extern int sscanf
__P ((__const
char *__restrict __s
,
335 __const
char *__restrict __format
, ...));
338 /* Read formatted input from S into argument list ARG. */
339 extern int vfscanf
__P ((FILE *__restrict __s
,
340 __const
char *__restrict __format
,
342 __attribute__ ((__format__ (__scanf__
, 2, 0)));
344 /* Read formatted input from stdin into argument list ARG. */
345 extern int vscanf
__P ((__const
char *__restrict __format
, _G_va_list __arg
))
346 __attribute__ ((__format__ (__scanf__
, 1, 0)));
348 /* Read formatted input from S into argument list ARG. */
349 extern int vsscanf
__P ((__const
char *__restrict __s
,
350 __const
char *__restrict __format
,
352 __attribute__ ((__format__ (__scanf__
, 2, 0)));
353 #endif /* Use ISO C9x. */
356 /* Read a character from STREAM. */
357 extern int fgetc
__P ((FILE *__stream
));
358 extern int getc
__P ((FILE *__stream
));
360 /* Read a character from stdin. */
361 extern int getchar
__P ((void));
363 /* The C standard explicitly says this is a macro, so we always do the
364 optimization for it. */
365 #define getc(_fp) _IO_getc (_fp)
367 #ifdef __USE_EXTERN_INLINES
371 return _IO_getc (stdin
);
373 #endif /* Use extern inlines. */
375 #if defined __USE_POSIX || defined __USE_MISC
376 /* These are defined in POSIX.1:1996. */
377 extern int getc_unlocked
__P ((FILE *__stream
));
378 extern int getchar_unlocked
__P ((void));
380 # ifdef __USE_EXTERN_INLINES
382 getc_unlocked (FILE *__fp
)
384 return _IO_getc_unlocked (__fp
);
388 getchar_unlocked (void)
390 return _IO_getc_unlocked (stdin
);
392 # endif /* Use extern inlines. */
393 #endif /* Use POSIX or MISC. */
396 /* Write a character to STREAM. */
397 extern int fputc
__P ((int __c
, FILE *__stream
));
398 extern int putc
__P ((int __c
, FILE *__stream
));
400 /* Write a character to stdout. */
401 extern int putchar
__P ((int __c
));
403 /* The C standard explicitly says this can be a macro,
404 so we always do the optimization for it. */
405 #define putc(_ch, _fp) _IO_putc (_ch, _fp)
407 #ifdef __USE_EXTERN_INLINES
411 return _IO_putc (__c
, stdout
);
413 #endif /* Use extern inlines. */
416 /* Faster version when locking is not necessary. */
417 extern int fputc_unlocked
__P ((int __c
, FILE *__stream
));
419 # ifdef __USE_EXTERN_INLINES
421 fputc_unlocked (int __c
, FILE *__stream
)
423 return _IO_putc_unlocked (__c
, __stream
);
425 # endif /* Use extern inlines. */
426 #endif /* Use MISC. */
428 #if defined __USE_POSIX || defined __USE_MISC
429 /* These are defined in POSIX.1:1996. */
430 extern int putc_unlocked
__P ((int __c
, FILE *__stream
));
431 extern int putchar_unlocked
__P ((int __c
));
433 # ifdef __USE_EXTERN_INLINES
435 putc_unlocked (int __c
, FILE *__stream
)
437 return _IO_putc_unlocked (__c
, __stream
);
441 putchar_unlocked (int __c
)
443 return _IO_putc_unlocked (__c
, stdout
);
445 # endif /* Use extern inlines. */
446 #endif /* Use POSIX or MISC. */
449 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
450 /* Get a word (int) from STREAM. */
451 extern int getw
__P ((FILE *__stream
));
453 /* Write a word (int) to STREAM. */
454 extern int putw
__P ((int __w
, FILE *__stream
));
458 /* Get a newline-terminated string of finite length from STREAM. */
459 extern char *fgets
__P ((char *__restrict __s
, int __n
,
460 FILE *__restrict __stream
));
463 /* This function does the same as `fgets' but does not lock the stream. */
464 extern char *fgets_unlocked
__P ((char *__restrict __s
, int __n
,
465 FILE *__restrict __stream
));
468 /* Get a newline-terminated string from stdin, removing the newline.
469 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
470 extern char *gets
__P ((char *__s
));
474 /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
475 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
476 NULL), pointing to *N characters of space. It is realloc'd as
477 necessary. Returns the number of characters read (not including the
478 null terminator), or -1 on error or EOF. */
479 extern _IO_ssize_t __getdelim
__P ((char **__lineptr
, size_t *__n
,
480 int __delimiter
, FILE *__stream
));
481 extern _IO_ssize_t getdelim
__P ((char **__lineptr
, size_t *__n
,
482 int __delimiter
, FILE *__stream
));
484 /* Like `getdelim', but reads up to a newline. */
485 extern _IO_ssize_t getline
__P ((char **__lineptr
, size_t *__n
,
488 # ifdef __USE_EXTERN_INLINES
489 __STDIO_INLINE _IO_ssize_t
490 getline (char **__lineptr
, size_t *__n
, FILE *__stream
)
492 return __getdelim (__lineptr
, __n
, '\n', __stream
);
494 # endif /* Use extern inlines. */
498 /* Write a string to STREAM. */
499 extern int fputs
__P ((__const
char *__restrict __s
,
500 FILE *__restrict __stream
));
503 /* This function does the same as `fputs' but does not lock the stream. */
504 extern int fputs_unlocked
__P ((__const
char *__restrict __s
,
505 FILE *__restrict __stream
));
508 /* Write a string, followed by a newline, to stdout. */
509 extern int puts
__P ((__const
char *__s
));
512 /* Push a character back onto the input buffer of STREAM. */
513 extern int ungetc
__P ((int __c
, FILE *__stream
));
516 /* Read chunks of generic data from STREAM. */
517 extern size_t fread
__P ((void *__restrict __ptr
, size_t __size
,
518 size_t __n
, FILE *__restrict __stream
));
519 /* Write chunks of generic data to STREAM. */
520 extern size_t fwrite
__P ((__const
void *__restrict __ptr
, size_t __size
,
521 size_t __n
, FILE *__restrict __s
));
524 /* Faster versions when locking is not necessary. */
525 extern size_t fread_unlocked
__P ((void *__restrict __ptr
, size_t __size
,
526 size_t __n
, FILE *__restrict __stream
));
527 extern size_t fwrite_unlocked
__P ((__const
void *__restrict __ptr
,
528 size_t __size
, size_t __n
,
529 FILE *__restrict __stream
));
533 /* Seek to a certain position on STREAM. */
534 extern int fseek
__P ((FILE *__stream
, long int __off
, int __whence
));
535 /* Return the current position of STREAM. */
536 extern long int ftell
__P ((FILE *__stream
));
537 /* Rewind to the beginning of STREAM. */
538 extern void rewind
__P ((FILE *__stream
));
540 /* The Single Unix Specification, Version 2, specifies an alternative,
541 more adequate interface for the two functions above which deal with
542 file offset. `long int' is not the right type. These definitions
543 are originally defined in the Large File Support API. */
545 /* Types needed in these functions. */
547 # ifndef __USE_FILE_OFFSET64
548 typedef __off_t off_t
;
550 typedef __off64_t off_t
;
555 #if defined __USE_LARGEFILE64 && !defined off64_t
556 typedef __off64_t off64_t
;
557 # define off64_t off64_t
561 #ifndef __USE_FILE_OFFSET64
563 /* Seek to a certain position on STREAM. */
564 extern int fseeko
__P ((FILE *__stream
, __off_t __off
, int __whence
));
565 /* Return the current position of STREAM. */
566 extern __off_t ftello
__P ((FILE *__stream
));
569 /* Get STREAM's position. */
570 extern int fgetpos
__P ((FILE *__restrict __stream
,
571 fpos_t *__restrict __pos
));
572 /* Set STREAM's position. */
573 extern int fsetpos
__P ((FILE *__stream
, __const
fpos_t *__pos
));
577 extern int __REDIRECT (fseeko
,
578 __P ((FILE *__stream
, __off64_t __off
, int __whence
)),
580 extern __off64_t
__REDIRECT (ftello
, __P ((FILE *__stream
)), ftello64
);
582 extern int __REDIRECT (fgetpos
, __P ((FILE *__restrict __stream
,
583 fpos_t *__restrict __pos
)), fgetpos64
);
584 extern int __REDIRECT (fsetpos
, __P ((FILE *__stream
, __const
fpos_t *__pos
)),
588 # define fseeko fseeko64
589 # define ftello ftello64
591 # define fgetpos fgetpos64
592 # define fsetpos fsetpos64
596 #ifdef __USE_LARGEFILE64
598 extern int fseeko64
__P ((FILE *__stream
, __off64_t __off
, int __whence
));
599 extern __off64_t ftello64
__P ((FILE *__stream
));
601 extern int fgetpos64
__P ((FILE *__restrict __stream
,
602 fpos64_t
*__restrict __pos
));
603 extern int fsetpos64
__P ((FILE *__stream
, __const fpos64_t
*__pos
));
606 /* Clear the error and EOF indicators for STREAM. */
607 extern void clearerr
__P ((FILE *__stream
));
608 /* Return the EOF indicator for STREAM. */
609 extern int feof
__P ((FILE *__stream
));
610 /* Return the error indicator for STREAM. */
611 extern int ferror
__P ((FILE *__stream
));
614 /* Faster versions when locking is not required. */
615 extern void clearerr_unlocked
__P ((FILE *__stream
));
616 extern int feof_unlocked
__P ((FILE *__stream
));
617 extern int ferror_unlocked
__P ((FILE *__stream
));
619 # ifdef __USE_EXTERN_INLINES
621 feof_unlocked (FILE *__stream
)
623 return _IO_feof_unlocked (__stream
);
627 ferror_unlocked (FILE *__stream
)
629 return _IO_ferror_unlocked (__stream
);
631 # endif /* Use extern inlines. */
635 /* Print a message describing the meaning of the value of errno. */
636 extern void perror
__P ((__const
char *__s
));
638 /* These variables normally should not be used directly. The `strerror'
639 function provides all the needed functionality. */
642 extern __const
char *__const sys_errlist
[];
645 extern int _sys_nerr
;
646 extern __const
char *__const _sys_errlist
[];
651 /* Return the system file descriptor for STREAM. */
652 extern int fileno
__P ((FILE *__stream
));
653 #endif /* Use POSIX. */
656 /* Faster version when locking is not required. */
657 extern int fileno_unlocked
__P ((FILE *__stream
));
661 #if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \
663 /* Create a new stream connected to a pipe running the given command. */
664 extern FILE *popen
__P ((__const
char *__command
, __const
char *__modes
));
666 /* Close a stream opened by popen and return the status of its child. */
667 extern int pclose
__P ((FILE *__stream
));
672 /* Return the name of the controlling terminal. */
673 extern char *ctermid
__P ((char *__s
));
674 #endif /* Use POSIX. */
678 /* Return the name of the current user. */
679 extern char *cuserid
__P ((char *__s
));
680 #endif /* Use X/Open. */
684 struct obstack
; /* See <obstack.h>. */
686 /* Write formatted output to an obstack. */
687 extern int obstack_printf
__P ((struct obstack
*__obstack
,
688 __const
char *__format
, ...));
689 extern int obstack_vprintf
__P ((struct obstack
*__obstack
,
690 __const
char *__format
,
692 #endif /* Use GNU. */
695 #if defined __USE_POSIX || defined __USE_MISC
696 /* These are defined in POSIX.1:1996. */
698 /* Acquire ownership of STREAM. */
699 extern void flockfile
__P ((FILE *__stream
));
701 /* Try to acquire ownership of STREAM but do not block if it is not
703 extern int ftrylockfile
__P ((FILE *__stream
));
705 /* Relinquish the ownership granted for STREAM. */
706 extern void funlockfile
__P ((FILE *__stream
));
707 #endif /* POSIX || misc */
709 #if defined __USE_XOPEN && !defined __USE_GNU
710 /* The X/Open standard requires some functions and variables to be
711 declared here which do not belong into this header. But we have to
712 follow. In GNU mode we don't do this nonsense. */
714 /* For more information on these symbols look in <getopt.h>. */
720 extern int getopt
__P ((int __argc
, char *__const
*__argv
,
721 __const
char *__shortopts
));
726 /* Define helper macro. */
727 #undef __STDIO_INLINE
729 #endif /* <stdio.h> included. */
731 #endif /* !_STDIO_H */