1 /* Define ISO C stdio on top of C++ iostreams.
2 Copyright (C) 1991, 1994-1999, 2000 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>
28 #include <sys/types.h>
33 /* when you add or change fields here, be sure to change the initialization
34 * in stdio_init and fopen */
36 unsigned char *bufpos; /* the next byte to write to or read from */
37 unsigned char *bufread; /* the end of data returned by last read() */
38 unsigned char *bufwrite; /* highest address writable by macro */
39 unsigned char *bufstart; /* the start of the buffer */
40 unsigned char *bufend; /* the end of the buffer; ie the byte after the last
43 int fd; /* the file descriptor associated with the stream */
46 char unbuf[8]; /* The buffer for 'unbuffered' streams */
48 struct __stdio_file * next;
51 typedef struct __stdio_file FILE;
53 /* Default buffer size. */
56 /* Define EOF and NULL */
62 /* The possibilities for the third argument to `setvbuf'. */
63 #define _IOFBF 0 /* Fully buffered. */
64 #define _IOLBF 1 /* Line buffered. */
65 #define _IONBF 2 /* No buffering. */
67 /* Possible states for a file stream -- internal use only */
68 #define __MODE_IOTRAN 0
69 #define __MODE_BUF 0x03 /* Modal buffering dependent on isatty */
70 #define __MODE_FREEBUF 0x04 /* Buffer allocated with malloc, can free */
71 #define __MODE_FREEFIL 0x08 /* FILE allocated with malloc, can free */
72 #define __MODE_READ 0x10 /* Opened in read only */
73 #define __MODE_WRITE 0x20 /* Opened in write only */
74 #define __MODE_RDWR 0x30 /* Opened in read/write */
75 #define __MODE_READING 0x40 /* Buffer has pending read data */
76 #define __MODE_WRITING 0x80 /* Buffer has pending write data */
77 #define __MODE_EOF 0x100 /* EOF status */
78 #define __MODE_ERR 0x200 /* Error status */
79 #define __MODE_UNGOT 0x400 /* Buffer has been polluted by ungetc */
82 /* The possibilities for the third argument to `fseek'.
83 These values should not be changed. */
84 #define SEEK_SET 0 /* Seek from beginning of file. */
85 #define SEEK_CUR 1 /* Seek from current position. */
86 #define SEEK_END 2 /* Seek from end of file. */
89 #define stdio_pending(fp) ((fp)->bufread>(fp)->bufpos)
93 /* Default path prefix for `tempnam' and `tmpnam'. */
94 #define P_tmpdir "/tmp"
96 L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
97 TMP_MAX The minimum number of unique filenames generated by tmpnam
98 (and tempnam when it uses tmpnam's name space),
99 or tempnam (the two are separate).
100 L_ctermid How long an array to pass to `ctermid'.
101 L_cuserid How long an array to pass to `cuserid'.
102 FOPEN_MAX Minimum number of files that can be open at once.
103 FILENAME_MAX Maximum length of a filename. */
104 #define __need_FOPEN_MAX
105 #include <bits/stdio_lim.h>
106 #undef __need_FOPEN_MAX
108 /* Standard streams (internal). */
110 extern FILE *_stdout;
111 extern FILE *_stderr;
113 /* C89/C99 say they're macros. Make them happy. */
115 #define stdout _stdout
116 #define stderr _stderr
118 /* Remove file FILENAME. */
119 extern int remove __P ((__const char *__filename));
120 /* Rename file OLD to NEW. */
121 extern int rename __P ((__const char *__old, __const char *__new));
124 /* Create a temporary file and open it read/write. */
125 extern FILE *tmpfile __P ((void));
126 #ifdef __USE_LARGEFILE64
127 extern FILE *tmpfile64 __P ((void));
129 /* Generate a temporary filename. */
130 extern char *tmpnam __P ((char *__s));
133 /* This is the reentrant variant of `tmpnam'. The only difference is
134 that it does not allow S to be NULL. */
135 extern char *tmpnam_r __P ((char *__s));
139 #if defined __USE_SVID || defined __USE_XOPEN
140 /* Generate a unique temporary filename using up to five characters of PFX
141 if it is not NULL. The directory to put this file in is searched for
142 as follows: First the environment variable "TMPDIR" is checked.
143 If it contains the name of a writable directory, that directory is used.
144 If not and if DIR is not NULL, that value is checked. If that fails,
145 P_tmpdir is tried and finally "/tmp". The storage for the filename
146 is allocated by `malloc'. */
147 extern char *tempnam __P ((__const char *__dir, __const char *__pfx));
152 extern int fclose __P ((FILE *__stream));
153 /* Flush STREAM, or all streams if STREAM is NULL. */
154 extern int fflush __P ((FILE *__stream));
156 /* Open a file and create a new stream for it. */
157 extern FILE *fopen __P ((__const char *__restrict __filename,
158 __const char *__restrict __modes));
159 /* Used internally to actuall open files */
160 extern FILE *__fopen __P((__const char *__restrict __filename, int __fd,
161 FILE *__restrict __stream, __const char *__restrict __modes));
162 #define fopen(__file, __mode) __fopen((__file), -1, (FILE*)0, (__mode))
163 /* Open a file, replacing an existing stream with it. */
164 extern FILE *freopen __P ((__const char *__restrict __filename,
165 __const char *__restrict __modes,
166 FILE *__restrict __stream));
167 #define freopen(__file, __mode, __fp) __fopen((__file), -1, (__fp), (__mode))
169 #ifdef __USE_LARGEFILE64
170 extern FILE *fopen64 __P ((__const char *__restrict __filename,
171 __const char *__restrict __modes));
172 extern FILE *freopen64 __P ((__const char *__restrict __filename,
173 __const char *__restrict __modes,
174 FILE *__restrict __stream));
178 /* Create a new stream that refers to an existing system file descriptor. */
179 extern FILE *fdopen __P ((int __fd, __const char *__modes));
180 #define fdopen(__file, __mode) __fopen((char*)0, (__file), (FILE*)0, (__mode))
184 /* If BUF is NULL, make STREAM unbuffered.
185 Else make it use buffer BUF, of size BUFSIZ. */
186 extern void setbuf __P ((FILE *__restrict __stream, char *__restrict __buf));
187 #define setbuf(__fp, __buf) setbuffer((__fp), (__buf), BUFSIZ)
189 /* Make STREAM use buffering mode MODE.
190 If BUF is not NULL, use N bytes of it for buffering;
191 else allocate an internal buffer N bytes long. */
192 extern int setvbuf __P ((FILE *__restrict __stream, char *__restrict __buf,
193 int __modes, size_t __n));
196 /* If BUF is NULL, make STREAM unbuffered.
197 Else make it use SIZE bytes of BUF for buffering. */
198 extern void setbuffer __P ((FILE *__restrict __stream, char *__restrict __buf,
201 /* Make STREAM line-buffered. */
202 extern void setlinebuf __P ((FILE *__stream));
203 #define setlinebuf(__fp) setvbuf((__fp), (char*)0, _IOLBF, 0)
207 /* Write formatted output to STREAM. */
208 extern int fprintf __P ((FILE *__restrict __stream,
209 __const char *__restrict __format, ...));
210 /* Write formatted output to stdout. */
211 extern int printf __P ((__const char *__restrict __format, ...));
212 /* Write formatted output to S. */
213 extern int sprintf __P ((char *__restrict __s,
214 __const char *__restrict __format, ...));
216 /* Write formatted output to a file descriptor */
217 extern int vdprintf __P((int fd, __const char *__restrict __format,
220 /* Write formatted output to a buffer S dynamically allocated by asprintf. */
221 extern int asprintf __P ((char **__restrict __s,
222 __const char *__restrict __format, ...));
224 /* Write formatted output to S from argument list ARG. */
225 extern int vfprintf __P ((FILE *__restrict __s,
226 __const char *__restrict __format,
228 /* Write formatted output to stdout from argument list ARG. */
229 extern int vprintf __P ((__const char *__restrict __format,
231 /* Write formatted output to S from argument list ARG. */
232 extern int vsprintf __P ((char *__restrict __s,
233 __const char *__restrict __format,
236 /* Maximum chars of output to write in MAXLEN. */
237 extern int snprintf __P ((char *__restrict __s, size_t __maxlen,
238 __const char *__restrict __format, ...))
239 __attribute__ ((__format__ (__printf__, 3, 4)));
241 extern int __vsnprintf __P ((char *__restrict __s, size_t __maxlen,
242 __const char *__restrict __format,
244 __attribute__ ((__format__ (__printf__, 3, 0)));
245 extern int vsnprintf __P ((char *__restrict __s, size_t __maxlen,
246 __const char *__restrict __format,
248 __attribute__ ((__format__ (__printf__, 3, 0)));
250 /* Read formatted input from STREAM. */
251 extern int fscanf __P ((FILE *__restrict __stream,
252 __const char *__restrict __format, ...));
253 /* Read formatted input from stdin. */
254 extern int scanf __P ((__const char *__restrict __format, ...));
255 /* Read formatted input from S. */
256 extern int sscanf __P ((__const char *__restrict __s,
257 __const char *__restrict __format, ...));
259 /* Read formatted input from S into argument list ARG. */
260 extern int vfscanf __P ((FILE *__restrict __s,
261 __const char *__restrict __format,
263 __attribute__ ((__format__ (__scanf__, 2, 0)));
265 /* Read formatted input from stdin into argument list ARG. */
266 extern int vscanf __P ((__const char *__restrict __format, va_list __arg))
267 __attribute__ ((__format__ (__scanf__, 1, 0)));
269 /* Read formatted input from S into argument list ARG. */
270 extern int vsscanf __P ((__const char *__restrict __s,
271 __const char *__restrict __format,
273 __attribute__ ((__format__ (__scanf__, 2, 0)));
276 /* Read a character from STREAM. */
277 extern int fgetc __P ((FILE *__stream));
278 extern int getc __P ((FILE *__stream));
280 /* Read a character from stdin. */
281 extern int getchar __P ((void));
282 #define getchar() getc(stdin)
284 /* The C standard explicitly says this is a macro, so be that way */
285 #define getc(stream) \
286 (((stream)->bufpos >= (stream)->bufread) ? fgetc(stream): \
287 (*(stream)->bufpos++))
289 /* Write a character to STREAM. */
290 extern int fputc __P ((int __c, FILE *__stream));
291 extern int putc __P ((int __c, FILE *__stream));
293 /* Write a character to stdout. */
294 extern int putchar __P ((int __c));
295 #define putchar(c) putc((c), stdout)
297 /* The C standard explicitly says this can be a macro, so be that way */
298 #define putc(c, stream) \
299 (((stream)->bufpos >= (stream)->bufwrite) ? fputc((c), (stream)) \
300 : (unsigned char) (*(stream)->bufpos++ = (c)) )
302 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
303 /* Get a word (int) from STREAM. */
304 extern int getw __P ((FILE *__stream));
306 /* Write a word (int) to STREAM. */
307 extern int putw __P ((int __w, FILE *__stream));
311 /* Get a newline-terminated string of finite length from STREAM. */
312 extern char *fgets __P ((char *__restrict __s, int __n,
313 FILE *__restrict __stream));
315 /* Get a newline-terminated string from stdin, removing the newline.
316 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
317 extern char *gets __P ((char *__s));
320 /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
321 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
322 NULL), pointing to *N characters of space. It is realloc'd as
323 necessary. Returns the number of characters read (not including the
324 null terminator), or -1 on error or EOF. */
325 extern ssize_t __getdelim __P ((char **__restrict __lineptr,
326 size_t *__restrict __n, int __delimiter,
327 FILE *__restrict __stream));
328 extern ssize_t getdelim __P ((char **__restrict __lineptr,
329 size_t *__restrict __n, int __delimiter,
330 FILE *__restrict __stream));
332 /* Like `getdelim', but reads up to a newline. */
333 extern ssize_t getline __P ((char **__restrict __lineptr,
334 size_t *__restrict __n,
335 FILE *__restrict __stream));
338 /* Write a string to STREAM. */
339 extern int fputs __P ((__const char *__restrict __s,
340 FILE *__restrict __stream));
342 /* Write a string, followed by a newline, to stdout. */
343 extern int puts __P ((__const char *__s));
346 /* Push a character back onto the input buffer of STREAM. */
347 extern int ungetc __P ((int __c, FILE *__stream));
350 /* Read chunks of generic data from STREAM. */
351 extern size_t fread __P ((void *__restrict __ptr, size_t __size,
352 size_t __n, FILE *__restrict __stream));
353 /* Write chunks of generic data to STREAM. */
354 extern size_t fwrite __P ((__const void *__restrict __ptr, size_t __size,
355 size_t __n, FILE *__restrict __s));
357 /* Seek to a certain position on STREAM. */
358 extern int fseek __P ((FILE *__stream, long int __off, int __whence));
359 /* Return the current position of STREAM. */
360 extern long int ftell __P ((FILE *__stream));
361 /* Rewind to the beginning of STREAM. */
362 extern void rewind __P ((FILE *__stream));
364 /* The Single Unix Specification, Version 2, specifies an alternative,
365 more adequate interface for the two functions above which deal with
366 file offset. `long int' is not the right type. These definitions
367 are originally defined in the Large File Support API. */
369 /* Types needed in these functions. */
371 typedef __off_t off_t;
375 #if defined __USE_LARGEFILE64 && !defined off64_t
376 typedef __off64_t off64_t;
377 # define off64_t off64_t
381 /* Clear the error and EOF indicators for STREAM. */
382 extern void clearerr __P ((FILE *__stream));
383 #define clearerr(fp) ((fp)->mode &= ~(__MODE_EOF|__MODE_ERR),0)
384 /* Return the EOF indicator for STREAM. */
385 extern int feof __P ((FILE *__stream));
386 #define feof(fp) (((fp)->mode&__MODE_EOF) != 0)
387 /* Return the error indicator for STREAM. */
388 extern int ferror __P ((FILE *__stream));
389 #define ferror(fp) (((fp)->mode&__MODE_ERR) != 0)
391 /* Print a message describing the meaning of the value of errno. */
392 extern void perror __P ((__const char *__s));
394 /* These variables normally should not be used directly. The `strerror'
395 function provides all the needed functionality. */
397 extern __const char *__const sys_errlist[];
400 /* Return the system file descriptor for STREAM. */
401 extern int fileno __P ((FILE *__stream));
402 #define fileno(fp) ((fp)->fd)
403 #endif /* Use POSIX. */
405 #if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \
407 /* Create a new stream connected to a pipe running the given command. */
408 extern FILE *popen __P ((__const char *__command, __const char *__modes));
410 /* Close a stream opened by popen and return the status of its child. */
411 extern int pclose __P ((FILE *__stream));
416 /* Return the name of the controlling terminal. */
417 extern char *ctermid __P ((char *__s));
418 #endif /* Use POSIX. */
422 /* Return the name of the current user. */
423 extern char *cuserid __P ((char *__s));
424 #endif /* Use X/Open. */
427 #if defined __USE_POSIX || defined __USE_MISC
428 /* These are defined in POSIX.1:1996. */
430 /* Acquire ownership of STREAM. */
431 extern void flockfile __P ((FILE *__stream));
433 /* Try to acquire ownership of STREAM but do not block if it is not
435 extern int ftrylockfile __P ((FILE *__stream));
437 /* Relinquish the ownership granted for STREAM. */
438 extern void funlockfile __P ((FILE *__stream));
439 #endif /* POSIX || misc */
441 #if defined __USE_XOPEN && !defined __USE_GNU
442 /* The X/Open standard requires some functions and variables to be
443 declared here which do not belong into this header. But we have to
444 follow. In GNU mode we don't do this nonsense. */
445 # define __need_getopt
449 /* If we are compiling with optimizing read this file. It contains
450 several optizing inline functions and macros. */
451 #ifdef __USE_EXTERN_INLINES
452 # include <bits/stdio.h>
457 #endif /* !_STDIO_H */