1 /* Copyright (C) 1990 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 modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 1, or (at your option)
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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with the GNU C Library; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 * ANSI Standard: 4.9 INPUT/OUTPUT <stdio.h>
24 #if !defined(__need_FILE)
32 #define __need___va_list
35 #define __need___off_t
36 #include <sys/types.h>
37 #endif /* Don't need FILE. */
41 #ifndef __FILE_defined
43 /* The opaque type of streams. */
44 typedef struct __stdio_file FILE;
46 #define __FILE_defined 1
47 #endif /* FILE not defined. */
52 /* The type of the second argument to `fgetpos' and `fsetpos'. */
53 typedef __off_t fpos_t;
55 /* The current direction of I/O: reading, writing, or in between. */
56 typedef enum { __READING, __WRITING, __CHANGING } __io_direction;
59 /* The mode of I/O, as given in the MODE argument to fopen, etc. */
62 unsigned int __read:1; /* Open for reading. */
63 unsigned int __write:1; /* Open for writing. */
64 unsigned int __append:1; /* Open for appending. */
65 unsigned int __bin:1; /* Opened binary. */
66 unsigned int __create:1; /* Create the file. */
67 unsigned int __exclusive:1; /* Error if it already exists. */
68 unsigned int __truncate:1; /* Truncate the file on opening. */
72 /* Functions to do I/O and file management for a stream. */
74 typedef int EXFUN(__io_read, (PTR __cookie, char *__buf, size_t __nbytes));
75 typedef int EXFUN(__io_write, (PTR __cookie, CONST char *__buf, size_t __n));
76 typedef int EXFUN(__io_seek, (PTR __cookie, fpos_t *__pos, int __w));
77 typedef int EXFUN(__io_close, (PTR __cookie));
79 /* Low level interface, independent of FILE representation. */
82 __io_read *__read; /* Read bytes. */
83 __io_write *__write; /* Write bytes. */
84 __io_seek *__seek; /* Seek/tell file position. */
85 __io_close *__close; /* Close file. */
88 /* Higher level interface, dependent on FILE representation. */
91 /* Make room in the input buffer. */
92 int EXFUN((*__input), (FILE *__stream));
93 /* Make room in the output buffer. */
94 void EXFUN((*__output), (FILE *__stream, int __flush_only, int __c));
97 extern CONST __io_functions __default_io_functions;
98 extern CONST __room_functions __default_room_functions;
101 /* Default close function. */
102 extern __io_close __stdio_close;
103 /* Open FILE with mode M, return cookie or NULL to use an int in *DP. */
104 extern PTR EXFUN(__stdio_open, (CONST char *__file, __io_mode __m, int *__dp));
105 /* Decide the type of buffering and buffer size used for COOKIE. */
106 extern void EXFUN(__stdio_linebf, (PTR __cookie, int *__buffering,
107 size_t *__bufsize, __io_functions *__iof,
108 __room_functions *__rf));
109 /* Put out an error message for when stdio needs to die. */
110 extern void EXFUN(__stdio_errmsg, (CONST char *__msg, size_t __len));
111 /* Generate a unique file name. */
112 extern char *EXFUN(__stdio_gen_tempname, (CONST char *__dir, CONST char *__pfx,
113 int __dir_search, size_t *__lenptr));
117 #define __NORETURN __volatile
121 #endif /* __NORETURN not defined. */
123 /* Print out MESSAGE on the error output and abort. */
124 extern __NORETURN void EXFUN(__libc_fatal, (CONST char *__message));
127 /* The FILE structure. */
130 FILE *__next; /* Next FILE in the linked list. */
131 char *__buf; /* Base of buffer. */
132 char *__bp; /* Pointer into the buffer. */
133 char *__end; /* Pointer past end of the buffer. */
134 size_t __bufsz; /* Size of the buffer. */
135 PTR __crunch; /* Magic cookie. */
136 int __fd; /* System file descriptor. */
137 unsigned char __magic; /* Magic number for validation. */
138 #define _IOMAGIC 0xd5 /* Magic number to fill `__magic'. */
139 char __pb_buf[3]; /* Pushback buffer. */
140 size_t __pb_count; /* Count of chars pushed back. */
141 __io_mode __iomode; /* File access mode. */
142 unsigned int __eof:1; /* End of file encountered. */
143 unsigned int __error:1; /* Error encountered. */
144 unsigned int __usrbuf:1; /* Buffer is from user. */
145 unsigned int __lnbuf:1; /* Flush on newline. */
146 unsigned int __beenseen:1; /* This stream has been seen. */
147 unsigned int __isapipe:1; /* Nonzero if opened by popen. */
148 unsigned int __iodir:2; /* Current direction of I/O. */
149 __io_functions __iofuncs; /* I/O functions. */
150 __room_functions __roomfuncs; /* I/O buffer room functions. */
151 fpos_t __off; /* Offset of the buffer. */
155 /* All macros used internally by other macros here and by stdio functions begin
156 with `__'. All of these may evaluate their arguments more than once. */
159 /* Nonzero if STREAM is a valid stream. */
160 #define __validfp(stream) ((stream) != NULL && (stream)->__magic == _IOMAGIC)
163 /* Check to see if I/O for STREAM is going in
164 direction DIR and signal a fatal error if so. */
165 #define __ill_dirch_check(stream, dir) \
166 (__direction(stream) == (dir) && (__stdio_ill_dirch(dir), 0))
168 /* Print a message about an illegal direction change and abort. */
169 extern __NORETURN void EXFUN(__stdio_ill_dirch, (__io_direction __dir));
172 /* This must be called before each function that does output. */
173 #define __beginwrite(stream) \
175 switch (__direction(stream)) \
178 __stdio_ill_dirch(__READING); \
183 /* The buffer is empty. */ \
184 __bufp(stream) = __buffer(stream); \
185 if (__linebuf(stream)) \
186 __endp(stream) = __bufp(stream); \
187 __set_direction(stream, __WRITING); \
192 /* These macros hide the member names of the FILE structure by providing
193 access to their information in the form of pseudo-functions. */
195 #define __direction(stream) ((__io_direction) (stream)->__iodir)
196 #define __set_direction(stream, direction) \
197 ((stream)->__iodir = (unsigned int) (direction))
198 #define __seen(stream) ((stream)->__beenseen)
199 #define __linebuf(stream) ((stream)->__lnbuf)
200 #define __userbuf(stream) ((stream)->__usrbuf)
201 #define __ispipe(stream) ((stream)->__isapipe)
203 #define __mode(stream) ((stream)->__iomode)
204 #define __canread(stream) (__mode(stream).__read)
205 #define __canwrite(stream) (__mode(stream).__write)
206 #define __binary(stream) (__mode(stream).__bin)
208 #define __buffer(stream) ((stream)->__buf)
209 #define __bufp(stream) ((stream)->__bp)
210 #define __bufsize(stream) ((stream)->__bufsz)
211 #define __endp(stream) ((stream)->__end)
213 #define __charsleft(stream) (__endp(stream) - __bufp(stream))
215 #define __feof(stream) ((stream)->__eof)
216 #define __ferror(stream) ((stream)->__error)
218 #define __cookie(stream) ((stream)->__crunch)
219 #define __fileno(stream) ((stream)->__fd)
221 #define __io_funcs(stream) ((stream)->__iofuncs)
222 #define __room_funcs(stream) ((stream)->__roomfuncs)
224 #define __offset(stream) ((stream)->__off)
227 /* Clear the error and EOF indicators of STREAM. */
228 #define __clearerr(stream) (__ferror(stream) = __feof(stream) = 0)
231 /* Set the magic cookie to the fd and the I/O functions to their
232 defaults and set the `seen' flag so it won't be done again. */
233 #define __see(stream) \
234 ((void) (__cookie(stream) = (PTR) &__fileno(stream), \
235 __io_funcs(stream) = __default_io_functions, \
236 __room_funcs(stream) = __default_room_functions, \
240 /* Return one of the standard buffering-method constants,
241 telling which method is used by STREAM. */
242 #define __buffering(stream) \
243 ((__buffer(stream) && __buffer(stream) != (stream)->__pb_buf) ? \
244 (__linebuf(stream) ? _IOLBF : _IOFBF) : \
245 (__userbuf(stream) ? _IONBF : _IOFBF)) \
248 /* Nuke STREAM, making it unusable but available for reuse. */
249 extern void EXFUN(__invalidate, (FILE *__stream));
252 /* The possibilities for the third argument to `setvbuf'. */
253 #define _IOFBF 0x1 /* Full buffering. */
254 #define _IOLBF 0x2 /* Line buffering. */
255 #define _IONBF 0x4 /* No buffering. */
258 /* Default buffer size. */
262 /* End of file character.
263 Some things throughout the library rely on this being -1. */
267 /* The possibilities for the third argument to `fseek'.
268 These values should not be changed. */
269 #define SEEK_SET 0 /* Seek from beginning of file. */
270 #define SEEK_CUR 1 /* Seek from current position. */
271 #define SEEK_END 2 /* Seek from end of file. */
275 /* Default path prefix for `tempnam' and `tmpnam'. */
276 #define P_tmpdir "/usr/tmp"
281 L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
282 TMP_MAX The minimum number of unique filenames generated by tmpnam
283 (and tempnam when it uses tmpnam's name space),
284 or tempnam (the two are separate).
285 L_ctermid How long an array to pass to `ctermid'.
286 L_cuserid How long an array to pass to `cuserid'.
287 FOPEN_MAX Mininum number of files that can be open at once.
288 FILENAME_MAX Maximum length of a filename. */
289 #include <stdio_limits.h>
292 /* All the known streams are in a linked list
293 linked by the `next' field of the FILE structure. */
295 extern FILE *__stdio_head; /* Head of the list. */
296 extern FILE *__stdio_tail; /* Tail of the list. */
298 /* The standard streams (standard input, output, and error) must be
299 defined statically because they are never opened. They are still
300 part of the linked list; _stdio_head is initialized to point to
301 the first of them and _stdio_tail to the last and all of their
302 `next' fields point to the appropriate next address. */
303 extern FILE stdin[1], stdout[1], stderr[1];
306 /* Remove file FILENAME. */
307 extern int EXFUN(remove, (CONST char *__filename));
308 /* Rename file OLD to NEW. */
309 extern int EXFUN(rename, (CONST char *__old, CONST char *__new));
312 /* Create a temporary file and open it read/write. */
313 extern FILE *EXFUN(tmpfile, (NOARGS));
314 /* Generate a temporary filename. */
315 extern char *EXFUN(tmpnam, (char *__s));
319 /* Generate a unique temporary filename using up to five characters of PFX
320 if it is not NULL. The directory to put this file in is searched for
321 as follows: First the environment variable "TMPDIR" is checked.
322 If it contains the name of a writable directory, that directory is used.
323 If not and if DIR is not NULL, that value is checked. If that fails,
324 P_tmpdir is tried and finally "/tmp". The storage for the filename
325 is allocated by `malloc'. */
326 extern char *EXFUN(tempnam, (CONST char *__dir, CONST char *__pfx));
330 /* This performs actual output when necessary, flushing
331 STREAM's buffer and optionally writing another character. */
332 extern int EXFUN(__flshfp, (FILE *__stream, int __c,
336 /* Close STREAM, or all streams if STREAM is NULL. */
337 extern int EXFUN(fclose, (FILE *__stream));
338 /* Flush STREAM, or all streams if STREAM is NULL. */
339 extern int EXFUN(fflush, (FILE *__stream));
342 /* Open a file and create a new stream for it. */
343 extern FILE *EXFUN(fopen, (CONST char *__filename, CONST char *__modes));
344 /* Open a file, replacing an existing stream with it. */
345 extern FILE *EXFUN(freopen, (CONST char *__filename,
346 CONST char *__modes, FILE *__stream));
348 /* Create a new stream that refers to the given magic cookie,
349 and uses the given functions for input and output. (Used internally.) */
350 extern FILE *EXFUN(__fopencookie, (PTR __magic_cookie, __io_mode mode,
351 __io_functions __io_functions));
354 /* Create a new stream that refers to an existing system file descriptor. */
355 extern FILE *EXFUN(fdopen, (int __fd, CONST char *__modes));
359 /* Create a new stream that refers to the given magic cookie,
360 and uses the given functions for input and output. */
361 extern FILE *EXFUN(fopencookie, (PTR __magic_cookie, CONST char *__modes,
362 __io_functions __io_functions));
364 /* Create a new stream that refers to a memory buffer. */
365 extern FILE *EXFUN(fmemopen, (char *__s, size_t __len, CONST char *__modes));
369 /* If BUF is NULL, make STREAM unbuffered.
370 Else make it use buffer BUF, of size BUFSIZ. */
371 extern void EXFUN(setbuf, (FILE *__stream, char *__buf));
372 /* Make STREAM use buffering mode MODE.
373 If BUF is not NULL, use N bytes of it for buffering;
374 else allocate an internal buffer N bytes long. */
375 extern int EXFUN(setvbuf, (FILE *__stream, char *__buf,
376 int __modes, size_t __n));
379 /* If BUF is NULL, make STREAM unbuffered.
380 Else make it use SIZE bytes of BUF for buffering. */
381 extern void EXFUN(setbuffer, (FILE *__stream, char *__buf, size_t __size));
383 /* Make STREAM line-buffered. */
384 extern void EXFUN(setlinebuf, (FILE *__stream));
388 /* Write formatted output to STREAM. */
389 extern int EXFUN(fprintf, (FILE *__stream, CONST char *__format, ...));
390 /* Write formatted output to stdout. */
391 extern int EXFUN(printf, (CONST char *__format, ...));
392 /* Write formatted output to S. */
393 extern int EXFUN(sprintf, (char *__s, CONST char *__format, ...));
395 /* Write formatted output to S from argument list ARG. */
396 extern int EXFUN(vfprintf, (FILE *__s, CONST char *__format, __va_list __arg));
397 /* Write formatted output to stdout from argument list ARG. */
398 extern int EXFUN(vprintf, (CONST char *__format, __va_list __arg));
399 /* Write formatted output to S from argument list ARG. */
400 extern int EXFUN(vsprintf, (char *__s, CONST char *__format, __va_list __arg));
402 /* Write formatted output to S from argument list ARG.
403 If LIMIT is nonzero, process no more than MAX specs. */
404 extern int EXFUN(__vflprintf, (FILE *__s, CONST char *__format,
405 __va_list __arg, int __limit, size_t __max));
408 #define vfprintf(s, format, arg) __vflprintf((s), (format), (arg), 0, 0)
409 #define vprintf(fmt, arg) __vflprintf(stdout, (fmt), (arg), 0, 0)
410 #endif /* Optimizing. */
413 /* Maximum chars of output to write in MAXLEN. */
414 extern int EXFUN(snprintf, (char *__s, size_t __maxlen,
415 CONST char *__format, ...));
417 extern int EXFUN(vsnprintf, (char *__s, size_t __maxlen,
418 CONST char *__format, __va_list __arg));
420 /* Process no more than N format specifiers. */
421 extern int EXFUN(vflprintf, (FILE *__s, size_t __n,
422 CONST char *__format, __va_list __arg));
425 #define vflprintf(s, n, fmt, arg) __vflprintf((s), (fmt), (arg), 1, (n))
426 #endif /* Optimizing. */
428 extern int EXFUN(vslprintf, (char *__s, size_t __n,
429 CONST char *__format, __va_list __arg));
431 /* Write formatted output to S from FORMAT with arguments from ARG,
432 writing no more than MAXLEN chars and processing no more than N specs. */
433 extern int EXFUN(vslnprintf, (char *__s, size_t __n, size_t __maxlen,
434 CONST char *__format, __va_list __arg));
437 /* Write formatted output to a string dynamically allocated with `malloc'.
438 Store the address of the string in *PTR. */
439 extern int EXFUN(vasprintf, (char **__ptr, CONST char *__f, __va_list __arg));
440 extern int EXFUN(asprintf, (char **__ptr, CONST char *__fmt, ...));
444 /* Read formatted input from STREAM. */
445 extern int EXFUN(fscanf, (FILE *__stream, CONST char *__format, ...));
446 /* Read formatted input from stdin. */
447 extern int EXFUN(scanf, (CONST char *__format, ...));
448 /* Read formatted input from S. */
449 extern int EXFUN(sscanf, (CONST char *__s, CONST char *__format, ...));
452 /* Read formatted input from S into argument list ARG. */
453 extern int EXFUN(__vfscanf, (FILE *__s, CONST char *__format,
455 extern int EXFUN(vfscanf, (FILE *__s, CONST char *__format,
458 /* Read formatted input from stdin into argument list ARG. */
459 extern int EXFUN(vscanf, (CONST char *__format, __va_list __arg));
461 /* Read formatted input from S into argument list ARG. */
462 extern int EXFUN(__vsscanf, (CONST char *__s, CONST char *__format,
464 extern int EXFUN(vsscanf, (CONST char *__s, CONST char *__format,
469 #define vfscanf(s, format, arg) __vfscanf((s), (format), (arg))
470 #define vscanf(format, arg) __vfscanf(stdin, (format), (arg))
471 #define vsscanf(s, format, arg) __vsscanf((s), (format), (arg))
472 #endif /* Optimizing. */
473 #endif /* Use GNU. */
476 /* This does actual reading when necessary, filling STREAM's
477 buffer and returning the first character in it. */
478 extern int EXFUN(__fillbf, (FILE *__stream));
481 /* Read a character from STREAM. */
482 extern int EXFUN(fgetc, (FILE *__stream));
483 extern int EXFUN(getc, (FILE *__stream));
485 /* Read a character from stdin. */
486 extern int EXFUN(getchar, (NOARGS));
488 /* The C standard explicitly says this can
489 re-evaluate its argument, so it does. */
490 #define __getc(stream) \
491 (__bufp(stream) == __endp(stream) ? __fillbf(stream) : \
492 (int) ((unsigned char) *__bufp(stream)++))
494 /* The C standard explicitly says this is a macro,
495 so we always do the optimization for it. */
496 #define getc(stream) __getc(stream)
499 #define getchar() __getc(stdin)
500 #endif /* Optimizing. */
503 /* Write a character to STREAM. */
504 extern int EXFUN(fputc, (int __c, FILE *__stream));
505 extern int EXFUN(putc, (int __c, FILE *__stream));
507 /* Write a character to stdout. */
508 extern int EXFUN(putchar, (int __c));
510 /* The C standard explicitly says this can
511 re-evaluate its arguments, so it does. */
512 #define __putc(c, stream) \
513 (__bufp(stream) == __endp(stream) ? \
514 __flshfp((stream), (c), 0) : (unsigned char) (*__bufp(stream)++ = (c)))
516 /* The C standard explicitly says this can be a macro,
517 so we always do the optimization for it. */
518 #define putc(c, stream) __putc((c), (stream))
521 #define putchar(c) __putc((c), stdout)
522 #endif /* Optimizing. */
525 #if defined(__USE_SVID) || defined(__USE_MISC)
526 /* Get a word (int) from STREAM. */
527 extern int EXFUN(getw, (FILE *__stream));
529 /* Write a word (int) to STREAM. */
530 extern int EXFUN(putw, (int __w, FILE *__stream));
534 /* Get a newline-terminated string of finite length from STREAM. */
535 extern char *EXFUN(fgets, (char *__s, size_t __n, FILE *__stream));
537 /* Get a newline-terminated string from stdin, removing the newline.
538 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
539 extern char *EXFUN(gets, (char *__s));
542 /* Write a string to STREAM. */
543 extern int EXFUN(fputs, (CONST char *__s, FILE *__stream));
544 /* Write a string, followed by a newline, to stdout. */
545 extern int EXFUN(puts, (CONST char *__s));
548 #define puts(s) ((fputs((s), stdout) || __putc('\n', stdout) == EOF) ? EOF : 0)
549 #endif /* Optimizing. */
552 /* Push a character back onto the input buffer of STREAM. */
553 extern int EXFUN(ungetc, (int __c, FILE *__stream));
556 /* Read chunks of generic data from STREAM. */
557 extern size_t EXFUN(fread, (PTR __ptr, size_t __size,
558 size_t __n, FILE *__stream));
559 /* Write chunks of generic data to STREAM. */
560 extern size_t EXFUN(fwrite, (CONST PTR __ptr, size_t __size,
561 size_t __n, FILE *__s));
564 /* Seek to a certain position on STREAM. */
565 extern int EXFUN(fseek, (FILE *__stream, long int __off, int __whence));
566 /* Return the current position of STREAM. */
567 extern long int EXFUN(ftell, (FILE *__stream));
568 /* Rewind to the beginning of STREAM. */
569 extern void EXFUN(rewind, (FILE *__stream));
571 /* Get STREAM's position. */
572 extern int EXFUN(fgetpos, (FILE *__stream, fpos_t *__pos));
573 /* Set STREAM's position. */
574 extern int EXFUN(fsetpos, (FILE *__stream, CONST fpos_t *__pos));
577 /* Clear the error and EOF indicators for STREAM. */
578 extern void EXFUN(clearerr, (FILE *__stream));
579 /* Return the EOF indicator for STREAM. */
580 extern int EXFUN(feof, (FILE *__stream));
581 /* Return the error indicator for STREAM. */
582 extern int EXFUN(ferror, (FILE *__stream));
585 #define feof(stream) (__feof(stream) != 0)
586 #define ferror(stream) (__ferror(stream) != 0)
587 #endif /* Optimizing. */
590 /* Print a message describing the meaning of the value of errno. */
591 extern void EXFUN(perror, (CONST char *__s));
594 /* Print a message describing the meaning of the given signal number. */
595 extern void EXFUN(psignal, (int __sig, CONST char *__s));
596 #endif /* Non strict ANSI and not POSIX only. */
600 /* Return the system file descriptor for STREAM. */
601 extern int EXFUN(fileno, (CONST FILE *__stream));
604 /* The `+ 0' makes this not be an lvalue, so it can't be changed. */
605 #define fileno(stream) (__fileno(stream) + 0)
606 #endif /* Optimizing. */
608 #endif /* Use POSIX. */
611 #if (defined (__USE_POSIX2) || defined(__USE_SVID) || defined(__USE_BSD) || \
613 /* Create a new stream connected to a pipe running the given command. */
614 extern FILE *EXFUN(popen, (CONST char *__command, CONST char *__modes));
616 /* Close a stream opened by popen and return the status of its child. */
617 extern int EXFUN(pclose, (FILE *__stream));
622 /* Return the name of the controlling terminal. */
623 extern char *EXFUN(ctermid, (char *__s));
624 /* Return the name of the current user. */
625 extern char *EXFUN(cuserid, (char *__s));
628 #endif /* <stdio.h> included. */