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));
148 extern FILE *tmpfile __P ((void)) __asm__ ("tmpfile64");
150 #ifdef __USE_LARGEFILE64
151 extern FILE *tmpfile64 __P ((void));
153 /* Generate a temporary filename. */
154 extern char *tmpnam __P ((char *__s));
157 /* This is the reentrant variant of `tmpnam'. The only difference is
158 that it does not allow S to be NULL. */
159 extern char *tmpnam_r __P ((char *__s));
163 #if defined __USE_SVID || defined __USE_XOPEN
164 /* Generate a unique temporary filename using up to five characters of PFX
165 if it is not NULL. The directory to put this file in is searched for
166 as follows: First the environment variable "TMPDIR" is checked.
167 If it contains the name of a writable directory, that directory is used.
168 If not and if DIR is not NULL, that value is checked. If that fails,
169 P_tmpdir is tried and finally "/tmp". The storage for the filename
170 is allocated by `malloc'. */
171 extern char *tempnam __P ((__const char *__dir, __const char *__pfx));
176 extern int fclose __P ((FILE *__stream));
177 /* Flush STREAM, or all streams if STREAM is NULL. */
178 extern int fflush __P ((FILE *__stream));
181 /* Faster versions when locking is not required. */
182 extern int fclose_unlocked __P ((FILE *__stream));
183 extern int fflush_unlocked __P ((FILE *__stream));
187 /* Close all streams. */
188 extern int __fcloseall __P ((void));
189 extern int fcloseall __P ((void));
193 /* Open a file and create a new stream for it. */
194 #ifndef __USE_FILE_OFFSET64
195 extern FILE *fopen __P ((__const char *__restrict __filename,
196 __const char *__restrict __modes));
198 extern FILE *fopen __P ((__const char *__restrict __filename,
199 __const char *__restrict __modes))
202 #ifdef __USE_LARGEFILE64
203 extern FILE *fopen64 __P ((__const char *__restrict __filename,
204 __const char *__restrict __modes));
206 /* Open a file, replacing an existing stream with it. */
207 #ifndef __USE_FILE_OFFSET64
208 extern FILE *freopen __P ((__const char *__restrict __filename,
209 __const char *__restrict __modes,
210 FILE *__restrict __stream));
212 extern FILE *freopen __P ((__const char *__restrict __filename,
213 __const char *__restrict __modes,
214 FILE *__restrict __stream)) __asm__ ("freopen64");
216 #ifdef __USE_LARGEFILE64
217 extern FILE *freopen64 __P ((__const char *__restrict __filename,
218 __const char *__restrict __modes,
219 FILE *__restrict __stream));
223 /* Create a new stream that refers to an existing system file descriptor. */
224 extern FILE *fdopen __P ((int __fd, __const char *__modes));
228 /* Create a new stream that refers to the given magic cookie,
229 and uses the given functions for input and output. */
230 extern FILE *fopencookie __P ((void *__magic_cookie, __const char *__modes,
231 _IO_cookie_io_functions_t __io_funcs));
233 /* Open a stream that writes into a malloc'd buffer that is expanded as
234 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
235 and the number of characters written on fflush or fclose. */
236 extern FILE *open_memstream __P ((char **__bufloc, size_t *__sizeloc));
240 /* If BUF is NULL, make STREAM unbuffered.
241 Else make it use buffer BUF, of size BUFSIZ. */
242 extern void setbuf __P ((FILE *__restrict __stream, char *__restrict __buf));
243 /* Make STREAM use buffering mode MODE.
244 If BUF is not NULL, use N bytes of it for buffering;
245 else allocate an internal buffer N bytes long. */
246 extern int setvbuf __P ((FILE *__restrict __stream, char *__restrict __buf,
247 int __modes, size_t __n));
250 /* If BUF is NULL, make STREAM unbuffered.
251 Else make it use SIZE bytes of BUF for buffering. */
252 extern void setbuffer __P ((FILE *__stream, char *__buf, size_t __size));
254 /* Make STREAM line-buffered. */
255 extern void setlinebuf __P ((FILE *__stream));
259 /* Write formatted output to STREAM. */
260 extern int fprintf __P ((FILE *__restrict __stream,
261 __const char *__restrict __format, ...));
262 /* Write formatted output to stdout. */
263 extern int printf __P ((__const char *__restrict __format, ...));
264 /* Write formatted output to S. */
265 extern int sprintf __P ((char *__restrict __s,
266 __const char *__restrict __format, ...));
268 /* Write formatted output to S from argument list ARG. */
269 extern int vfprintf __P ((FILE *__restrict __s,
270 __const char *__restrict __format,
272 /* Write formatted output to stdout from argument list ARG. */
273 extern int vprintf __P ((__const char *__restrict __format,
275 /* Write formatted output to S from argument list ARG. */
276 extern int vsprintf __P ((char *__restrict __s,
277 __const char *__restrict __format,
282 vprintf (__const char *__restrict __fmt, _G_va_list __arg)
284 return vfprintf (stdout, __fmt, __arg);
286 #endif /* Optimizing. */
288 #if defined __USE_BSD || defined __USE_ISOC9X || defined __USE_UNIX98
289 /* Maximum chars of output to write in MAXLEN. */
290 extern int __snprintf __P ((char *__restrict __s, size_t __maxlen,
291 __const char *__restrict __format, ...))
292 __attribute__ ((__format__ (__printf__, 3, 4)));
293 extern int snprintf __P ((char *__restrict __s, size_t __maxlen,
294 __const char *__restrict __format, ...))
295 __attribute__ ((__format__ (__printf__, 3, 4)));
297 extern int __vsnprintf __P ((char *__restrict __s, size_t __maxlen,
298 __const char *__restrict __format,
300 __attribute__ ((__format__ (__printf__, 3, 0)));
301 extern int vsnprintf __P ((char *__restrict __s, size_t __maxlen,
302 __const char *__restrict __format,
304 __attribute__ ((__format__ (__printf__, 3, 0)));
308 /* Write formatted output to a string dynamically allocated with `malloc'.
309 Store the address of the string in *PTR. */
310 extern int vasprintf __P ((char **__restrict __ptr,
311 __const char *__restrict __f, _G_va_list __arg))
312 __attribute__ ((__format__ (__printf__, 2, 0)));
313 extern int asprintf __P ((char **__restrict __ptr,
314 __const char *__restrict __fmt, ...))
315 __attribute__ ((__format__ (__printf__, 2, 3)));
317 /* Write formatted output to a file descriptor. */
318 extern int vdprintf __P ((int __fd, __const char *__restrict __fmt,
320 __attribute__ ((__format__ (__printf__, 2, 0)));
321 extern int dprintf __P ((int __fd, __const char *__restrict __fmt, ...))
322 __attribute__ ((__format__ (__printf__, 2, 3)));
326 /* Read formatted input from STREAM. */
327 extern int fscanf __P ((FILE *__restrict __stream,
328 __const char *__restrict __format, ...));
329 /* Read formatted input from stdin. */
330 extern int scanf __P ((__const char *__restrict __format, ...));
331 /* Read formatted input from S. */
332 extern int sscanf __P ((__const char *__restrict __s,
333 __const char *__restrict __format, ...));
336 /* Read formatted input from S into argument list ARG. */
337 extern int __vfscanf __P ((FILE *__restrict __s,
338 __const char *__restrict __format,
340 __attribute__ ((__format__ (__scanf__, 2, 0)));
341 extern int vfscanf __P ((FILE *__restrict __s,
342 __const char *__restrict __format,
344 __attribute__ ((__format__ (__scanf__, 2, 0)));
346 /* Read formatted input from stdin into argument list ARG. */
347 extern int __vscanf __P ((__const char *__restrict __format,
349 __attribute__ ((__format__ (__scanf__, 1, 0)));
350 extern int vscanf __P ((__const char *__restrict __format, _G_va_list __arg))
351 __attribute__ ((__format__ (__scanf__, 1, 0)));
353 /* Read formatted input from S into argument list ARG. */
354 extern int __vsscanf __P ((__const char *__restrict __s,
355 __const char *__restrict __format,
357 __attribute__ ((__format__ (__scanf__, 2, 0)));
358 extern int vsscanf __P ((__const char *__restrict __s,
359 __const char *__restrict __format,
361 __attribute__ ((__format__ (__scanf__, 2, 0)));
362 #endif /* Use GNU. */
365 /* Read a character from STREAM. */
366 extern int fgetc __P ((FILE *__stream));
367 extern int getc __P ((FILE *__stream));
369 /* Read a character from stdin. */
370 extern int getchar __P ((void));
372 /* The C standard explicitly says this is a macro, so we always do the
373 optimization for it. */
374 #define getc(_fp) _IO_getc (_fp)
380 return _IO_getc (stdin);
382 #endif /* Optimizing. */
384 #if defined __USE_POSIX || defined __USE_MISC
385 /* These are defined in POSIX.1:1996. */
386 extern int getc_unlocked __P ((FILE *__stream));
387 extern int getchar_unlocked __P ((void));
391 getc_unlocked (FILE *__fp)
393 return _IO_getc_unlocked (__fp);
397 getchar_unlocked (void)
399 return _IO_getc_unlocked (stdin);
401 # endif /* Optimizing. */
402 #endif /* Use POSIX or MISC. */
405 /* Write a character to STREAM. */
406 extern int fputc __P ((int __c, FILE *__stream));
407 extern int putc __P ((int __c, FILE *__stream));
409 /* Write a character to stdout. */
410 extern int putchar __P ((int __c));
412 /* The C standard explicitly says this can be a macro,
413 so we always do the optimization for it. */
414 #define putc(_ch, _fp) _IO_putc (_ch, _fp)
420 return _IO_putc (__c, stdout);
425 /* Faster version when locking is not necessary. */
426 extern int fputc_unlocked __P ((int __c, FILE *__stream));
430 fputc_unlocked (int __c, FILE *__stream)
432 return _IO_putc_unlocked (__c, __stream);
434 # endif /* Optimizing. */
435 #endif /* Use MISC. */
437 #if defined __USE_POSIX || defined __USE_MISC
438 /* These are defined in POSIX.1:1996. */
439 extern int putc_unlocked __P ((int __c, FILE *__stream));
440 extern int putchar_unlocked __P ((int __c));
444 putc_unlocked (int __c, FILE *__stream)
446 return _IO_putc_unlocked (__c, __stream);
450 putchar_unlocked (int __c)
452 return _IO_putc_unlocked (__c, stdout);
454 # endif /* Optimizing. */
455 #endif /* Use POSIX or MISc. */
458 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
459 /* Get a word (int) from STREAM. */
460 extern int getw __P ((FILE *__stream));
462 /* Write a word (int) to STREAM. */
463 extern int putw __P ((int __w, FILE *__stream));
467 /* Get a newline-terminated string of finite length from STREAM. */
468 extern char *fgets __P ((char *__restrict __s, int __n,
469 FILE *__restrict __stream));
471 /* Get a newline-terminated string from stdin, removing the newline.
472 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
473 extern char *gets __P ((char *__s));
477 /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
478 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
479 NULL), pointing to *N characters of space. It is realloc'd as
480 necessary. Returns the number of characters read (not including the
481 null terminator), or -1 on error or EOF. */
482 _IO_ssize_t __getdelim __P ((char **__lineptr, size_t *__n,
483 int __delimiter, FILE *__stream));
484 _IO_ssize_t getdelim __P ((char **__lineptr, size_t *__n,
485 int __delimiter, FILE *__stream));
487 /* Like `getdelim', but reads up to a newline. */
488 _IO_ssize_t __getline __P ((char **__lineptr, size_t *__n, FILE *__stream));
489 _IO_ssize_t getline __P ((char **__lineptr, size_t *__n, FILE *__stream));
492 __STDIO_INLINE _IO_ssize_t
493 getline (char **__lineptr, size_t *__n, FILE *__stream)
495 return __getdelim (__lineptr, __n, '\n', __stream);
497 # endif /* Optimizing. */
501 /* Write a string to STREAM. */
502 extern int fputs __P ((__const char *__restrict __s,
503 FILE *__restrict __stream));
504 /* Write a string, followed by a newline, to stdout. */
505 extern int puts __P ((__const char *__s));
508 /* Push a character back onto the input buffer of STREAM. */
509 extern int ungetc __P ((int __c, FILE *__stream));
512 /* Read chunks of generic data from STREAM. */
513 extern size_t fread __P ((void *__restrict __ptr, size_t __size,
514 size_t __n, FILE *__restrict __stream));
515 /* Write chunks of generic data to STREAM. */
516 extern size_t fwrite __P ((__const void *__restrict __ptr, size_t __size,
517 size_t __n, FILE *__restrict __s));
520 /* Faster versions when locking is not necessary. */
521 extern size_t fread_unlocked __P ((void *__restrict __ptr, size_t __size,
522 size_t __n, FILE *__restrict __stream));
523 extern size_t fwrite_unlocked __P ((__const void *__restrict __ptr,
524 size_t __size, size_t __n,
525 FILE *__restrict __stream));
529 /* Seek to a certain position on STREAM. */
530 extern int fseek __P ((FILE *__stream, long int __off, int __whence));
531 /* Return the current position of STREAM. */
532 extern long int ftell __P ((FILE *__stream));
533 /* Rewind to the beginning of STREAM. */
534 extern void rewind __P ((FILE *__stream));
536 #if (defined __USE_LARGEFILE || defined __USE_LARGEFILE64 \
537 || defined __USE_FILE_OFFSET64)
538 /* The Single Unix Specification, Version 2, specifies an alternative,
539 more adequate interface for the two functions above which deal with
540 file offset. `long int' is not the right type. These definitions
541 are originally defined in the Large File Support API. */
543 /* Types needed in these functions. */
545 # ifndef __USE_FILE_OFFSET64
546 typedef __off_t off_t;
548 typedef __off64_t off_t;
553 # if defined __USE_LARGEFILE64 && !defined off64_t
554 typedef __off64_t off64_t;
555 # define off64_t off64_t
558 /* Seek to a certain position on STREAM. */
559 # ifndef __USE_FILE_OFFSET64
560 extern int fseeko __P ((FILE *__stream, __off_t __off, int __whence));
562 extern int fseeko __P ((FILE *__stream, __off64_t __off, int __whence))
563 __asm__ ("fseeko64");
565 # ifdef __USE_LARGEFILE64
566 extern int fseeko64 __P ((FILE *__stream, __off64_t __off, int __whence));
569 /* Return the current position of STREAM. */
570 # ifndef __USE_FILE_OFFSET64
571 extern __off_t ftello __P ((FILE *__stream));
573 extern __off64_t ftello __P ((FILE *__stream)) __asm__ ("ftello64");
575 # ifdef __USE_LARGEFILE64
576 extern __off64_t ftello64 __P ((FILE *__stream));
580 /* Get STREAM's position. */
581 #ifndef __USE_FILE_OFFSET64
582 extern int fgetpos __P ((FILE *__restrict __stream,
583 fpos_t *__restrict __pos));
585 extern int fgetpos __P ((FILE *__restrict __stream,
586 fpos_t *__restrict __pos)) __asm__ ("fgetpos64");
588 #ifdef __USE_LARGEFILE64
589 extern int fgetpos64 __P ((FILE *__restrict __stream,
590 fpos64_t *__restrict __pos));
592 /* Set STREAM's position. */
593 #ifndef __USE_FILE_OFFSET64
594 extern int fsetpos __P ((FILE *__stream, __const fpos_t *__pos));
596 extern int fsetpos __P ((FILE *__stream, __const fpos_t *__pos))
597 __asm__ ("fsetpos64");
599 #ifdef __USE_LARGEFILE64
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 */