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. */
54 #define BUFSIZ (500) /* should get us a fully used kmalloc bucket */
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). */
109 extern FILE _stdio_streams[3];
110 /* C89/C99 say they're macros. Make them happy. */
111 #define stdin (_stdio_streams)
112 #define stdout (_stdio_streams+1)
113 #define stderr (_stdio_streams+2)
115 /* Remove file FILENAME. */
116 extern int remove __P ((__const char *__filename));
117 /* Rename file OLD to NEW. */
118 extern int rename __P ((__const char *__old, __const char *__new));
121 /* Create a temporary file and open it read/write. */
122 extern FILE *tmpfile __P ((void));
123 #ifdef __USE_LARGEFILE64
124 extern FILE *tmpfile64 __P ((void));
126 /* Generate a temporary filename. */
127 extern char *tmpnam __P ((char *__s));
130 /* This is the reentrant variant of `tmpnam'. The only difference is
131 that it does not allow S to be NULL. */
132 extern char *tmpnam_r __P ((char *__s));
136 #if defined __USE_SVID || defined __USE_XOPEN
137 /* Generate a unique temporary filename using up to five characters of PFX
138 if it is not NULL. The directory to put this file in is searched for
139 as follows: First the environment variable "TMPDIR" is checked.
140 If it contains the name of a writable directory, that directory is used.
141 If not and if DIR is not NULL, that value is checked. If that fails,
142 P_tmpdir is tried and finally "/tmp". The storage for the filename
143 is allocated by `malloc'. */
144 extern char *tempnam __P ((__const char *__dir, __const char *__pfx));
149 extern int fclose __P ((FILE *__stream));
150 /* Flush STREAM, or all streams if STREAM is NULL. */
151 extern int fflush __P ((FILE *__stream));
153 /* Open a file and create a new stream for it. */
154 extern FILE *fopen __P ((__const char *__restrict __filename,
155 __const char *__restrict __modes));
156 /* Used internally to actuall open files */
157 extern FILE *__fopen __P((__const char *__restrict __filename, int __fd,
158 FILE *__restrict __stream, __const char *__restrict __modes));
159 #define fopen(__file, __mode) __fopen((__file), -1, (FILE*)0, (__mode))
160 /* Open a file, replacing an existing stream with it. */
161 extern FILE *freopen __P ((__const char *__restrict __filename,
162 __const char *__restrict __modes,
163 FILE *__restrict __stream));
164 #define freopen(__file, __mode, __fp) __fopen((__file), -1, (__fp), (__mode))
166 #ifdef __USE_LARGEFILE64
167 extern FILE *fopen64 __P ((__const char *__restrict __filename,
168 __const char *__restrict __modes));
169 extern FILE *freopen64 __P ((__const char *__restrict __filename,
170 __const char *__restrict __modes,
171 FILE *__restrict __stream));
175 /* Create a new stream that refers to an existing system file descriptor. */
176 extern FILE *fdopen __P ((int __fd, __const char *__modes));
177 #define fdopen(__file, __mode) __fopen((char*)0, (__file), (FILE*)0, (__mode))
181 /* If BUF is NULL, make STREAM unbuffered.
182 Else make it use buffer BUF, of size BUFSIZ. */
183 extern void setbuf __P ((FILE *__restrict __stream, char *__restrict __buf));
184 #define setbuf(__fp, __buf) setbuffer((__fp), (__buf), BUFSIZ)
186 /* Make STREAM use buffering mode MODE.
187 If BUF is not NULL, use N bytes of it for buffering;
188 else allocate an internal buffer N bytes long. */
189 extern int setvbuf __P ((FILE *__restrict __stream, char *__restrict __buf,
190 int __modes, size_t __n));
193 /* If BUF is NULL, make STREAM unbuffered.
194 Else make it use SIZE bytes of BUF for buffering. */
195 extern void setbuffer __P ((FILE *__restrict __stream, char *__restrict __buf,
198 /* Make STREAM line-buffered. */
199 extern void setlinebuf __P ((FILE *__stream));
200 #define setlinebuf(__fp) setvbuf((__fp), (char*)0, _IOLBF, 0)
204 /* Write formatted output to STREAM. */
205 extern int fprintf __P ((FILE *__restrict __stream,
206 __const char *__restrict __format, ...));
207 /* Write formatted output to stdout. */
208 extern int printf __P ((__const char *__restrict __format, ...));
209 /* Write formatted output to S. */
210 extern int sprintf __P ((char *__restrict __s,
211 __const char *__restrict __format, ...));
213 /* Write formatted output to S from argument list ARG. */
214 extern int vfprintf __P ((FILE *__restrict __s,
215 __const char *__restrict __format,
217 /* Write formatted output to stdout from argument list ARG. */
218 extern int vprintf __P ((__const char *__restrict __format,
220 /* Write formatted output to S from argument list ARG. */
221 extern int vsprintf __P ((char *__restrict __s,
222 __const char *__restrict __format,
225 /* Maximum chars of output to write in MAXLEN. */
226 extern int snprintf __P ((char *__restrict __s, size_t __maxlen,
227 __const char *__restrict __format, ...))
228 __attribute__ ((__format__ (__printf__, 3, 4)));
230 extern int __vsnprintf __P ((char *__restrict __s, size_t __maxlen,
231 __const char *__restrict __format,
233 __attribute__ ((__format__ (__printf__, 3, 0)));
234 extern int vsnprintf __P ((char *__restrict __s, size_t __maxlen,
235 __const char *__restrict __format,
237 __attribute__ ((__format__ (__printf__, 3, 0)));
239 /* Read formatted input from STREAM. */
240 extern int fscanf __P ((FILE *__restrict __stream,
241 __const char *__restrict __format, ...));
242 /* Read formatted input from stdin. */
243 extern int scanf __P ((__const char *__restrict __format, ...));
244 /* Read formatted input from S. */
245 extern int sscanf __P ((__const char *__restrict __s,
246 __const char *__restrict __format, ...));
248 /* Read formatted input from S into argument list ARG. */
249 extern int vfscanf __P ((FILE *__restrict __s,
250 __const char *__restrict __format,
252 __attribute__ ((__format__ (__scanf__, 2, 0)));
254 /* Read formatted input from stdin into argument list ARG. */
255 extern int vscanf __P ((__const char *__restrict __format, va_list __arg))
256 __attribute__ ((__format__ (__scanf__, 1, 0)));
258 /* Read formatted input from S into argument list ARG. */
259 extern int vsscanf __P ((__const char *__restrict __s,
260 __const char *__restrict __format,
262 __attribute__ ((__format__ (__scanf__, 2, 0)));
265 /* Read a character from STREAM. */
266 extern int fgetc __P ((FILE *__stream));
267 extern int getc __P ((FILE *__stream));
269 /* Read a character from stdin. */
270 extern int getchar __P ((void));
271 #define getchar() getc(stdin)
273 /* The C standard explicitly says this is a macro, so be that way */
274 #define getc(stream) \
275 (((stream)->bufpos >= (stream)->bufread) ? fgetc(stream): \
276 (*(stream)->bufpos++))
278 /* Write a character to STREAM. */
279 extern int fputc __P ((int __c, FILE *__stream));
280 extern int putc __P ((int __c, FILE *__stream));
282 /* Write a character to stdout. */
283 extern int putchar __P ((int __c));
284 #define putchar(c) putc((c), stdout)
286 /* The C standard explicitly says this can be a macro, so be that way */
287 #define putc(c, stream) \
288 (((stream)->bufpos >= (stream)->bufwrite) ? fputc((c), (stream)) \
289 : (unsigned char) (*(stream)->bufpos++ = (c)) )
291 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
292 /* Get a word (int) from STREAM. */
293 extern int getw __P ((FILE *__stream));
295 /* Write a word (int) to STREAM. */
296 extern int putw __P ((int __w, FILE *__stream));
300 /* Get a newline-terminated string of finite length from STREAM. */
301 extern char *fgets __P ((char *__restrict __s, int __n,
302 FILE *__restrict __stream));
304 /* Get a newline-terminated string from stdin, removing the newline.
305 DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
306 extern char *gets __P ((char *__s));
309 /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
310 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
311 NULL), pointing to *N characters of space. It is realloc'd as
312 necessary. Returns the number of characters read (not including the
313 null terminator), or -1 on error or EOF. */
314 extern ssize_t __getdelim __P ((char **__restrict __lineptr,
315 size_t *__restrict __n, int __delimiter,
316 FILE *__restrict __stream));
317 extern ssize_t getdelim __P ((char **__restrict __lineptr,
318 size_t *__restrict __n, int __delimiter,
319 FILE *__restrict __stream));
321 /* Like `getdelim', but reads up to a newline. */
322 extern ssize_t getline __P ((char **__restrict __lineptr,
323 size_t *__restrict __n,
324 FILE *__restrict __stream));
327 /* Write a string to STREAM. */
328 extern int fputs __P ((__const char *__restrict __s,
329 FILE *__restrict __stream));
331 /* Write a string, followed by a newline, to stdout. */
332 extern int puts __P ((__const char *__s));
335 /* Push a character back onto the input buffer of STREAM. */
336 extern int ungetc __P ((int __c, FILE *__stream));
339 /* Read chunks of generic data from STREAM. */
340 extern size_t fread __P ((void *__restrict __ptr, size_t __size,
341 size_t __n, FILE *__restrict __stream));
342 /* Write chunks of generic data to STREAM. */
343 extern size_t fwrite __P ((__const void *__restrict __ptr, size_t __size,
344 size_t __n, FILE *__restrict __s));
346 /* Seek to a certain position on STREAM. */
347 extern int fseek __P ((FILE *__stream, long int __off, int __whence));
348 /* Return the current position of STREAM. */
349 extern long int ftell __P ((FILE *__stream));
350 /* Rewind to the beginning of STREAM. */
351 extern void rewind __P ((FILE *__stream));
353 /* The Single Unix Specification, Version 2, specifies an alternative,
354 more adequate interface for the two functions above which deal with
355 file offset. `long int' is not the right type. These definitions
356 are originally defined in the Large File Support API. */
358 /* Types needed in these functions. */
360 typedef __off_t off_t;
364 #if defined __USE_LARGEFILE64 && !defined off64_t
365 typedef __off64_t off64_t;
366 # define off64_t off64_t
370 /* Clear the error and EOF indicators for STREAM. */
371 extern void clearerr __P ((FILE *__stream));
372 #define clearerr(fp) ((fp)->mode &= ~(__MODE_EOF|__MODE_ERR),0)
373 /* Return the EOF indicator for STREAM. */
374 extern int feof __P ((FILE *__stream));
375 #define feof(fp) (((fp)->mode&__MODE_EOF) != 0)
376 /* Return the error indicator for STREAM. */
377 extern int ferror __P ((FILE *__stream));
378 #define ferror(fp) (((fp)->mode&__MODE_ERR) != 0)
380 /* Print a message describing the meaning of the value of errno. */
381 extern void perror __P ((__const char *__s));
383 /* These variables normally should not be used directly. The `strerror'
384 function provides all the needed functionality. */
386 extern __const char *__const sys_errlist[];
389 /* Return the system file descriptor for STREAM. */
390 extern int fileno __P ((FILE *__stream));
391 #define fileno(fp) ((fp)->fd)
392 #endif /* Use POSIX. */
394 #if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \
396 /* Create a new stream connected to a pipe running the given command. */
397 extern FILE *popen __P ((__const char *__command, __const char *__modes));
399 /* Close a stream opened by popen and return the status of its child. */
400 extern int pclose __P ((FILE *__stream));
405 /* Return the name of the controlling terminal. */
406 extern char *ctermid __P ((char *__s));
407 #endif /* Use POSIX. */
411 /* Return the name of the current user. */
412 extern char *cuserid __P ((char *__s));
413 #endif /* Use X/Open. */
416 #if defined __USE_POSIX || defined __USE_MISC
417 /* These are defined in POSIX.1:1996. */
419 /* Acquire ownership of STREAM. */
420 extern void flockfile __P ((FILE *__stream));
422 /* Try to acquire ownership of STREAM but do not block if it is not
424 extern int ftrylockfile __P ((FILE *__stream));
426 /* Relinquish the ownership granted for STREAM. */
427 extern void funlockfile __P ((FILE *__stream));
428 #endif /* POSIX || misc */
430 #if defined __USE_XOPEN && !defined __USE_GNU
431 /* The X/Open standard requires some functions and variables to be
432 declared here which do not belong into this header. But we have to
433 follow. In GNU mode we don't do this nonsense. */
434 # define __need_getopt
438 /* If we are compiling with optimizing read this file. It contains
439 several optizing inline functions and macros. */
440 #ifdef __USE_EXTERN_INLINES
441 # include <bits/stdio.h>
446 #endif /* !_STDIO_H */