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 with mode "w+b"). */
76 extern char *__stdio_gen_tempname
__P ((char *__buf
, size_t __bufsize
,
85 /* Print out MESSAGE on the error output and abort. */
86 extern void __libc_fatal
__P ((__const
char *__message
))
87 __attribute__ ((__noreturn__
));
90 /* The possibilities for the third argument to `setvbuf'. */
91 #define _IOFBF 0 /* Fully buffered. */
92 #define _IOLBF 1 /* Line buffered. */
93 #define _IONBF 2 /* No buffering. */
96 /* Default buffer size. */
98 # define BUFSIZ _IO_BUFSIZ
102 /* End of file character.
103 Some things throughout the library rely on this being -1. */
109 /* The possibilities for the third argument to `fseek'.
110 These values should not be changed. */
111 #define SEEK_SET 0 /* Seek from beginning of file. */
112 #define SEEK_CUR 1 /* Seek from current position. */
113 #define SEEK_END 2 /* Seek from end of file. */
116 #if defined __USE_SVID || defined __USE_XOPEN
117 /* Default path prefix for `tempnam' and `tmpnam'. */
118 # define P_tmpdir "/tmp"
123 L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
124 TMP_MAX The minimum number of unique filenames generated by tmpnam
125 (and tempnam when it uses tmpnam's name space),
126 or tempnam (the two are separate).
127 L_ctermid How long an array to pass to `ctermid'.
128 L_cuserid How long an array to pass to `cuserid'.
129 FOPEN_MAX Minimum number of files that can be open at once.
130 FILENAME_MAX Maximum length of a filename. */
131 #include <bits/stdio_lim.h>
134 /* Standard streams. */
135 extern FILE *stdin
; /* Standard input stream. */
136 extern FILE *stdout
; /* Standard output stream. */
137 extern FILE *stderr
; /* Standard error output stream. */
140 /* Remove file FILENAME. */
141 extern int remove
__P ((__const
char *__filename
));
142 /* Rename file OLD to NEW. */
143 extern int rename
__P ((__const
char *__old
, __const
char *__new
));
146 /* Create a temporary file and open it read/write. */
147 #ifndef __USE_FILE_OFFSET64
148 extern FILE *tmpfile
__P ((void));
151 extern FILE *__REDIRECT (tmpfile
, __P ((void)), tmpfile64
);
153 # define tmpfile tmpfile64
156 #ifdef __USE_LARGEFILE64
157 extern FILE *tmpfile64
__P ((void));
159 /* Generate a temporary filename. */
160 extern char *tmpnam
__P ((char *__s
));
163 /* This is the reentrant variant of `tmpnam'. The only difference is
164 that it does not allow S to be NULL. */
165 extern char *tmpnam_r
__P ((char *__s
));
169 #if defined __USE_SVID || defined __USE_XOPEN
170 /* Generate a unique temporary filename using up to five characters of PFX
171 if it is not NULL. The directory to put this file in is searched for
172 as follows: First the environment variable "TMPDIR" is checked.
173 If it contains the name of a writable directory, that directory is used.
174 If not and if DIR is not NULL, that value is checked. If that fails,
175 P_tmpdir is tried and finally "/tmp". The storage for the filename
176 is allocated by `malloc'. */
177 extern char *tempnam
__P ((__const
char *__dir
, __const
char *__pfx
));
182 extern int fclose
__P ((FILE *__stream
));
183 /* Flush STREAM, or all streams if STREAM is NULL. */
184 extern int fflush
__P ((FILE *__stream
));
187 /* Faster versions when locking is not required. */
188 extern int fflush_unlocked
__P ((FILE *__stream
));
192 /* Close all streams. */
193 extern int __fcloseall
__P ((void));
194 extern int fcloseall
__P ((void));
198 #ifndef __USE_FILE_OFFSET64
199 /* Open a file and create a new stream for it. */
200 extern FILE *fopen
__P ((__const
char *__restrict __filename
,
201 __const
char *__restrict __modes
));
202 /* Open a file, replacing an existing stream with it. */
203 extern FILE *freopen
__P ((__const
char *__restrict __filename
,
204 __const
char *__restrict __modes
,
205 FILE *__restrict __stream
));
208 extern FILE *__REDIRECT (fopen
, __P ((__const
char *__restrict __filename
,
209 __const
char *__restrict __modes
)),
211 extern FILE *__REDIRECT (freopen
, __P ((__const
char *__restrict __filename
,
212 __const
char *__restrict __modes
,
213 FILE *__restrict __stream
)),
216 # define fopen fopen64
217 # define freopen freopen64
220 #ifdef __USE_LARGEFILE64
221 extern FILE *fopen64
__P ((__const
char *__restrict __filename
,
222 __const
char *__restrict __modes
));
223 extern FILE *freopen64
__P ((__const
char *__restrict __filename
,
224 __const
char *__restrict __modes
,
225 FILE *__restrict __stream
));
229 /* Create a new stream that refers to an existing system file descriptor. */
230 extern FILE *fdopen
__P ((int __fd
, __const
char *__modes
));
234 /* Create a new stream that refers to the given magic cookie,
235 and uses the given functions for input and output. */
236 extern FILE *fopencookie
__P ((void *__magic_cookie
, __const
char *__modes
,
237 _IO_cookie_io_functions_t __io_funcs
));
239 /* Open a stream that writes into a malloc'd buffer that is expanded as
240 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
241 and the number of characters written on fflush or fclose. */
242 extern FILE *open_memstream
__P ((char **__bufloc
, size_t *__sizeloc
));
246 /* If BUF is NULL, make STREAM unbuffered.
247 Else make it use buffer BUF, of size BUFSIZ. */
248 extern void setbuf
__P ((FILE *__restrict __stream
, char *__restrict __buf
));
249 /* Make STREAM use buffering mode MODE.
250 If BUF is not NULL, use N bytes of it for buffering;
251 else allocate an internal buffer N bytes long. */
252 extern int setvbuf
__P ((FILE *__restrict __stream
, char *__restrict __buf
,
253 int __modes
, size_t __n
));
256 /* If BUF is NULL, make STREAM unbuffered.
257 Else make it use SIZE bytes of BUF for buffering. */
258 extern void setbuffer
__P ((FILE *__stream
, char *__buf
, size_t __size
));
260 /* Make STREAM line-buffered. */
261 extern void setlinebuf
__P ((FILE *__stream
));
265 /* Write formatted output to STREAM. */
266 extern int fprintf
__P ((FILE *__restrict __stream
,
267 __const
char *__restrict __format
, ...));
268 /* Write formatted output to stdout. */
269 extern int printf
__P ((__const
char *__restrict __format
, ...));
270 /* Write formatted output to S. */
271 extern int sprintf
__P ((char *__restrict __s
,
272 __const
char *__restrict __format
, ...));
274 /* Write formatted output to S from argument list ARG. */
275 extern int vfprintf
__P ((FILE *__restrict __s
,
276 __const
char *__restrict __format
,
278 /* Write formatted output to stdout from argument list ARG. */
279 extern int vprintf
__P ((__const
char *__restrict __format
,
281 /* Write formatted output to S from argument list ARG. */
282 extern int vsprintf
__P ((char *__restrict __s
,
283 __const
char *__restrict __format
,
288 vprintf (__const
char *__restrict __fmt
, _G_va_list __arg
)
290 return vfprintf (stdout
, __fmt
, __arg
);
292 #endif /* Optimizing. */
294 #if defined __USE_BSD || defined __USE_ISOC9X || defined __USE_UNIX98
295 /* Maximum chars of output to write in MAXLEN. */
296 extern int __snprintf
__P ((char *__restrict __s
, size_t __maxlen
,
297 __const
char *__restrict __format
, ...))
298 __attribute__ ((__format__ (__printf__
, 3, 4)));
299 extern int snprintf
__P ((char *__restrict __s
, size_t __maxlen
,
300 __const
char *__restrict __format
, ...))
301 __attribute__ ((__format__ (__printf__
, 3, 4)));
303 extern int __vsnprintf
__P ((char *__restrict __s
, size_t __maxlen
,
304 __const
char *__restrict __format
,
306 __attribute__ ((__format__ (__printf__
, 3, 0)));
307 extern int vsnprintf
__P ((char *__restrict __s
, size_t __maxlen
,
308 __const
char *__restrict __format
,
310 __attribute__ ((__format__ (__printf__
, 3, 0)));
314 /* Write formatted output to a string dynamically allocated with `malloc'.
315 Store the address of the string in *PTR. */
316 extern int vasprintf
__P ((char **__restrict __ptr
,
317 __const
char *__restrict __f
, _G_va_list __arg
))
318 __attribute__ ((__format__ (__printf__
, 2, 0)));
319 extern int __asprintf
__P ((char **__restrict __ptr
,
320 __const
char *__restrict __fmt
, ...))
321 __attribute__ ((__format__ (__printf__
, 2, 3)));
322 extern int asprintf
__P ((char **__restrict __ptr
,
323 __const
char *__restrict __fmt
, ...))
324 __attribute__ ((__format__ (__printf__
, 2, 3)));
326 /* Write formatted output to a file descriptor. */
327 extern int vdprintf
__P ((int __fd
, __const
char *__restrict __fmt
,
329 __attribute__ ((__format__ (__printf__
, 2, 0)));
330 extern int dprintf
__P ((int __fd
, __const
char *__restrict __fmt
, ...))
331 __attribute__ ((__format__ (__printf__
, 2, 3)));
335 /* Read formatted input from STREAM. */
336 extern int fscanf
__P ((FILE *__restrict __stream
,
337 __const
char *__restrict __format
, ...));
338 /* Read formatted input from stdin. */
339 extern int scanf
__P ((__const
char *__restrict __format
, ...));
340 /* Read formatted input from S. */
341 extern int sscanf
__P ((__const
char *__restrict __s
,
342 __const
char *__restrict __format
, ...));
345 /* Read formatted input from S into argument list ARG. */
346 extern int __vfscanf
__P ((FILE *__restrict __s
,
347 __const
char *__restrict __format
,
349 __attribute__ ((__format__ (__scanf__
, 2, 0)));
350 extern int vfscanf
__P ((FILE *__restrict __s
,
351 __const
char *__restrict __format
,
353 __attribute__ ((__format__ (__scanf__
, 2, 0)));
355 /* Read formatted input from stdin into argument list ARG. */
356 extern int __vscanf
__P ((__const
char *__restrict __format
,
358 __attribute__ ((__format__ (__scanf__
, 1, 0)));
359 extern int vscanf
__P ((__const
char *__restrict __format
, _G_va_list __arg
))
360 __attribute__ ((__format__ (__scanf__
, 1, 0)));
362 /* Read formatted input from S into argument list ARG. */
363 extern int __vsscanf
__P ((__const
char *__restrict __s
,
364 __const
char *__restrict __format
,
366 __attribute__ ((__format__ (__scanf__
, 2, 0)));
367 extern int vsscanf
__P ((__const
char *__restrict __s
,
368 __const
char *__restrict __format
,
370 __attribute__ ((__format__ (__scanf__
, 2, 0)));
371 #endif /* Use GNU. */
374 /* Read a character from STREAM. */
375 extern int fgetc
__P ((FILE *__stream
));
376 extern int getc
__P ((FILE *__stream
));
378 /* Read a character from stdin. */
379 extern int getchar
__P ((void));
381 /* The C standard explicitly says this is a macro, so we always do the
382 optimization for it. */
383 #define getc(_fp) _IO_getc (_fp)
389 return _IO_getc (stdin
);
391 #endif /* Optimizing. */
393 #if defined __USE_POSIX || defined __USE_MISC
394 /* These are defined in POSIX.1:1996. */
395 extern int getc_unlocked
__P ((FILE *__stream
));
396 extern int getchar_unlocked
__P ((void));
400 getc_unlocked (FILE *__fp
)
402 return _IO_getc_unlocked (__fp
);
406 getchar_unlocked (void)
408 return _IO_getc_unlocked (stdin
);
410 # endif /* Optimizing. */
411 #endif /* Use POSIX or MISC. */
414 /* Write a character to STREAM. */
415 extern int fputc
__P ((int __c
, FILE *__stream
));
416 extern int putc
__P ((int __c
, FILE *__stream
));
418 /* Write a character to stdout. */
419 extern int putchar
__P ((int __c
));
421 /* The C standard explicitly says this can be a macro,
422 so we always do the optimization for it. */
423 #define putc(_ch, _fp) _IO_putc (_ch, _fp)
429 return _IO_putc (__c
, stdout
);
434 /* Faster version when locking is not necessary. */
435 extern int fputc_unlocked
__P ((int __c
, FILE *__stream
));
439 fputc_unlocked (int __c
, FILE *__stream
)
441 return _IO_putc_unlocked (__c
, __stream
);
443 # endif /* Optimizing. */
444 #endif /* Use MISC. */
446 #if defined __USE_POSIX || defined __USE_MISC
447 /* These are defined in POSIX.1:1996. */
448 extern int putc_unlocked
__P ((int __c
, FILE *__stream
));
449 extern int putchar_unlocked
__P ((int __c
));
453 putc_unlocked (int __c
, FILE *__stream
)
455 return _IO_putc_unlocked (__c
, __stream
);
459 putchar_unlocked (int __c
)
461 return _IO_putc_unlocked (__c
, stdout
);
463 # endif /* Optimizing. */
464 #endif /* Use POSIX or MISc. */
467 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
468 /* Get a word (int) from STREAM. */
469 extern int getw
__P ((FILE *__stream
));
471 /* Write a word (int) to STREAM. */
472 extern int putw
__P ((int __w
, FILE *__stream
));
476 /* Get a newline-terminated string of finite length from STREAM. */
477 extern char *fgets
__P ((char *__restrict __s
, int __n
,
478 FILE *__restrict __stream
));
481 /* This function does the same as `fgets' but does not lock the stream. */
482 extern char *fgets_unlocked
__P ((char *__restrict __s
, int __n
,
483 FILE *__restrict __stream
));
486 /* Get a newline-terminated string from stdin, removing the newline.
487 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
488 extern char *gets
__P ((char *__s
));
492 /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
493 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
494 NULL), pointing to *N characters of space. It is realloc'd as
495 necessary. Returns the number of characters read (not including the
496 null terminator), or -1 on error or EOF. */
497 _IO_ssize_t __getdelim
__P ((char **__lineptr
, size_t *__n
,
498 int __delimiter
, FILE *__stream
));
499 _IO_ssize_t getdelim
__P ((char **__lineptr
, size_t *__n
,
500 int __delimiter
, FILE *__stream
));
502 /* Like `getdelim', but reads up to a newline. */
503 _IO_ssize_t __getline
__P ((char **__lineptr
, size_t *__n
, FILE *__stream
));
504 _IO_ssize_t getline
__P ((char **__lineptr
, size_t *__n
, FILE *__stream
));
507 __STDIO_INLINE _IO_ssize_t
508 getline (char **__lineptr
, size_t *__n
, FILE *__stream
)
510 return __getdelim (__lineptr
, __n
, '\n', __stream
);
512 # endif /* Optimizing. */
516 /* Write a string to STREAM. */
517 extern int fputs
__P ((__const
char *__restrict __s
,
518 FILE *__restrict __stream
));
521 /* This function does the same as `fgets' but does not lock the stream. */
522 extern int fputs_unlocked
__P ((__const
char *__restrict __s
,
523 FILE *__restrict __stream
));
525 /* Write a string, followed by a newline, to stdout. */
526 extern int puts
__P ((__const
char *__s
));
529 /* Push a character back onto the input buffer of STREAM. */
530 extern int ungetc
__P ((int __c
, FILE *__stream
));
533 /* Read chunks of generic data from STREAM. */
534 extern size_t fread
__P ((void *__restrict __ptr
, size_t __size
,
535 size_t __n
, FILE *__restrict __stream
));
536 /* Write chunks of generic data to STREAM. */
537 extern size_t fwrite
__P ((__const
void *__restrict __ptr
, size_t __size
,
538 size_t __n
, FILE *__restrict __s
));
541 /* Faster versions when locking is not necessary. */
542 extern size_t fread_unlocked
__P ((void *__restrict __ptr
, size_t __size
,
543 size_t __n
, FILE *__restrict __stream
));
544 extern size_t fwrite_unlocked
__P ((__const
void *__restrict __ptr
,
545 size_t __size
, size_t __n
,
546 FILE *__restrict __stream
));
550 /* Seek to a certain position on STREAM. */
551 extern int fseek
__P ((FILE *__stream
, long int __off
, int __whence
));
552 /* Return the current position of STREAM. */
553 extern long int ftell
__P ((FILE *__stream
));
554 /* Rewind to the beginning of STREAM. */
555 extern void rewind
__P ((FILE *__stream
));
557 #if (defined __USE_LARGEFILE || defined __USE_LARGEFILE64 \
558 || defined __USE_FILE_OFFSET64)
559 /* The Single Unix Specification, Version 2, specifies an alternative,
560 more adequate interface for the two functions above which deal with
561 file offset. `long int' is not the right type. These definitions
562 are originally defined in the Large File Support API. */
564 /* Types needed in these functions. */
566 # ifndef __USE_FILE_OFFSET64
567 typedef __off_t off_t
;
569 typedef __off64_t off_t
;
574 # if defined __USE_LARGEFILE64 && !defined off64_t
575 typedef __off64_t off64_t
;
576 # define off64_t off64_t
580 # ifndef __USE_FILE_OFFSET64
581 /* Seek to a certain position on STREAM. */
582 extern int fseeko
__P ((FILE *__stream
, __off_t __off
, int __whence
));
583 /* Return the current position of STREAM. */
584 extern __off_t ftello
__P ((FILE *__stream
));
585 /* Get STREAM's position. */
586 extern int fgetpos
__P ((FILE *__restrict __stream
,
587 fpos_t *__restrict __pos
));
588 /* Set STREAM's position. */
589 extern int fsetpos
__P ((FILE *__stream
, __const
fpos_t *__pos
));
592 extern int __REDIRECT (fseeko
,
593 __P ((FILE *__stream
, __off64_t __off
, int __whence
)),
595 extern __off64_t
__REDIRECT (ftello
, __P ((FILE *__stream
)), ftello64
);
596 extern int __REDIRECT (fgetpos
, __P ((FILE *__restrict __stream
,
597 fpos_t *__restrict __pos
)), fgetpos64
);
598 extern int __REDIRECT (fsetpos
, __P ((FILE *__stream
, __const
fpos_t *__pos
)),
601 # define fseeko fseeko64
602 # define ftello ftello64
603 # define fgetpos fgetpos64
604 # define fsetpos fsetpos64
608 # ifdef __USE_LARGEFILE64
609 extern int fseeko64
__P ((FILE *__stream
, __off64_t __off
, int __whence
));
610 extern __off64_t ftello64
__P ((FILE *__stream
));
611 extern int fgetpos64
__P ((FILE *__restrict __stream
,
612 fpos64_t
*__restrict __pos
));
613 extern int fsetpos64
__P ((FILE *__stream
, __const fpos64_t
*__pos
));
617 /* Clear the error and EOF indicators for STREAM. */
618 extern void clearerr
__P ((FILE *__stream
));
619 /* Return the EOF indicator for STREAM. */
620 extern int feof
__P ((FILE *__stream
));
621 /* Return the error indicator for STREAM. */
622 extern int ferror
__P ((FILE *__stream
));
625 /* Faster versions when locking is not required. */
626 extern void clearerr_unlocked
__P ((FILE *__stream
));
627 extern int feof_unlocked
__P ((FILE *__stream
));
628 extern int ferror_unlocked
__P ((FILE *__stream
));
632 feof_unlocked (FILE *__stream
)
634 return _IO_feof_unlocked (__stream
);
638 ferror_unlocked (FILE *__stream
)
640 return _IO_ferror_unlocked (__stream
);
642 # endif /* Optimizing. */
646 /* Print a message describing the meaning of the value of errno. */
647 extern void perror
__P ((__const
char *__s
));
649 /* These variables normally should not be used directly. The `strerror'
650 function provides all the needed functionality. */
653 extern __const
char *__const sys_errlist
[];
656 extern int _sys_nerr
;
657 extern __const
char *__const _sys_errlist
[];
662 /* Return the system file descriptor for STREAM. */
663 extern int fileno
__P ((FILE *__stream
));
664 #endif /* Use POSIX. */
667 /* Faster version when locking is not required. */
668 extern int fileno_unlocked
__P ((FILE *__stream
));
672 #if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \
674 /* Create a new stream connected to a pipe running the given command. */
675 extern FILE *popen
__P ((__const
char *__command
, __const
char *__modes
));
677 /* Close a stream opened by popen and return the status of its child. */
678 extern int pclose
__P ((FILE *__stream
));
683 /* Return the name of the controlling terminal. */
684 extern char *ctermid
__P ((char *__s
));
685 #endif /* Use POSIX. */
689 /* Return the name of the current user. */
690 extern char *cuserid
__P ((char *__s
));
691 #endif /* Use X/Open. */
695 struct obstack
; /* See <obstack.h>. */
697 /* Write formatted output to an obstack. */
698 extern int obstack_printf
__P ((struct obstack
*__obstack
,
699 __const
char *__format
, ...));
700 extern int obstack_vprintf
__P ((struct obstack
*__obstack
,
701 __const
char *__format
,
703 #endif /* Use GNU. */
706 #if defined __USE_POSIX || defined __USE_MISC
707 /* These are defined in POSIX.1:1996. */
709 /* Acquire ownership of STREAM. */
710 extern void flockfile
__P ((FILE *__stream
));
712 /* Try to acquire ownership of STREAM but do not block if it is not
714 extern int ftrylockfile
__P ((FILE *__stream
));
716 /* Relinquish the ownership granted for STREAM. */
717 extern void funlockfile
__P ((FILE *__stream
));
718 #endif /* POSIX || misc */
720 #if defined __USE_XOPEN && !defined __USE_GNU
721 /* The X/Open standard requires some functions and variables to be
722 declared here which do not belong into this header. But we have to
723 follow. In GNU mode we don't do this nonsense. */
725 /* For more information on these symbols look in <getopt.h>. */
731 extern int getopt
__P ((int __argc
, char *__const
*__argv
,
732 __const
char *__shortopts
));
737 /* Define helper macro. */
738 #undef __STDIO_INLINE
740 #endif /* <stdio.h> included. */
742 #endif /* !_STDIO_H */