1 /* Copyright (C) 1991, 1992 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 ____va_list_defined
38 #define __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 __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 __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 __P ((__ptr_t __cookie, fpos_t * __pos, int __w));
98 typedef int __io_close __P ((__ptr_t __cookie));
100 /* Low level interface, independent of FILE representation. */
103 __io_read *__read; /* Read bytes. */
104 __io_write *__write; /* Write bytes. */
105 __io_seek *__seek; /* Seek/tell file position. */
106 __io_close *__close; /* Close file. */
109 /* Higher level interface, dependent on FILE representation. */
112 /* Make room in the input buffer. */
113 int (*__input) __P ((FILE * __stream));
114 /* Make room in the output buffer. */
115 void (*__output) __P ((FILE * __stream, int __c));
118 extern __const __io_functions __default_io_functions;
119 extern __const __room_functions __default_room_functions;
122 /* Default close function. */
123 extern __io_close __stdio_close;
124 /* Open FILE with mode M, return cookie or NULL to use an int in *DP. */
125 extern __ptr_t __stdio_open __P ((__const char *__file, __io_mode __m,
127 /* Put out an error message for when stdio needs to die. */
128 extern void __stdio_errmsg __P ((__const char *__msg, size_t __len));
129 /* Generate a unique file name. */
130 extern char *__stdio_gen_tempname __P ((__const char *__dir,
132 int __dir_search, size_t * __lenptr));
136 #define __NORETURN __volatile
140 #endif /* __NORETURN not defined. */
142 /* Print out MESSAGE on the error output and abort. */
143 extern __NORETURN void __libc_fatal __P ((__const char *__message));
146 /* The FILE structure. */
149 /* Magic number for validation. Must be negative in open streams
150 for the glue to Unix stdio getc/putc to work. */
152 #define _IOMAGIC 0xfedabeeb /* Magic number to fill `__magic'. */
153 #define _GLUEMAGIC 0xfeedbabe /* Magic for glued Unix streams. */
155 char *__bufp; /* Pointer into the buffer. */
156 char *__get_limit; /* Reading limit. */
157 char *__put_limit; /* Writing limit. */
158 char *__buffer; /* Base of buffer. */
159 size_t __bufsize; /* Size of the buffer. */
160 FILE *__next; /* Next FILE in the linked list. */
161 __ptr_t __cookie; /* Magic cookie. */
162 int __fileno; /* System file descriptor. */
163 unsigned char __pushback; /* Pushed-back character. */
164 char *__pushback_bufp; /* Old bufp if char pushed back. */
165 unsigned int __pushed_back:1; /* A char has been pushed back. */
166 unsigned int __eof:1; /* End of file encountered. */
167 unsigned int __error:1; /* Error encountered. */
168 unsigned int __userbuf:1; /* Buffer is from user. */
169 unsigned int __linebuf:1; /* Flush on newline. */
170 unsigned int __seen:1; /* This stream has been seen. */
171 unsigned int __ispipe:1; /* Nonzero if opened by popen. */
172 __io_mode __mode; /* File access mode. */
173 __io_functions __io_funcs; /* I/O functions. */
174 __room_functions __room_funcs;/* I/O buffer room functions. */
175 fpos_t __offset; /* Current file position. */
176 fpos_t __target; /* Target file position. */
180 /* All macros used internally by other macros here and by stdio functions begin
181 with `__'. All of these may evaluate their arguments more than once. */
184 /* Nonzero if STREAM is a valid stream. */
185 #define __validfp(stream) \
186 (stream != NULL && ((stream->__magic == _GLUEMAGIC && \
187 (stream = (FILE *) &((int *) stream)[1])), \
188 (stream->__magic == _IOMAGIC))) \
190 /* Clear the error and EOF indicators of STREAM. */
191 #define __clearerr(stream) ((stream)->__error = (stream)->__eof = 0)
193 /* Nuke STREAM, making it unusable but available for reuse. */
194 extern void __invalidate __P ((FILE * __stream));
196 /* Make sure STREAM->__offset and STREAM->__target are initialized.
197 Returns 0 if successful, or EOF on
198 error (but doesn't set STREAM->__error). */
199 extern int __stdio_check_offset __P ((FILE * __stream));
202 /* The possibilities for the third argument to `setvbuf'. */
203 #define _IOFBF 0x1 /* Full buffering. */
204 #define _IOLBF 0x2 /* Line buffering. */
205 #define _IONBF 0x4 /* No buffering. */
208 /* Default buffer size. */
212 /* End of file character.
213 Some things throughout the library rely on this being -1. */
217 /* The possibilities for the third argument to `fseek'.
218 These values should not be changed. */
219 #define SEEK_SET 0 /* Seek from beginning of file. */
220 #define SEEK_CUR 1 /* Seek from current position. */
221 #define SEEK_END 2 /* Seek from end of file. */
225 /* Default path prefix for `tempnam' and `tmpnam'. */
226 #define P_tmpdir "/usr/tmp"
231 L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
232 TMP_MAX The minimum number of unique filenames generated by tmpnam
233 (and tempnam when it uses tmpnam's name space),
234 or tempnam (the two are separate).
235 L_ctermid How long an array to pass to `ctermid'.
236 L_cuserid How long an array to pass to `cuserid'.
237 FOPEN_MAX Mininum number of files that can be open at once.
238 FILENAME_MAX Maximum length of a filename. */
239 #include <stdio_lim.h>
242 /* All the known streams are in a linked list
243 linked by the `next' field of the FILE structure. */
244 extern FILE *__stdio_head; /* Head of the list. */
246 /* Standard streams. */
247 extern FILE *stdin, *stdout, *stderr;
250 /* Remove file FILENAME. */
251 extern int remove __P ((__const char *__filename));
252 /* Rename file OLD to NEW. */
253 extern int rename __P ((__const char *__old, __const char *__new));
256 /* Create a temporary file and open it read/write. */
257 extern FILE *tmpfile __P ((void));
258 /* Generate a temporary filename. */
259 extern char *tmpnam __P ((char *__s));
263 /* Generate a unique temporary filename using up to five characters of PFX
264 if it is not NULL. The directory to put this file in is searched for
265 as follows: First the environment variable "TMPDIR" is checked.
266 If it contains the name of a writable directory, that directory is used.
267 If not and if DIR is not NULL, that value is checked. If that fails,
268 P_tmpdir is tried and finally "/tmp". The storage for the filename
269 is allocated by `malloc'. */
270 extern char *tempnam __P ((__const char *__dir, __const char *__pfx));
274 /* This performs actual output when necessary, flushing
275 STREAM's buffer and optionally writing another character. */
276 extern int __flshfp __P ((FILE * __stream, int __c));
279 /* Close STREAM, or all streams if STREAM is NULL. */
280 extern int fclose __P ((FILE * __stream));
281 /* Flush STREAM, or all streams if STREAM is NULL. */
282 extern int fflush __P ((FILE * __stream));
285 /* Open a file and create a new stream for it. */
286 extern FILE *fopen __P ((__const char *__filename, __const char *__modes));
287 /* Open a file, replacing an existing stream with it. */
288 extern FILE *freopen __P ((__const char *__filename,
289 __const char *__modes, FILE * __stream));
291 /* Return a new, zeroed, stream.
292 You must set its cookie and io_mode.
293 The first operation will give it a buffer unless you do.
294 It will also give it the default functions unless you set the `seen' flag.
295 The offset is set to -1, meaning it will be determined by doing a
296 stationary seek. You can set it to avoid the initial tell call.
297 The target is set to -1, meaning it will be set to the offset
298 before the target is needed.
299 Returns NULL if a stream can't be created. */
300 extern FILE *__newstream __P ((void));
303 /* Create a new stream that refers to an existing system file descriptor. */
304 extern FILE *fdopen __P ((int __fd, __const char *__modes));
308 /* Create a new stream that refers to the given magic cookie,
309 and uses the given functions for input and output. */
310 extern FILE *fopencookie __P ((__ptr_t __magic_cookie, __const char *__modes,
311 __io_functions __io_functions));
313 /* Create a new stream that refers to a memory buffer. */
314 extern FILE *fmemopen __P ((__ptr_t __s, size_t __len, __const char *__modes));
316 /* Open a stream that writes into a malloc'd buffer that is expanded as
317 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
318 and the number of characters written on fflush or fclose. */
319 extern FILE *open_memstream __P ((char **__bufloc, size_t * sizeloc));
323 /* If BUF is NULL, make STREAM unbuffered.
324 Else make it use buffer BUF, of size BUFSIZ. */
325 extern void setbuf __P ((FILE * __stream, char *__buf));
326 /* Make STREAM use buffering mode MODE.
327 If BUF is not NULL, use N bytes of it for buffering;
328 else allocate an internal buffer N bytes long. */
329 extern int setvbuf __P ((FILE * __stream, char *__buf,
330 int __modes, size_t __n));
333 /* If BUF is NULL, make STREAM unbuffered.
334 Else make it use SIZE bytes of BUF for buffering. */
335 extern void setbuffer __P ((FILE * __stream, char *__buf, size_t __size));
337 /* Make STREAM line-buffered. */
338 extern void setlinebuf __P ((FILE * __stream));
342 /* Write formatted output to STREAM. */
343 extern int fprintf __P ((FILE * __stream, __const char *__format, ...));
344 /* Write formatted output to stdout. */
345 extern int printf __P ((__const char *__format, ...));
346 /* Write formatted output to S. */
347 extern int sprintf __P ((char *__s, __const char *__format, ...));
349 /* Write formatted output to S from argument list ARG. */
350 extern int vfprintf __P ((FILE * __s, __const char *__format,
352 /* Write formatted output to stdout from argument list ARG. */
353 extern int vprintf __P ((__const char *__format, __ptr_t __arg));
354 /* Write formatted output to S from argument list ARG. */
355 extern int vsprintf __P ((char *__s, __const char *__format, __va_list __arg));
358 #define vprintf(fmt, arg) vfprintf(stdout, (fmt), (arg))
359 #endif /* Optimizing. */
362 /* Maximum chars of output to write in MAXLEN. */
363 extern int snprintf __P ((char *__s, size_t __maxlen,
364 __const char *__format, ...));
366 extern int vsnprintf __P ((char *__s, size_t __maxlen,
367 __const char *__format, __va_list __arg));
369 /* Write formatted output to a string dynamically allocated with `malloc'.
370 Store the address of the string in *PTR. */
371 extern int vasprintf __P ((char **__ptr, __const char *__f, __va_list __arg));
372 extern int asprintf __P ((char **__ptr, __const char *__fmt, ...));
374 /* Write formatted output to a file descriptor. */
375 extern int vdprintf __P ((int __fd, __const char *__fmt, __va_list __arg));
376 extern int dprintf __P ((int __fd, __const char *__fmt, ...));
380 /* Read formatted input from STREAM. */
381 extern int fscanf __P ((FILE * __stream, __const char *__format, ...));
382 /* Read formatted input from stdin. */
383 extern int scanf __P ((__const char *__format, ...));
384 /* Read formatted input from S. */
385 extern int sscanf __P ((__const char *__s, __const char *__format, ...));
388 /* Read formatted input from S into argument list ARG. */
389 extern int __vfscanf __P ((FILE * __s, __const char *__format,
391 extern int vfscanf __P ((FILE * __s, __const char *__format,
394 /* Read formatted input from stdin into argument list ARG. */
395 extern int vscanf __P ((__const char *__format, __va_list __arg));
397 /* Read formatted input from S into argument list ARG. */
398 extern int __vsscanf __P ((__const char *__s, __const char *__format,
400 extern int vsscanf __P ((__const char *__s, __const char *__format,
405 #define vfscanf(s, format, arg) __vfscanf((s), (format), (arg))
406 #define vscanf(format, arg) __vfscanf(stdin, (format), (arg))
407 #define vsscanf(s, format, arg) __vsscanf((s), (format), (arg))
408 #endif /* Optimizing. */
409 #endif /* Use GNU. */
412 /* This does actual reading when necessary, filling STREAM's
413 buffer and returning the first character in it. */
414 extern int __fillbf __P ((FILE * __stream));
417 /* Read a character from STREAM. */
418 extern int fgetc __P ((FILE * __stream));
419 extern int getc __P ((FILE * __stream));
421 /* Read a character from stdin. */
422 extern int getchar __P ((void));
424 /* The C standard explicitly says this can
425 re-evaluate its argument, so it does. */
426 #define __getc(stream) \
427 ((stream)->__bufp < (stream)->__get_limit ? \
428 (int) ((unsigned char) *(stream)->__bufp++) : __fillbf(stream))
430 /* The C standard explicitly says this is a macro,
431 so we always do the optimization for it. */
432 #define getc(stream) __getc(stream)
435 #define getchar() __getc(stdin)
436 #endif /* Optimizing. */
439 /* Write a character to STREAM. */
440 extern int fputc __P ((int __c, FILE * __stream));
441 extern int putc __P ((int __c, FILE * __stream));
443 /* Write a character to stdout. */
444 extern int putchar __P ((int __c));
446 /* The C standard explicitly says this can
447 re-evaluate its arguments, so it does. */
448 #define __putc(c, stream) \
449 ((stream)->__bufp < (stream)->__put_limit ? \
450 (int) (unsigned char) (*(stream)->__bufp++ = (unsigned char) (c)) : \
451 ((stream)->__linebuf && (c) != '\n' && \
452 (stream)->__bufp - (stream)->__buffer < (stream)->__bufsize) ? \
453 (*(stream)->__bufp++ = (unsigned char) (c)) : \
454 __flshfp((stream), (unsigned char) (c)))
456 /* The C standard explicitly says this can be a macro,
457 so we always do the optimization for it. */
458 #define putc(c, stream) __putc((c), (stream))
461 #define putchar(c) __putc((c), stdout)
462 #endif /* Optimizing. */
465 #if defined(__USE_SVID) || defined(__USE_MISC)
466 /* Get a word (int) from STREAM. */
467 extern int getw __P ((FILE * __stream));
469 /* Write a word (int) to STREAM. */
470 extern int putw __P ((int __w, FILE * __stream));
474 /* Get a newline-terminated string of finite length from STREAM. */
475 extern char *fgets __P ((char *__s, size_t __n, FILE * __stream));
477 /* Get a newline-terminated string from stdin, removing the newline.
478 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
479 extern char *gets __P ((char *__s));
483 #include <sys/types.h>
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 ssize_t __getdelim __P ((char **lineptr, size_t * n,
491 int delimiter, FILE * stream));
492 ssize_t getdelim __P ((char **lineptr, size_t * n,
493 int delimiter, FILE * stream));
495 /* Like `getdelim', but reads up to a newline. */
496 ssize_t __getline __P ((char **lineptr, size_t * n, FILE * stream));
497 ssize_t getline __P ((char **lineptr, size_t * n, FILE * stream));
500 #define getdelim(l, n, d, s) __getdelim ((l), (n), (d), (s))
501 #define getline(l, n, s) __getline ((l), (n), (s))
502 #define __getline(l, n, stream) __getdelim ((l), (n), '\n', (stream))
503 #endif /* Optimizing. */
507 /* Write a string to STREAM. */
508 extern int fputs __P ((__const char *__s, FILE * __stream));
509 /* Write a string, followed by a newline, to stdout. */
510 extern int puts __P ((__const char *__s));
513 #define puts(s) ((fputs((s), stdout) || __putc('\n', stdout) == EOF) ? EOF : 0)
514 #endif /* Optimizing. */
517 /* Push a character back onto the input buffer of STREAM. */
518 extern int ungetc __P ((int __c, FILE * __stream));
521 /* Read chunks of generic data from STREAM. */
522 extern size_t fread __P ((__ptr_t __ptr, size_t __size,
523 size_t __n, FILE * __stream));
524 /* Write chunks of generic data to STREAM. */
525 extern size_t fwrite __P ((__const __ptr_t __ptr, size_t __size,
526 size_t __n, FILE * __s));
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 /* Get STREAM's position. */
537 extern int fgetpos __P ((FILE * __stream, fpos_t * __pos));
538 /* Set STREAM's position. */
539 extern int fsetpos __P ((FILE * __stream, __const fpos_t * __pos));
542 /* Clear the error and EOF indicators for STREAM. */
543 extern void clearerr __P ((FILE * __stream));
544 /* Return the EOF indicator for STREAM. */
545 extern int feof __P ((FILE * __stream));
546 /* Return the error indicator for STREAM. */
547 extern int ferror __P ((FILE * __stream));
550 #define feof(stream) ((stream)->__eof != 0)
551 #define ferror(stream) ((stream)->__error != 0)
552 #endif /* Optimizing. */
555 /* Print a message describing the meaning of the value of errno. */
556 extern void perror __P ((__const char *__s));
560 extern char *sys_errlist[];
564 /* Print a message describing the meaning of the given signal number. */
565 extern void psignal __P ((int __sig, __const char *__s));
566 #endif /* Non strict ANSI and not POSIX only. */
570 /* Return the system file descriptor for STREAM. */
571 extern int fileno __P ((__const FILE * __stream));
574 /* The `+ 0' makes this not be an lvalue, so it can't be changed. */
575 #define fileno(stream) ((stream)->__fileno + 0)
576 #endif /* Optimizing. */
578 #endif /* Use POSIX. */
581 #if (defined (__USE_POSIX2) || defined(__USE_SVID) || defined(__USE_BSD) || \
583 /* Create a new stream connected to a pipe running the given command. */
584 extern FILE *popen __P ((__const char *__command, __const char *__modes));
586 /* Close a stream opened by popen and return the status of its child. */
587 extern int pclose __P ((FILE * __stream));
592 /* Return the name of the controlling terminal. */
593 extern char *ctermid __P ((char *__s));
594 /* Return the name of the current user. */
595 extern char *cuserid __P ((char *__s));
600 #endif /* <stdio.h> included. */