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>
29 #if !defined(__need_FILE)
37 #include <gnu/types.h>
38 #endif /* Don't need FILE. */
42 #ifndef __FILE_defined
44 /* The opaque type of streams. */
45 typedef struct __stdio_file FILE;
47 #define __FILE_defined 1
48 #endif /* FILE not defined. */
53 /* The type of the second argument to `fgetpos' and `fsetpos'. */
54 typedef __off_t fpos_t;
56 /* The mode of I/O, as given in the MODE argument to fopen, etc. */
59 unsigned int __read:1; /* Open for reading. */
60 unsigned int __write:1; /* Open for writing. */
61 unsigned int __append:1; /* Open for appending. */
62 unsigned int __binary:1; /* Opened binary. */
63 unsigned int __create:1; /* Create the file. */
64 unsigned int __exclusive:1; /* Error if it already exists. */
65 unsigned int __truncate:1; /* Truncate the file on opening. */
69 /* Functions to do I/O and file management for a stream. */
71 /* Read NBYTES bytes from COOKIE into a buffer pointed to by BUF.
72 Return number of bytes read. */
73 typedef __ssize_t EXFUN(__io_read, (PTR __cookie, char *__buf,
76 /* Write N bytes pointed to by BUF to COOKIE. Write all N bytes
77 unless there is an error. Return number of bytes written, or -1 if
78 there is an error without writing anything. If the file has been
79 opened for append (__mode.__append set), then set the file pointer
80 to the end of the file and then do the write; if not, just write at
81 the current file pointer. */
82 typedef __ssize_t EXFUN(__io_write, (PTR __cookie, CONST char *__buf,
85 /* Move COOKIE's file position to *POS bytes from the
86 beginning of the file (if W is SEEK_SET),
87 the current position (if W is SEEK_CUR),
88 or the end of the file (if W is SEEK_END).
89 Set *POS to the new file position.
90 Returns zero if successful, nonzero if not. */
91 typedef int EXFUN(__io_seek, (PTR __cookie, fpos_t *__pos, int __w));
94 typedef int EXFUN(__io_close, (PTR __cookie));
96 /* Low level interface, independent of FILE representation. */
99 __io_read *__read; /* Read bytes. */
100 __io_write *__write; /* Write bytes. */
101 __io_seek *__seek; /* Seek/tell file position. */
102 __io_close *__close; /* Close file. */
105 /* Higher level interface, dependent on FILE representation. */
108 /* Make room in the input buffer. */
109 int EXFUN((*__input), (FILE *__stream));
110 /* Make room in the output buffer. */
111 void EXFUN((*__output), (FILE *__stream, int __c));
114 extern CONST __io_functions __default_io_functions;
115 extern CONST __room_functions __default_room_functions;
118 /* Default close function. */
119 extern __io_close __stdio_close;
120 /* Open FILE with mode M, return cookie or NULL to use an int in *DP. */
121 extern PTR EXFUN(__stdio_open, (CONST char *__file, __io_mode __m, int *__dp));
122 /* Put out an error message for when stdio needs to die. */
123 extern void EXFUN(__stdio_errmsg, (CONST char *__msg, size_t __len));
124 /* Generate a unique file name. */
125 extern char *EXFUN(__stdio_gen_tempname, (CONST char *__dir, CONST char *__pfx,
126 int __dir_search, size_t *__lenptr));
130 #define __NORETURN __volatile
134 #endif /* __NORETURN not defined. */
136 /* Print out MESSAGE on the error output and abort. */
137 extern __NORETURN void EXFUN(__libc_fatal, (CONST char *__message));
140 /* The FILE structure. */
143 /* Magic number for validation. Must be negative in open streams
144 for the glue to Unix stdio getc/putc to work. */
146 #define _IOMAGIC 0xfedabeeb /* Magic number to fill `__magic'. */
147 #define _GLUEMAGIC 0xfeedbabe /* Magic for glued Unix streams. */
149 char *__bufp; /* Pointer into the buffer. */
150 char *__get_limit; /* Reading limit. */
151 char *__put_limit; /* Writing limit. */
152 char *__buffer; /* Base of buffer. */
153 size_t __bufsize; /* Size of the buffer. */
154 FILE *__next; /* Next FILE in the linked list. */
155 PTR __cookie; /* Magic cookie. */
156 int __fileno; /* System file descriptor. */
157 unsigned char __pushback; /* Pushed-back character. */
158 char *__pushback_bufp; /* Old bufp if char pushed back. */
159 unsigned int __pushed_back:1; /* A char has been pushed back. */
160 unsigned int __eof:1; /* End of file encountered. */
161 unsigned int __error:1; /* Error encountered. */
162 unsigned int __userbuf:1; /* Buffer is from user. */
163 unsigned int __linebuf:1; /* Flush on newline. */
164 unsigned int __seen:1; /* This stream has been seen. */
165 unsigned int __ispipe:1; /* Nonzero if opened by popen. */
166 __io_mode __mode; /* File access mode. */
167 __io_functions __io_funcs; /* I/O functions. */
168 __room_functions __room_funcs; /* I/O buffer room functions. */
169 fpos_t __offset; /* Current file position. */
170 fpos_t __target; /* Target file position. */
174 /* All macros used internally by other macros here and by stdio functions begin
175 with `__'. All of these may evaluate their arguments more than once. */
178 /* Nonzero if STREAM is a valid stream. */
179 #define __validfp(stream) \
180 (stream != NULL && ((stream->__magic == _GLUEMAGIC && \
181 (stream = (FILE *) &((int *) stream)[1])), \
182 (stream->__magic == _IOMAGIC))) \
184 /* Clear the error and EOF indicators of STREAM. */
185 #define __clearerr(stream) ((stream)->__error = (stream)->__eof = 0)
187 /* Nuke STREAM, making it unusable but available for reuse. */
188 extern void EXFUN(__invalidate, (FILE *__stream));
190 /* Make sure STREAM->__offset and STREAM->__target are initialized.
191 Returns 0 if successful, or EOF on
192 error (but doesn't set STREAM->__error). */
193 extern int EXFUN(__stdio_check_offset, (FILE *__stream));
196 /* The possibilities for the third argument to `setvbuf'. */
197 #define _IOFBF 0x1 /* Full buffering. */
198 #define _IOLBF 0x2 /* Line buffering. */
199 #define _IONBF 0x4 /* No buffering. */
202 /* Default buffer size. */
206 /* End of file character.
207 Some things throughout the library rely on this being -1. */
211 /* The possibilities for the third argument to `fseek'.
212 These values should not be changed. */
213 #define SEEK_SET 0 /* Seek from beginning of file. */
214 #define SEEK_CUR 1 /* Seek from current position. */
215 #define SEEK_END 2 /* Seek from end of file. */
219 /* Default path prefix for `tempnam' and `tmpnam'. */
220 #define P_tmpdir "/usr/tmp"
225 L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
226 TMP_MAX The minimum number of unique filenames generated by tmpnam
227 (and tempnam when it uses tmpnam's name space),
228 or tempnam (the two are separate).
229 L_ctermid How long an array to pass to `ctermid'.
230 L_cuserid How long an array to pass to `cuserid'.
231 FOPEN_MAX Mininum number of files that can be open at once.
232 FILENAME_MAX Maximum length of a filename. */
233 #include <stdio_lim.h>
236 /* All the known streams are in a linked list
237 linked by the `next' field of the FILE structure. */
238 extern FILE *__stdio_head; /* Head of the list. */
240 /* Standard streams. */
241 extern FILE *stdin, *stdout, *stderr;
244 /* Remove file FILENAME. */
245 extern int EXFUN(remove, (CONST char *__filename));
246 /* Rename file OLD to NEW. */
247 extern int EXFUN(rename, (CONST char *__old, CONST char *__new));
250 /* Create a temporary file and open it read/write. */
251 extern FILE *EXFUN(tmpfile, (NOARGS));
252 /* Generate a temporary filename. */
253 extern char *EXFUN(tmpnam, (char *__s));
257 /* Generate a unique temporary filename using up to five characters of PFX
258 if it is not NULL. The directory to put this file in is searched for
259 as follows: First the environment variable "TMPDIR" is checked.
260 If it contains the name of a writable directory, that directory is used.
261 If not and if DIR is not NULL, that value is checked. If that fails,
262 P_tmpdir is tried and finally "/tmp". The storage for the filename
263 is allocated by `malloc'. */
264 extern char *EXFUN(tempnam, (CONST char *__dir, CONST char *__pfx));
268 /* This performs actual output when necessary, flushing
269 STREAM's buffer and optionally writing another character. */
270 extern int EXFUN(__flshfp, (FILE *__stream, int __c));
273 /* Close STREAM, or all streams if STREAM is NULL. */
274 extern int EXFUN(fclose, (FILE *__stream));
275 /* Flush STREAM, or all streams if STREAM is NULL. */
276 extern int EXFUN(fflush, (FILE *__stream));
279 /* Open a file and create a new stream for it. */
280 extern FILE *EXFUN(fopen, (CONST char *__filename, CONST char *__modes));
281 /* Open a file, replacing an existing stream with it. */
282 extern FILE *EXFUN(freopen, (CONST char *__filename,
283 CONST char *__modes, FILE *__stream));
285 /* Return a new, zeroed, stream.
286 You must set its cookie and io_mode.
287 The first operation will give it a buffer unless you do.
288 It will also give it the default functions unless you set the `seen' flag.
289 The offset is set to -1, meaning it will be determined by doing a
290 stationary seek. You can set it to avoid the initial tell call.
291 The target is set to -1, meaning it will be set to the offset
292 before the target is needed.
293 Returns NULL if a stream can't be created. */
294 extern FILE *EXFUN(__newstream, (NOARGS));
297 /* Create a new stream that refers to an existing system file descriptor. */
298 extern FILE *EXFUN(fdopen, (int __fd, CONST char *__modes));
302 /* Create a new stream that refers to the given magic cookie,
303 and uses the given functions for input and output. */
304 extern FILE *EXFUN(fopencookie, (PTR __magic_cookie, CONST char *__modes,
305 __io_functions __io_functions));
307 /* Create a new stream that refers to a memory buffer. */
308 extern FILE *EXFUN(fmemopen, (PTR __s, size_t __len, CONST char *__modes));
310 /* Open a stream that writes into a malloc'd buffer that is expanded as
311 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
312 and the number of characters written on fflush or fclose. */
313 extern FILE *EXFUN(open_memstream, (char **__bufloc, size_t *sizeloc));
317 /* If BUF is NULL, make STREAM unbuffered.
318 Else make it use buffer BUF, of size BUFSIZ. */
319 extern void EXFUN(setbuf, (FILE *__stream, char *__buf));
320 /* Make STREAM use buffering mode MODE.
321 If BUF is not NULL, use N bytes of it for buffering;
322 else allocate an internal buffer N bytes long. */
323 extern int EXFUN(setvbuf, (FILE *__stream, char *__buf,
324 int __modes, size_t __n));
327 /* If BUF is NULL, make STREAM unbuffered.
328 Else make it use SIZE bytes of BUF for buffering. */
329 extern void EXFUN(setbuffer, (FILE *__stream, char *__buf, size_t __size));
331 /* Make STREAM line-buffered. */
332 extern void EXFUN(setlinebuf, (FILE *__stream));
336 /* Write formatted output to STREAM. */
337 extern int EXFUN(fprintf, (FILE *__stream, CONST char *__format, ...));
338 /* Write formatted output to stdout. */
339 extern int EXFUN(printf, (CONST char *__format, ...));
340 /* Write formatted output to S. */
341 extern int EXFUN(sprintf, (char *__s, CONST char *__format, ...));
343 /* Write formatted output to S from argument list ARG. */
344 extern int EXFUN(vfprintf, (FILE *__s, CONST char *__format, PTR __arg));
345 /* Write formatted output to stdout from argument list ARG. */
346 extern int EXFUN(vprintf, (CONST char *__format, PTR __arg));
347 /* Write formatted output to S from argument list ARG. */
348 extern int EXFUN(vsprintf, (char *__s, CONST char *__format, PTR __arg));
351 #define vprintf(fmt, arg) vfprintf(stdout, (fmt), (arg))
352 #endif /* Optimizing. */
355 /* Maximum chars of output to write in MAXLEN. */
356 extern int EXFUN(snprintf, (char *__s, size_t __maxlen,
357 CONST char *__format, ...));
359 extern int EXFUN(vsnprintf, (char *__s, size_t __maxlen,
360 CONST char *__format, PTR __arg));
362 /* Write formatted output to a string dynamically allocated with `malloc'.
363 Store the address of the string in *PTR. */
364 extern int EXFUN(vasprintf, (char **__ptr, CONST char *__f, PTR __arg));
365 extern int EXFUN(asprintf, (char **__ptr, CONST char *__fmt, ...));
367 /* Write formatted output to a file descriptor. */
368 extern int EXFUN(vdprintf, (int __fd, CONST char *__fmt, PTR __arg));
369 extern int EXFUN(dprintf, (int __fd, CONST char *__fmt, ...));
373 /* Read formatted input from STREAM. */
374 extern int EXFUN(fscanf, (FILE *__stream, CONST char *__format, ...));
375 /* Read formatted input from stdin. */
376 extern int EXFUN(scanf, (CONST char *__format, ...));
377 /* Read formatted input from S. */
378 extern int EXFUN(sscanf, (CONST char *__s, CONST char *__format, ...));
381 /* Read formatted input from S into argument list ARG. */
382 extern int EXFUN(__vfscanf, (FILE *__s, CONST char *__format,
384 extern int EXFUN(vfscanf, (FILE *__s, CONST char *__format,
387 /* Read formatted input from stdin into argument list ARG. */
388 extern int EXFUN(vscanf, (CONST char *__format, PTR __arg));
390 /* Read formatted input from S into argument list ARG. */
391 extern int EXFUN(__vsscanf, (CONST char *__s, CONST char *__format,
393 extern int EXFUN(vsscanf, (CONST char *__s, CONST char *__format,
398 #define vfscanf(s, format, arg) __vfscanf((s), (format), (arg))
399 #define vscanf(format, arg) __vfscanf(stdin, (format), (arg))
400 #define vsscanf(s, format, arg) __vsscanf((s), (format), (arg))
401 #endif /* Optimizing. */
402 #endif /* Use GNU. */
405 /* This does actual reading when necessary, filling STREAM's
406 buffer and returning the first character in it. */
407 extern int EXFUN(__fillbf, (FILE *__stream));
410 /* Read a character from STREAM. */
411 extern int EXFUN(fgetc, (FILE *__stream));
412 extern int EXFUN(getc, (FILE *__stream));
414 /* Read a character from stdin. */
415 extern int EXFUN(getchar, (NOARGS));
417 /* The C standard explicitly says this can
418 re-evaluate its argument, so it does. */
419 #define __getc(stream) \
420 ((stream)->__bufp < (stream)->__get_limit ? \
421 (int) ((unsigned char) *(stream)->__bufp++) : __fillbf(stream))
423 /* The C standard explicitly says this is a macro,
424 so we always do the optimization for it. */
425 #define getc(stream) __getc(stream)
428 #define getchar() __getc(stdin)
429 #endif /* Optimizing. */
432 /* Write a character to STREAM. */
433 extern int EXFUN(fputc, (int __c, FILE *__stream));
434 extern int EXFUN(putc, (int __c, FILE *__stream));
436 /* Write a character to stdout. */
437 extern int EXFUN(putchar, (int __c));
439 /* The C standard explicitly says this can
440 re-evaluate its arguments, so it does. */
441 #define __putc(c, stream) \
442 ((stream)->__bufp < (stream)->__put_limit ? \
443 (int) (unsigned char) (*(stream)->__bufp++ = (unsigned char) (c)) : \
444 ((stream)->__linebuf && (c) != '\n' && \
445 (stream)->__bufp - (stream)->__buffer < (stream)->__bufsize) ? \
446 (*(stream)->__bufp++ = (unsigned char) (c)) : \
447 __flshfp((stream), (unsigned char) (c)))
449 /* The C standard explicitly says this can be a macro,
450 so we always do the optimization for it. */
451 #define putc(c, stream) __putc((c), (stream))
454 #define putchar(c) __putc((c), stdout)
455 #endif /* Optimizing. */
458 #if defined(__USE_SVID) || defined(__USE_MISC)
459 /* Get a word (int) from STREAM. */
460 extern int EXFUN(getw, (FILE *__stream));
462 /* Write a word (int) to STREAM. */
463 extern int EXFUN(putw, (int __w, FILE *__stream));
467 /* Get a newline-terminated string of finite length from STREAM. */
468 extern char *EXFUN(fgets, (char *__s, size_t __n, FILE *__stream));
470 /* Get a newline-terminated string from stdin, removing the newline.
471 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
472 extern char *EXFUN(gets, (char *__s));
476 #include <sys/types.h>
478 /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
479 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
480 NULL), pointing to *N characters of space. It is realloc'd as
481 necessary. Returns the number of characters read (not including the
482 null terminator), or -1 on error or EOF. */
483 ssize_t EXFUN(__getdelim, (char **lineptr, size_t *n,
484 int delimiter, FILE *stream));
485 ssize_t EXFUN(getdelim, (char **lineptr, size_t *n,
486 int delimiter, FILE *stream));
488 /* Like `getdelim', but reads up to a newline. */
489 ssize_t EXFUN(__getline, (char **lineptr, size_t *n, FILE *stream));
490 ssize_t EXFUN(getline, (char **lineptr, size_t *n, FILE *stream));
493 #define getdelim(l, n, d, s) __getdelim ((l), (n), (d), (s))
494 #define getline(l, n, s) __getline ((l), (n), (s))
495 #define __getline(l, n, stream) __getdelim ((l), (n), '\n', (stream))
496 #endif /* Optimizing. */
500 /* Write a string to STREAM. */
501 extern int EXFUN(fputs, (CONST char *__s, FILE *__stream));
502 /* Write a string, followed by a newline, to stdout. */
503 extern int EXFUN(puts, (CONST char *__s));
506 #define puts(s) ((fputs((s), stdout) || __putc('\n', stdout) == EOF) ? EOF : 0)
507 #endif /* Optimizing. */
510 /* Push a character back onto the input buffer of STREAM. */
511 extern int EXFUN(ungetc, (int __c, FILE *__stream));
514 /* Read chunks of generic data from STREAM. */
515 extern size_t EXFUN(fread, (PTR __ptr, size_t __size,
516 size_t __n, FILE *__stream));
517 /* Write chunks of generic data to STREAM. */
518 extern size_t EXFUN(fwrite, (CONST PTR __ptr, size_t __size,
519 size_t __n, FILE *__s));
522 /* Seek to a certain position on STREAM. */
523 extern int EXFUN(fseek, (FILE *__stream, long int __off, int __whence));
524 /* Return the current position of STREAM. */
525 extern long int EXFUN(ftell, (FILE *__stream));
526 /* Rewind to the beginning of STREAM. */
527 extern void EXFUN(rewind, (FILE *__stream));
529 /* Get STREAM's position. */
530 extern int EXFUN(fgetpos, (FILE *__stream, fpos_t *__pos));
531 /* Set STREAM's position. */
532 extern int EXFUN(fsetpos, (FILE *__stream, CONST fpos_t *__pos));
535 /* Clear the error and EOF indicators for STREAM. */
536 extern void EXFUN(clearerr, (FILE *__stream));
537 /* Return the EOF indicator for STREAM. */
538 extern int EXFUN(feof, (FILE *__stream));
539 /* Return the error indicator for STREAM. */
540 extern int EXFUN(ferror, (FILE *__stream));
543 #define feof(stream) ((stream)->__eof != 0)
544 #define ferror(stream) ((stream)->__error != 0)
545 #endif /* Optimizing. */
548 /* Print a message describing the meaning of the value of errno. */
549 extern void EXFUN(perror, (CONST char *__s));
552 /* Print a message describing the meaning of the given signal number. */
553 extern void EXFUN(psignal, (int __sig, CONST char *__s));
554 #endif /* Non strict ANSI and not POSIX only. */
558 /* Return the system file descriptor for STREAM. */
559 extern int EXFUN(fileno, (CONST FILE *__stream));
562 /* The `+ 0' makes this not be an lvalue, so it can't be changed. */
563 #define fileno(stream) ((stream)->__fileno + 0)
564 #endif /* Optimizing. */
566 #endif /* Use POSIX. */
569 #if (defined (__USE_POSIX2) || defined(__USE_SVID) || defined(__USE_BSD) || \
571 /* Create a new stream connected to a pipe running the given command. */
572 extern FILE *EXFUN(popen, (CONST char *__command, CONST char *__modes));
574 /* Close a stream opened by popen and return the status of its child. */
575 extern int EXFUN(pclose, (FILE *__stream));
580 /* Return the name of the controlling terminal. */
581 extern char *EXFUN(ctermid, (char *__s));
582 /* Return the name of the current user. */
583 extern char *EXFUN(cuserid, (char *__s));
586 #endif /* <stdio.h> included. */