1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
20 * ANSI Standard: 4.9 INPUT/OUTPUT <stdio.h>
25 #if !defined(__need_FILE)
35 #define __need___va_list
37 #ifndef __GNUC_VA_LIST
38 #define __gnuc_va_list __ptr_t
41 #include <gnu/types.h>
42 #endif /* Don't need FILE. */
46 #ifndef __FILE_defined
48 /* The opaque type of streams. */
49 typedef struct __stdio_file
FILE;
51 #define __FILE_defined 1
52 #endif /* FILE not defined. */
57 /* The type of the second argument to `fgetpos' and `fsetpos'. */
58 typedef __off_t
fpos_t;
60 /* The mode of I/O, as given in the MODE argument to fopen, etc. */
63 unsigned int __read
:1; /* Open for reading. */
64 unsigned int __write
:1; /* Open for writing. */
65 unsigned int __append
:1; /* Open for appending. */
66 unsigned int __binary
:1; /* Opened binary. */
67 unsigned int __create
:1; /* Create the file. */
68 unsigned int __exclusive
:1; /* Error if it already exists. */
69 unsigned int __truncate
:1; /* Truncate the file on opening. */
73 /* Functions to do I/O and file management for a stream. */
75 /* Read NBYTES bytes from COOKIE into a buffer pointed to by BUF.
76 Return number of bytes read. */
77 typedef __ssize_t __io_read_fn
__P ((__ptr_t __cookie
, char *__buf
,
80 /* Write N bytes pointed to by BUF to COOKIE. Write all N bytes
81 unless there is an error. Return number of bytes written, or -1 if
82 there is an error without writing anything. If the file has been
83 opened for append (__mode.__append set), then set the file pointer
84 to the end of the file and then do the write; if not, just write at
85 the current file pointer. */
86 typedef __ssize_t __io_write_fn
__P ((__ptr_t __cookie
, __const
char *__buf
,
89 /* Move COOKIE's file position to *POS bytes from the
90 beginning of the file (if W is SEEK_SET),
91 the current position (if W is SEEK_CUR),
92 or the end of the file (if W is SEEK_END).
93 Set *POS to the new file position.
94 Returns zero if successful, nonzero if not. */
95 typedef int __io_seek_fn
__P ((__ptr_t __cookie
, fpos_t *__pos
, int __w
));
98 typedef int __io_close_fn
__P ((__ptr_t __cookie
));
100 /* Return the file descriptor associated with COOKIE,
101 or -1 on error. There need not be any associated file descriptor. */
102 typedef int __io_fileno_fn
__P ((__ptr_t __cookie
));
105 /* User-visible names for the above. */
106 typedef __io_read_fn cookie_read_function_t
;
107 typedef __io_write_fn cookie_write_function_t
;
108 typedef __io_seek_fn cookie_seek_function_t
;
109 typedef __io_close_fn cookie_close_function_t
;
110 typedef __io_fileno_fn cookie_fileno_function_t
;
113 /* Low level interface, independent of FILE representation. */
114 #if defined (__USE_GNU) && !defined (_LIBC)
115 /* Define the user-visible type, with user-friendly member names. */
118 __io_read_fn
*read
; /* Read bytes. */
119 __io_write_fn
*write
; /* Write bytes. */
120 __io_seek_fn
*seek
; /* Seek/tell file position. */
121 __io_close_fn
*close
; /* Close file. */
122 __io_fileno_fn
*fileno
; /* Return file descriptor. */
123 } cookie_io_functions_t
;
124 /* This name is still used in the prototypes in this file. */
125 typedef cookie_io_functions_t __io_functions
;
127 /* Stick to ANSI-safe names. */
130 __io_read_fn
*__read
; /* Read bytes. */
131 __io_write_fn
*__write
; /* Write bytes. */
132 __io_seek_fn
*__seek
; /* Seek/tell file position. */
133 __io_close_fn
*__close
; /* Close file. */
134 __io_fileno_fn
*__fileno
; /* Return file descriptor. */
138 /* Higher level interface, dependent on FILE representation. */
141 /* Make room in the input buffer. */
142 int (*__input
) __P ((FILE *__stream
));
143 /* Make room in the output buffer. */
144 void (*__output
) __P ((FILE *__stream
, int __c
));
147 extern __const __io_functions __default_io_functions
;
148 extern __const __room_functions __default_room_functions
;
151 /* Default close function. */
152 extern __io_close_fn __stdio_close
;
153 /* Open FILE with mode M, store cookie in *COOKIEPTR. */
154 extern int __stdio_open
__P ((__const
char *__file
, __io_mode __m
,
155 __ptr_t
*__cookieptr
));
156 /* Put out an error message for when stdio needs to die. */
157 extern void __stdio_errmsg
__P ((__const
char *__msg
, size_t __len
));
158 /* Generate a unique file name (and possibly open it with mode "w+b"). */
159 extern char *__stdio_gen_tempname
__P ((__const
char *__dir
,
163 FILE **__streamptr
));
166 /* Print out MESSAGE on the error output and abort. */
167 extern void __libc_fatal
__P ((__const
char *__message
))
168 __attribute__ ((__noreturn__
));
171 /* The FILE structure. */
174 /* Magic number for validation. Must be negative in open streams
175 for the glue to Unix stdio getc/putc to work.
176 NOTE: stdio/glue.c has special knowledge of these first four members. */
178 #define _IOMAGIC 0xfedabeeb /* Magic number to fill `__magic'. */
179 #define _GLUEMAGIC 0xfeedbabe /* Magic for glued Unix streams. */
181 char *__bufp
; /* Pointer into the buffer. */
182 char *__get_limit
; /* Reading limit. */
183 char *__put_limit
; /* Writing limit. */
185 char *__buffer
; /* Base of buffer. */
186 size_t __bufsize
; /* Size of the buffer. */
187 __ptr_t __cookie
; /* Magic cookie. */
188 __io_mode __mode
; /* File access mode. */
189 __io_functions __io_funcs
; /* I/O functions. */
190 __room_functions __room_funcs
;/* I/O buffer room functions. */
191 fpos_t __offset
; /* Current file position. */
192 fpos_t __target
; /* Target file position. */
193 FILE *__next
; /* Next FILE in the linked list. */
194 char *__pushback_bufp
; /* Old bufp if char pushed back. */
195 unsigned char __pushback
; /* Pushed-back character. */
196 unsigned int __pushed_back
:1; /* A char has been pushed back. */
197 unsigned int __eof
:1; /* End of file encountered. */
198 unsigned int __error
:1; /* Error encountered. */
199 unsigned int __userbuf
:1; /* Buffer from user (should not be freed). */
200 unsigned int __linebuf
:1; /* Flush on newline. */
201 unsigned int __linebuf_active
:1; /* put_limit is not really in use. */
202 unsigned int __seen
:1; /* This stream has been seen. */
203 unsigned int __ispipe
:1; /* Nonzero if opened by popen. */
207 /* All macros used internally by other macros here and by stdio functions begin
208 with `__'. All of these may evaluate their arguments more than once. */
211 /* Nonzero if STREAM is a valid stream.
212 STREAM must be a modifiable lvalue (wow, I got to use that term).
213 See stdio/glue.c for what the confusing bit is about. */
214 #define __validfp(stream) \
216 ({ if (stream->__magic == _GLUEMAGIC) \
217 stream = *((struct { int __magic; FILE **__p; } *) stream)->__p; \
218 stream->__magic == _IOMAGIC; }))
220 /* Clear the error and EOF indicators of STREAM. */
221 #define __clearerr(stream) ((stream)->__error = (stream)->__eof = 0)
223 /* Nuke STREAM, making it unusable but available for reuse. */
224 extern void __invalidate
__P ((FILE *__stream
));
226 /* Make sure STREAM->__offset and STREAM->__target are initialized.
227 Returns 0 if successful, or EOF on
228 error (but doesn't set STREAM->__error). */
229 extern int __stdio_check_offset
__P ((FILE *__stream
));
232 /* The possibilities for the third argument to `setvbuf'. */
233 #define _IOFBF 0x1 /* Full buffering. */
234 #define _IOLBF 0x2 /* Line buffering. */
235 #define _IONBF 0x4 /* No buffering. */
238 /* Default buffer size. */
242 /* End of file character.
243 Some things throughout the library rely on this being -1. */
247 /* The possibilities for the third argument to `fseek'.
248 These values should not be changed. */
249 #define SEEK_SET 0 /* Seek from beginning of file. */
250 #define SEEK_CUR 1 /* Seek from current position. */
251 #define SEEK_END 2 /* Seek from end of file. */
255 /* Default path prefix for `tempnam' and `tmpnam'. */
256 #define P_tmpdir "/usr/tmp"
261 L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
262 TMP_MAX The minimum number of unique filenames generated by tmpnam
263 (and tempnam when it uses tmpnam's name space),
264 or tempnam (the two are separate).
265 L_ctermid How long an array to pass to `ctermid'.
266 L_cuserid How long an array to pass to `cuserid'.
267 FOPEN_MAX Mininum number of files that can be open at once.
268 FILENAME_MAX Maximum length of a filename. */
269 #include <stdio_lim.h>
272 /* All the known streams are in a linked list
273 linked by the `next' field of the FILE structure. */
274 extern FILE *__stdio_head
; /* Head of the list. */
276 /* Standard streams. */
277 extern FILE *stdin
, *stdout
, *stderr
;
278 #ifdef __STRICT_ANSI__
279 /* ANSI says these are macros; satisfy pedants. */
281 #define stdout stdout
282 #define stderr stderr
286 /* Remove file FILENAME. */
287 extern int remove
__P ((__const
char *__filename
));
288 /* Rename file OLD to NEW. */
289 extern int rename
__P ((__const
char *__old
, __const
char *__new
));
292 /* Create a temporary file and open it read/write. */
293 extern FILE *tmpfile
__P ((void));
294 /* Generate a temporary filename. */
295 extern char *tmpnam
__P ((char *__s
));
299 /* Generate a unique temporary filename using up to five characters of PFX
300 if it is not NULL. The directory to put this file in is searched for
301 as follows: First the environment variable "TMPDIR" is checked.
302 If it contains the name of a writable directory, that directory is used.
303 If not and if DIR is not NULL, that value is checked. If that fails,
304 P_tmpdir is tried and finally "/tmp". The storage for the filename
305 is allocated by `malloc'. */
306 extern char *tempnam
__P ((__const
char *__dir
, __const
char *__pfx
));
310 /* This performs actual output when necessary, flushing
311 STREAM's buffer and optionally writing another character. */
312 extern int __flshfp
__P ((FILE *__stream
, int __c
));
315 /* Close STREAM, or all streams if STREAM is NULL. */
316 extern int fclose
__P ((FILE *__stream
));
317 /* Flush STREAM, or all streams if STREAM is NULL. */
318 extern int fflush
__P ((FILE *__stream
));
321 /* Open a file and create a new stream for it. */
322 extern FILE *fopen
__P ((__const
char *__filename
, __const
char *__modes
));
323 /* Open a file, replacing an existing stream with it. */
324 extern FILE *freopen
__P ((__const
char *__filename
,
325 __const
char *__modes
, FILE *__stream
));
327 /* Return a new, zeroed, stream.
328 You must set its cookie and io_mode.
329 The first operation will give it a buffer unless you do.
330 It will also give it the default functions unless you set the `seen' flag.
331 The offset is set to -1, meaning it will be determined by doing a
332 stationary seek. You can set it to avoid the initial tell call.
333 The target is set to -1, meaning it will be set to the offset
334 before the target is needed.
335 Returns NULL if a stream can't be created. */
336 extern FILE *__newstream
__P ((void));
339 /* Create a new stream that refers to an existing system file descriptor. */
340 extern FILE *fdopen
__P ((int __fd
, __const
char *__modes
));
344 /* Create a new stream that refers to the given magic cookie,
345 and uses the given functions for input and output. */
346 extern FILE *fopencookie
__P ((__ptr_t __magic_cookie
, __const
char *__modes
,
347 __io_functions __io_funcs
));
349 /* Create a new stream that refers to a memory buffer. */
350 extern FILE *fmemopen
__P ((__ptr_t __s
, size_t __len
, __const
char *__modes
));
352 /* Open a stream that writes into a malloc'd buffer that is expanded as
353 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
354 and the number of characters written on fflush or fclose. */
355 extern FILE *open_memstream
__P ((char **__bufloc
, size_t *__sizeloc
));
359 /* If BUF is NULL, make STREAM unbuffered.
360 Else make it use buffer BUF, of size BUFSIZ. */
361 extern void setbuf
__P ((FILE *__stream
, char *__buf
));
362 /* Make STREAM use buffering mode MODE.
363 If BUF is not NULL, use N bytes of it for buffering;
364 else allocate an internal buffer N bytes long. */
365 extern int setvbuf
__P ((FILE *__stream
, char *__buf
,
366 int __modes
, size_t __n
));
369 /* If BUF is NULL, make STREAM unbuffered.
370 Else make it use SIZE bytes of BUF for buffering. */
371 extern void setbuffer
__P ((FILE *__stream
, char *__buf
, size_t __size
));
373 /* Make STREAM line-buffered. */
374 extern void setlinebuf
__P ((FILE *__stream
));
378 /* Write formatted output to STREAM. */
379 extern int fprintf
__P ((FILE *__stream
, __const
char *__format
, ...));
380 /* Write formatted output to stdout. */
381 extern int printf
__P ((__const
char *__format
, ...));
382 /* Write formatted output to S. */
383 extern int sprintf
__P ((char *__s
, __const
char *__format
, ...));
385 /* Write formatted output to S from argument list ARG. */
386 extern int vfprintf
__P ((FILE *__s
, __const
char *__format
,
387 __gnuc_va_list __arg
));
388 /* Write formatted output to stdout from argument list ARG. */
389 extern int vprintf
__P ((__const
char *__format
, __gnuc_va_list __arg
));
390 /* Write formatted output to S from argument list ARG. */
391 extern int vsprintf
__P ((char *__s
, __const
char *__format
,
392 __gnuc_va_list __arg
));
396 vprintf (const char *__fmt
, __gnuc_va_list __arg
)
398 return vfprintf (stdout
, __fmt
, __arg
);
400 #endif /* Optimizing. */
403 /* Maximum chars of output to write in MAXLEN. */
404 extern int __snprintf
__P ((char *__s
, size_t __maxlen
,
405 __const
char *__format
, ...));
406 extern int snprintf
__P ((char *__s
, size_t __maxlen
,
407 __const
char *__format
, ...));
409 extern int __vsnprintf
__P ((char *__s
, size_t __maxlen
,
410 __const
char *__format
, __gnuc_va_list __arg
));
411 extern int vsnprintf
__P ((char *__s
, size_t __maxlen
,
412 __const
char *__format
, __gnuc_va_list __arg
));
414 /* Write formatted output to a string dynamically allocated with `malloc'.
415 Store the address of the string in *PTR. */
416 extern int vasprintf
__P ((char **__ptr
, __const
char *__f
,
417 __gnuc_va_list __arg
));
418 extern int asprintf
__P ((char **__ptr
, __const
char *__fmt
, ...));
420 /* Write formatted output to a file descriptor. */
421 extern int vdprintf
__P ((int __fd
, __const
char *__fmt
,
422 __gnuc_va_list __arg
));
423 extern int dprintf
__P ((int __fd
, __const
char *__fmt
, ...));
427 /* Read formatted input from STREAM. */
428 extern int fscanf
__P ((FILE *__stream
, __const
char *__format
, ...));
429 /* Read formatted input from stdin. */
430 extern int scanf
__P ((__const
char *__format
, ...));
431 /* Read formatted input from S. */
432 extern int sscanf
__P ((__const
char *__s
, __const
char *__format
, ...));
435 /* Read formatted input from S into argument list ARG. */
436 extern int __vfscanf
__P ((FILE *__s
, __const
char *__format
,
437 __gnuc_va_list __arg
));
438 extern int vfscanf
__P ((FILE *__s
, __const
char *__format
,
439 __gnuc_va_list __arg
));
441 /* Read formatted input from stdin into argument list ARG. */
442 extern int vscanf
__P ((__const
char *__format
, __gnuc_va_list __arg
));
444 /* Read formatted input from S into argument list ARG. */
445 extern int __vsscanf
__P ((__const
char *__s
, __const
char *__format
,
446 __gnuc_va_list __arg
));
447 extern int vsscanf
__P ((__const
char *__s
, __const
char *__format
,
448 __gnuc_va_list __arg
));
453 vfscanf (FILE *__s
, const char *__fmt
, __gnuc_va_list __arg
)
455 return __vfscanf (__s
, __fmt
, __arg
);
458 vscanf (const char *__fmt
, __gnuc_va_list __arg
)
460 return __vfscanf (stdin
, __fmt
, __arg
);
463 vsscanf (const char *__s
, const char *__fmt
, __gnuc_va_list __arg
)
465 return __vsscanf (__s
, __fmt
, __arg
);
467 #endif /* Optimizing. */
468 #endif /* Use GNU. */
471 /* This does actual reading when necessary, filling STREAM's
472 buffer and returning the first character in it. */
473 extern int __fillbf
__P ((FILE *__stream
));
476 /* Read a character from STREAM. */
477 extern int fgetc
__P ((FILE *__stream
));
478 extern int getc
__P ((FILE *__stream
));
480 /* Read a character from stdin. */
481 extern int getchar
__P ((void));
483 /* The C standard explicitly says this can
484 re-evaluate its argument, so it does. */
485 #define __getc(stream) \
486 ((stream)->__bufp < (stream)->__get_limit ? \
487 (int) ((unsigned char) *(stream)->__bufp++) : __fillbf(stream))
489 /* The C standard explicitly says this is a macro,
490 so we always do the optimization for it. */
491 #define getc(stream) __getc(stream)
497 return __getc (stdin
);
499 #endif /* Optimizing. */
502 /* Write a character to STREAM. */
503 extern int fputc
__P ((int __c
, FILE *__stream
));
504 extern int putc
__P ((int __c
, FILE *__stream
));
506 /* Write a character to stdout. */
507 extern int putchar
__P ((int __c
));
510 /* The C standard explicitly says this can
511 re-evaluate its arguments, so it does. */
512 #define __putc(c, stream) \
513 ((stream)->__bufp < (stream)->__put_limit ? \
514 (int) (unsigned char) (*(stream)->__bufp++ = (unsigned char) (c)) : \
515 __flshfp ((stream), (unsigned char) (c)))
517 /* The C standard explicitly says this can be a macro,
518 so we always do the optimization for it. */
519 #define putc(c, stream) __putc ((c), (stream))
525 return __putc (__c
, stdout
);
530 #if defined(__USE_SVID) || defined(__USE_MISC)
531 /* Get a word (int) from STREAM. */
532 extern int getw
__P ((FILE *__stream
));
534 /* Write a word (int) to STREAM. */
535 extern int putw
__P ((int __w
, FILE *__stream
));
539 /* Get a newline-terminated string of finite length from STREAM. */
540 extern char *fgets
__P ((char *__s
, int __n
, FILE *__stream
));
542 /* Get a newline-terminated string from stdin, removing the newline.
543 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
544 extern char *gets
__P ((char *__s
));
548 #include <sys/types.h>
550 /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
551 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
552 NULL), pointing to *N characters of space. It is realloc'd as
553 necessary. Returns the number of characters read (not including the
554 null terminator), or -1 on error or EOF. */
555 ssize_t __getdelim
__P ((char **__lineptr
, size_t *__n
,
556 int __delimiter
, FILE *__stream
));
557 ssize_t getdelim
__P ((char **__lineptr
, size_t *__n
,
558 int __delimiter
, FILE *__stream
));
560 /* Like `getdelim', but reads up to a newline. */
561 ssize_t __getline
__P ((char **__lineptr
, size_t *__n
, FILE *__stream
));
562 ssize_t getline
__P ((char **__lineptr
, size_t *__n
, FILE *__stream
));
565 extern __inline ssize_t
566 __getline (char **__lineptr
, size_t *__n
, FILE *__stream
)
568 return __getdelim (__lineptr
, __n
, '\n', __stream
);
571 extern __inline ssize_t
572 getdelim (char **__lineptr
, size_t *__n
, int __delimiter
, FILE *__stream
)
574 return __getdelim (__lineptr
, __n
, __delimiter
, __stream
);
576 extern __inline ssize_t
577 getline (char **__lineptr
, size_t *__n
, FILE *__stream
)
579 return __getline (__lineptr
, __n
, __stream
);
581 #endif /* Optimizing. */
585 /* Write a string to STREAM. */
586 extern int fputs
__P ((__const
char *__s
, FILE *__stream
));
587 /* Write a string, followed by a newline, to stdout. */
588 extern int puts
__P ((__const
char *__s
));
591 /* Push a character back onto the input buffer of STREAM. */
592 extern int ungetc
__P ((int __c
, FILE *__stream
));
595 /* Read chunks of generic data from STREAM. */
596 extern size_t fread
__P ((__ptr_t __ptr
, size_t __size
,
597 size_t __n
, FILE *__stream
));
598 /* Write chunks of generic data to STREAM. */
599 extern size_t fwrite
__P ((__const __ptr_t __ptr
, size_t __size
,
600 size_t __n
, FILE *__s
));
603 /* Seek to a certain position on STREAM. */
604 extern int fseek
__P ((FILE *__stream
, long int __off
, int __whence
));
605 /* Return the current position of STREAM. */
606 extern long int ftell
__P ((FILE *__stream
));
607 /* Rewind to the beginning of STREAM. */
608 extern void rewind
__P ((FILE *__stream
));
610 /* Get STREAM's position. */
611 extern int fgetpos
__P ((FILE *__stream
, fpos_t *__pos
));
612 /* Set STREAM's position. */
613 extern int fsetpos
__P ((FILE *__stream
, __const
fpos_t *__pos
));
616 /* Clear the error and EOF indicators for STREAM. */
617 extern void clearerr
__P ((FILE *__stream
));
618 /* Return the EOF indicator for STREAM. */
619 extern int feof
__P ((FILE *__stream
));
620 /* Return the error indicator for STREAM. */
621 extern int ferror
__P ((FILE *__stream
));
624 #define feof(stream) ((stream)->__eof != 0)
625 #define ferror(stream) ((stream)->__error != 0)
626 #endif /* Optimizing. */
629 /* Print a message describing the meaning of the value of errno. */
630 extern void perror
__P ((__const
char *__s
));
634 extern const char *const sys_errlist
[];
637 extern int _sys_nerr
;
638 extern const char *const _sys_errlist
[];
643 /* Return the system file descriptor for STREAM. */
644 extern int fileno
__P ((FILE *__stream
));
645 #endif /* Use POSIX. */
648 #if (defined (__USE_POSIX2) || defined(__USE_SVID) || defined(__USE_BSD) || \
650 /* Create a new stream connected to a pipe running the given command. */
651 extern FILE *popen
__P ((__const
char *__command
, __const
char *__modes
));
653 /* Close a stream opened by popen and return the status of its child. */
654 extern int pclose
__P ((FILE *__stream
));
659 /* Return the name of the controlling terminal. */
660 extern char *ctermid
__P ((char *__s
));
661 /* Return the name of the current user. */
662 extern char *cuserid
__P ((char *__s
));
667 struct obstack
; /* See <obstack.h>. */
669 /* Open a stream that writes to OBSTACK. */
670 extern FILE *open_obstack_stream
__P ((struct obstack
*__obstack
));
672 /* Write formatted output to an obstack. */
673 extern int obstack_printf
__P ((struct obstack
*__obstack
,
674 __const
char *__format
, ...));
675 extern int obstack_vprintf
__P ((struct obstack
*__obstack
,
676 __const
char *__format
,
677 __gnuc_va_list __args
));
683 #endif /* <stdio.h> included. */