OSDN Git Service

Increase BUFSIZ to 512 to avoid bug in busybox tar.
[uclinux-h8/uClibc.git] / include / stdio.h
1 /* Define ISO C stdio on top of C++ iostreams.
2    Copyright (C) 1991, 1994-1999, 2000 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
18
19 /*
20  *      ISO C Standard: 4.9 INPUT/OUTPUT        <stdio.h>
21  */
22
23 #ifndef _STDIO_H
24 #define _STDIO_H
25
26 #include <features.h>
27 #include <stdarg.h>
28 #include <sys/types.h>
29
30 __BEGIN_DECLS
31
32
33 /* when you add or change fields here, be sure to change the initialization
34  * in stdio_init and fopen */
35 struct __stdio_file {
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
41                               malloc()ed byte */
42
43   int fd; /* the file descriptor associated with the stream */
44   int mode;
45
46   char unbuf[8];           /* The buffer for 'unbuffered' streams */
47
48   struct __stdio_file * next;
49 };
50
51 typedef struct __stdio_file FILE;
52
53 /* Default buffer size.  */
54 #define BUFSIZ      (512)
55
56 /* Define EOF and NULL */
57 #define EOF     (-1)
58 #ifndef NULL
59 #define NULL    (0)
60 #endif
61
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.  */
66
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 */
80
81
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.  */
87
88
89 #define stdio_pending(fp) ((fp)->bufread>(fp)->bufpos)
90
91
92
93 /* Default path prefix for `tempnam' and `tmpnam'.  */
94 #define P_tmpdir        "/tmp"
95 /* Get the values:
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
107
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)
114
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));
119
120
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));
125 #endif
126 /* Generate a temporary filename.  */
127 extern char *tmpnam __P ((char *__s));
128
129 #ifdef __USE_MISC
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));
133 #endif
134
135
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));
145 #endif
146
147
148 /* Close STREAM.  */
149 extern int fclose __P ((FILE *__stream));
150 /* Flush STREAM, or all streams if STREAM is NULL.  */
151 extern int fflush __P ((FILE *__stream));
152
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))
165
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));
172 #endif
173
174 #ifdef  __USE_POSIX
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))
178 #endif
179
180
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)
185
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));
191
192 #ifdef  __USE_BSD
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,
196                             size_t __size));
197
198 /* Make STREAM line-buffered.  */
199 extern void setlinebuf __P ((FILE *__stream));
200 #define setlinebuf(__fp)             setvbuf((__fp), (char*)0, _IOLBF, 0)
201 #endif
202
203
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, ...));
212
213 /* Write formatted output to S from argument list ARG.  */
214 extern int vfprintf __P ((FILE *__restrict __s,
215                           __const char *__restrict __format,
216                           va_list __arg));
217 /* Write formatted output to stdout from argument list ARG.  */
218 extern int vprintf __P ((__const char *__restrict __format,
219                          va_list __arg));
220 /* Write formatted output to S from argument list ARG.  */
221 extern int vsprintf __P ((char *__restrict __s,
222                           __const char *__restrict __format,
223                           va_list __arg));
224
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)));
229
230 extern int __vsnprintf __P ((char *__restrict __s, size_t __maxlen,
231                              __const char *__restrict __format,
232                              va_list __arg))
233      __attribute__ ((__format__ (__printf__, 3, 0)));
234 extern int vsnprintf __P ((char *__restrict __s, size_t __maxlen,
235                            __const char *__restrict __format,
236                            va_list __arg))
237      __attribute__ ((__format__ (__printf__, 3, 0)));
238
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, ...));
247
248 /* Read formatted input from S into argument list ARG.  */
249 extern int vfscanf __P ((FILE *__restrict __s,
250                          __const char *__restrict __format,
251                          va_list __arg))
252      __attribute__ ((__format__ (__scanf__, 2, 0)));
253
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)));
257
258 /* Read formatted input from S into argument list ARG.  */
259 extern int vsscanf __P ((__const char *__restrict __s,
260                          __const char *__restrict __format,
261                          va_list __arg))
262      __attribute__ ((__format__ (__scanf__, 2, 0)));
263
264
265 /* Read a character from STREAM.  */
266 extern int fgetc __P ((FILE *__stream));
267 extern int getc __P ((FILE *__stream));
268
269 /* Read a character from stdin.  */
270 extern int getchar __P ((void));
271 #define getchar() getc(stdin)
272
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++))
277
278 /* Write a character to STREAM.  */
279 extern int fputc __P ((int __c, FILE *__stream));
280 extern int putc __P ((int __c, FILE *__stream));
281
282 /* Write a character to stdout.  */
283 extern int putchar __P ((int __c));
284 #define putchar(c) putc((c), stdout)  
285
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)) )
290
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));
294
295 /* Write a word (int) to STREAM.  */
296 extern int putw __P ((int __w, FILE *__stream));
297 #endif
298
299
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));
303
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));
307
308
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));
320
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));
325
326
327 /* Write a string to STREAM.  */
328 extern int fputs __P ((__const char *__restrict __s,
329                        FILE *__restrict __stream));
330
331 /* Write a string, followed by a newline, to stdout.  */
332 extern int puts __P ((__const char *__s));
333
334
335 /* Push a character back onto the input buffer of STREAM.  */
336 extern int ungetc __P ((int __c, FILE *__stream));
337
338
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));
345
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));
352
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.  */
357
358 /* Types needed in these functions.  */
359 #ifndef off_t
360 typedef __off_t off_t;
361 # define off_t off_t
362 #endif
363
364 #if defined __USE_LARGEFILE64 && !defined off64_t
365 typedef __off64_t off64_t;
366 # define off64_t off64_t
367 #endif
368
369
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)
379
380 /* Print a message describing the meaning of the value of errno.  */
381 extern void perror __P ((__const char *__s));
382
383 /* These variables normally should not be used directly.  The `strerror'
384    function provides all the needed functionality.  */
385 extern int sys_nerr;
386 extern __const char *__const sys_errlist[];
387
388 #ifdef  __USE_POSIX
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.  */
393
394 #if (defined __USE_POSIX2 || defined __USE_SVID  || defined __USE_BSD || \
395      defined __USE_MISC)
396 /* Create a new stream connected to a pipe running the given command.  */
397 extern FILE *popen __P ((__const char *__command, __const char *__modes));
398
399 /* Close a stream opened by popen and return the status of its child.  */
400 extern int pclose __P ((FILE *__stream));
401 #endif
402
403
404 #ifdef  __USE_POSIX
405 /* Return the name of the controlling terminal.  */
406 extern char *ctermid __P ((char *__s));
407 #endif /* Use POSIX.  */
408
409
410 #ifdef __USE_XOPEN
411 /* Return the name of the current user.  */
412 extern char *cuserid __P ((char *__s));
413 #endif /* Use X/Open.  */
414
415
416 #if defined __USE_POSIX || defined __USE_MISC
417 /* These are defined in POSIX.1:1996.  */
418
419 /* Acquire ownership of STREAM.  */
420 extern void flockfile __P ((FILE *__stream));
421
422 /* Try to acquire ownership of STREAM but do not block if it is not
423    possible.  */
424 extern int ftrylockfile __P ((FILE *__stream));
425
426 /* Relinquish the ownership granted for STREAM.  */
427 extern void funlockfile __P ((FILE *__stream));
428 #endif /* POSIX || misc */
429
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
435 # include <getopt.h>
436 #endif
437
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>
442 #endif
443
444 __END_DECLS
445
446 #endif /* !_STDIO_H */