nrelease/Makefile: Fix 'pkg_${PKG}' to 'PKG_${PKG}' in a message
[dragonfly.git] / include / stdio.h
1 /*-
2  * Copyright (c) 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Torek.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      @(#)stdio.h     8.5 (Berkeley) 4/29/95
33  * $FreeBSD: src/include/stdio.h,v 1.78 2009/03/25 08:07:52 das Exp $
34  */
35
36 #ifndef _STDIO_H_
37 #define _STDIO_H_
38
39 #include <sys/cdefs.h>
40 #include <sys/_null.h>
41 #include <sys/types.h>
42 #include <machine/stdarg.h> /* for __va_list */
43
44 typedef __off_t         fpos_t;
45
46 #ifndef _SIZE_T_DECLARED
47 typedef __size_t        size_t;
48 #define _SIZE_T_DECLARED
49 #endif
50
51 #if __EXT1_VISIBLE
52 #ifndef _RSIZE_T_DECLARED
53 #define _RSIZE_T_DECLARED
54 typedef size_t          rsize_t;
55 #endif
56 #endif
57
58 #if __POSIX_VISIBLE >= 200809
59 #ifndef _OFF_T_DECLARED
60 #define _OFF_T_DECLARED
61 typedef __off_t         off_t;
62 #endif
63 #ifndef _SSIZE_T_DECLARED
64 #define _SSIZE_T_DECLARED
65 typedef __ssize_t       ssize_t;
66 #endif
67 #endif
68
69 #if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
70 #ifdef __GNUC__
71 #ifndef _VA_LIST_DECLARED
72 #define _VA_LIST_DECLARED
73 typedef __va_list       va_list;
74 /* __gnuc_va_list is not needed, provided by <machine/stdarg.h> */
75 #endif
76 #else
77 #include <stdarg.h>
78 #endif
79 #endif
80
81 #define _FSTDIO                 /* Define for new stdio with functions. */
82
83 /*
84  * stdio state variables.
85  *
86  * The following always hold:
87  *
88  *      if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
89  *              _lbfsize is -_bf._size, else _lbfsize is 0
90  *      if _flags&__SRD, _w is 0
91  *      if _flags&__SWR, _r is 0
92  *
93  * This ensures that the getc and putc macros (or inline functions) never
94  * try to write or read from a file that is in `read' or `write' mode.
95  * (Moreover, they can, and do, automatically switch from read mode to
96  * write mode, and back, on "r+" and "w+" files.)
97  *
98  * _lbfsize is used only to make the inline line-buffered output stream
99  * code as compact as possible.
100  *
101  * WARNING: Do not change the order of the fields in __FILE_public!
102  */
103 #ifndef _STDFILE_DECLARED
104 #define _STDFILE_DECLARED
105 typedef struct __FILE FILE;
106 #endif
107
108 struct __FILE_public {
109         unsigned char   *_p;            /* current position in (some) buffer */
110         int             _flags;         /* flags, below; this FILE is free if 0 */
111         int             _fileno;        /* fileno, if Unix descriptor, else -1 */
112         __ssize_t       _r;             /* read space left for getc() */
113         __ssize_t       _w;             /* write space left for putc() */
114         __ssize_t       _lbfsize;       /* 0 or -_bf._size, for inline putc */
115 };
116
117 #ifndef _STDSTREAM_DECLARED
118 __BEGIN_DECLS
119 extern FILE *__stdinp;
120 extern FILE *__stdoutp;
121 extern FILE *__stderrp;
122 __END_DECLS
123 #define _STDSTREAM_DECLARED
124 #endif
125
126 #define __SLBF  0x0001          /* line buffered */
127 #define __SNBF  0x0002          /* unbuffered */
128 #define __SRD   0x0004          /* OK to read */
129 #define __SWR   0x0008          /* OK to write */
130         /* RD and WR are never simultaneously asserted */
131 #define __SRW   0x0010          /* open for reading & writing */
132 #define __SEOF  0x0020          /* found EOF */
133 #define __SERR  0x0040          /* found error */
134 #define __SMBF  0x0080          /* _buf is from malloc */
135 #define __SAPP  0x0100          /* fdopen()ed in append mode */
136 #define __SSTR  0x0200          /* this is an sprintf/snprintf string */
137 #define __SOPT  0x0400          /* do fseek() optimization */
138 #define __SNPT  0x0800          /* do not do fseek() optimization */
139 #define __SOFF  0x1000          /* set iff _offset is in fact correct */
140 #define __SMOD  0x2000          /* true => fgetln modified _p text */
141 #define __SALC  0x4000          /* allocate string space dynamically */
142 #define __SIGN  0x8000          /* ignore this file in _fwalk */
143
144 /*
145  * The following three definitions are for ANSI C, which took them
146  * from System V, which brilliantly took internal interface macros and
147  * made them official arguments to setvbuf(), without renaming them.
148  * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
149  *
150  * Although numbered as their counterparts above, the implementation
151  * does not rely on this.
152  */
153 #define _IOFBF  0               /* setvbuf should set fully buffered */
154 #define _IOLBF  1               /* setvbuf should set line buffered */
155 #define _IONBF  2               /* setvbuf should set unbuffered */
156
157 #define BUFSIZ  1024            /* size of buffer used by setbuf */
158 #define EOF     (-1)
159
160 /*
161  * FOPEN_MAX is a minimum maximum, and is the number of streams that
162  * stdio can provide without attempting to allocate further resources
163  * (which could fail).  Do not use this for anything.
164  */
165                                 /* must be == _POSIX_STREAM_MAX <limits.h> */
166 #ifndef FOPEN_MAX
167 #define FOPEN_MAX       20      /* must be <= OPEN_MAX <sys/syslimits.h> */
168 #endif
169 #define FILENAME_MAX    1024    /* must be <= PATH_MAX <sys/syslimits.h> */
170
171 /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
172 #if __XSI_VISIBLE
173 #define P_tmpdir        "/tmp/"
174 #endif
175 #define L_tmpnam        1024    /* XXX must be == PATH_MAX */
176 #define TMP_MAX         308915776
177
178 /* Always ensure that these are consistent with <fcntl.h> and <unistd.h>! */
179 #ifndef SEEK_SET
180 #define SEEK_SET        0       /* set file offset to offset */
181 #endif
182 #ifndef SEEK_CUR
183 #define SEEK_CUR        1       /* set file offset to current plus offset */
184 #endif
185 #ifndef SEEK_END
186 #define SEEK_END        2       /* set file offset to EOF plus offset */
187 #endif
188
189 #define stdin   __stdinp
190 #define stdout  __stdoutp
191 #define stderr  __stderrp
192
193 __BEGIN_DECLS
194 /*
195  * Functions defined in ANSI C standard.
196  */
197 void     clearerr(FILE *);
198 int      fclose(FILE *);
199 int      feof(FILE *);
200 int      ferror(FILE *);
201 int      fflush(FILE *);
202 int      fgetc(FILE *);
203 int      fgetpos(FILE * __restrict, fpos_t * __restrict);
204 char    *fgets(char * __restrict, int, FILE * __restrict);
205 FILE    *fopen(const char * __restrict, const char * __restrict);
206 int      fprintf(FILE * __restrict, const char * __restrict, ...)
207              __printflike(2, 3);
208 int      fputc(int, FILE *);
209 int      fputs(const char * __restrict, FILE * __restrict);
210 size_t   fread(void * __restrict, size_t, size_t, FILE * __restrict);
211 FILE    *freopen(const char * __restrict, const char * __restrict,
212                  FILE * __restrict);
213 int      fscanf(FILE * __restrict, const char * __restrict, ...)
214              __scanflike(2, 3);
215 int      fseek(FILE *, long, int);
216 int      fsetpos(FILE *, const fpos_t *);
217 long     ftell(FILE *);
218 size_t   fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
219 int      getc(FILE *);
220 int      getchar(void);
221 char    *gets(char *);
222 #if __EXT1_VISIBLE
223 char    *gets_s(char *, rsize_t);
224 #endif
225 void     perror(const char *);
226 int      printf(const char * __restrict, ...) __printflike(1, 2);
227 int      putc(int, FILE *);
228 int      putchar(int);
229 int      puts(const char *);
230 int      remove(const char *);
231 int      rename(const char *, const char *);
232 void     rewind(FILE *);
233 int      scanf(const char * __restrict, ...) __scanflike(1, 2);
234 void     setbuf(FILE * __restrict, char * __restrict);
235 int      setvbuf(FILE * __restrict, char * __restrict, int, size_t);
236 int      sprintf(char * __restrict, const char * __restrict, ...)
237              __printflike(2, 3);
238 int      sscanf(const char * __restrict, const char * __restrict, ...)
239              __scanflike(2, 3);
240 FILE    *tmpfile(void);
241 char    *tmpnam(char *);
242 int      ungetc(int, FILE *);
243 int      vfprintf(FILE * __restrict, const char * __restrict, __va_list)
244              __printflike(2, 0);
245 int      vprintf(const char * __restrict, __va_list) __printflike(1, 0);
246 int      vsprintf(char * __restrict, const char * __restrict, __va_list)
247              __printflike(2, 0);
248
249 #if __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE >= 500
250 int      snprintf(char * __restrict, size_t, const char * __restrict, ...)
251             __printflike(3, 4);
252 int      vsnprintf(char * __restrict, size_t, const char * __restrict,
253                    __va_list) __printflike(3, 0);
254 #endif
255 #if __ISO_C_VISIBLE >= 1999
256 int      vfscanf(FILE * __restrict, const char * __restrict, __va_list)
257             __scanflike(2, 0);
258 int      vscanf(const char * __restrict, __va_list) __scanflike(1, 0);
259 int      vsscanf(const char * __restrict, const char * __restrict, __va_list)
260             __scanflike(2, 0);
261 #endif
262
263 /*
264  * Functions defined in all versions of POSIX 1003.1.
265  */
266 #if __BSD_VISIBLE || (__POSIX_VISIBLE && __POSIX_VISIBLE < 200112)
267 #define L_cuserid       17      /* size for cuserid(3); MAXLOGNAME, legacy */
268 #endif
269
270 #if __POSIX_VISIBLE
271 #define L_ctermid       1024    /* size for ctermid(3); PATH_MAX */
272
273 char    *ctermid(char *);
274 FILE    *fdopen(int, const char *);
275 int      fileno(FILE *);
276 #endif /* __POSIX_VISIBLE */
277
278 #if __POSIX_VISIBLE >= 199209
279 int      pclose(FILE *);
280 FILE    *popen(const char *, const char *);
281 #endif
282
283 #if __POSIX_VISIBLE >= 199506
284 int      ftrylockfile(FILE *);
285 void     flockfile(FILE *);
286 void     funlockfile(FILE *);
287
288 /*
289  * These are normally used through macros as defined below, but POSIX
290  * requires functions as well.
291  */
292 int      getc_unlocked(FILE *);
293 int      getchar_unlocked(void);
294 int      putc_unlocked(int, FILE *);
295 int      putchar_unlocked(int);
296 #endif
297 #if __BSD_VISIBLE
298 void     clearerr_unlocked(FILE *);
299 int      feof_unlocked(FILE *);
300 int      ferror_unlocked(FILE *);
301 int      fileno_unlocked(FILE *);
302 #endif
303
304 #if __XSI_VISIBLE >= 500 || __POSIX_VISIBLE >= 200112
305 int      fseeko(FILE *, __off_t, int);
306 __off_t  ftello(FILE *);
307 #endif
308
309 #if __BSD_VISIBLE || (__XSI_VISIBLE && __XSI_VISIBLE < 600)
310 int      getw(FILE *);
311 int      putw(int, FILE *);
312 #endif /* BSD or X/Open before issue 6 */
313
314 #if __XSI_VISIBLE
315 char    *tempnam(const char *, const char *);
316 #endif
317
318 #if __POSIX_VISIBLE >= 200809
319 FILE    *fmemopen(void *__restrict, size_t, const char *__restrict);
320 ssize_t  getdelim(char ** __restrict, size_t * __restrict, int,
321                   FILE * __restrict);
322 FILE    *open_memstream(char **, size_t *);
323 int      renameat(int, const char *, int, const char *);
324 int      vdprintf(int, const char * __restrict, __va_list) __printflike(2, 0);
325 int      dprintf(int, const char * __restrict, ...) __printflike(2, 3);
326 ssize_t  getline(char ** __restrict, size_t * __restrict, FILE * __restrict);
327 #endif /* __POSIX_VISIBLE >= 200809 */
328
329 /*
330  * Routines that are purely local.
331  */
332 #if __BSD_VISIBLE
333 int      asprintf(char **, const char *, ...) __printflike(2, 3);
334 char    *ctermid_r(char *);
335 void     fcloseall(void);
336 void    *fcookie(FILE *);
337 char    *fgetln(FILE *, size_t *);
338 const char *fmtcheck(const char *, const char *) __format_arg(2);
339 __ssize_t __fpending(const FILE *);
340 int      fpurge(FILE *);
341 void     setbuffer(FILE *, char *, int);
342 int      setlinebuf(FILE *);
343 int      vasprintf(char **, const char *, __va_list) __printflike(2, 0);
344
345 /*
346  * The system error table contains messages for the first sys_nerr
347  * positive errno values.  Use strerror() or strerror_r() from <string.h>
348  * instead.
349  */
350 extern const int sys_nerr;
351 extern const char *const sys_errlist[];
352
353 /*
354  * Stdio function-access interface.
355  */
356 FILE    *funopen(const void *, int (*)(void *, char *, int),
357                  int (*)(void *, const char *, int),
358                  fpos_t (*)(void *, fpos_t, int), int (*)(void *));
359 #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
360 #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
361 #endif /* __BSD_VISIBLE */
362
363 /*
364  * Functions internal to the implementation.
365  */
366 int     __srget(FILE *);
367 int     __swbuf(int, FILE *);
368
369 /*
370  * The __sfoo functions are here so that we can
371  * define real function versions in the C library.
372  */
373 static __inline __always_inline int
374 __sgetc(FILE *_fp)
375 {
376         struct __FILE_public *_p = (struct __FILE_public *)_fp;
377
378         if (--_p->_r < 0)
379                 return (__srget(_fp));
380         else
381                 return (*_p->_p++);
382 }
383
384 static __inline __always_inline int
385 __sputc(int _c, FILE *_fp)
386 {
387         struct __FILE_public *_p = (struct __FILE_public *)_fp;
388
389         if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && _c != '\n'))
390                 return (*_p->_p++ = _c);
391         else
392                 return (__swbuf(_c, _fp));
393 }
394
395 static __inline __always_inline int
396 __sfeof(FILE *_fp)
397 {
398         struct __FILE_public *_p = (struct __FILE_public *)_fp;
399
400         return ((_p->_flags & __SEOF) != 0);
401 }
402
403 static __inline __always_inline int
404 __sferror(FILE *_fp)
405 {
406         struct __FILE_public *_p = (struct __FILE_public *)_fp;
407
408         return ((_p->_flags & __SERR) != 0);
409 }
410
411 static __inline __always_inline void
412 __sclearerr(FILE *_fp)
413 {
414         struct __FILE_public *_p = (struct __FILE_public *)_fp;
415
416         _p->_flags &= ~(__SERR|__SEOF);
417 }
418
419 static __inline __always_inline int
420 __sfileno(FILE *_fp)
421 {
422         struct __FILE_public *_p = (struct __FILE_public *)_fp;
423
424         return (_p->_fileno);
425 }
426
427 #ifndef __LIBC_ISTHREADED_DECLARED
428 #define __LIBC_ISTHREADED_DECLARED
429 extern int __isthreaded;
430 #endif
431
432 #ifndef __cplusplus
433 #define feof(p)         (!__isthreaded ? __sfeof(p) : (feof)(p))
434 #define ferror(p)       (!__isthreaded ? __sferror(p) : (ferror)(p))
435 #define clearerr(p)     (!__isthreaded ? __sclearerr(p) : (clearerr)(p))
436
437 #if __POSIX_VISIBLE
438 #define fileno(p)       (!__isthreaded ? __sfileno(p) : (fileno)(p))
439 #endif
440
441 #define getc(fp)        (!__isthreaded ? __sgetc(fp) : (getc)(fp))
442 #define putc(x, fp)     (!__isthreaded ? __sputc(x, fp) : (putc)(x, fp))
443
444 #define getchar()       getc(stdin)
445 #define putchar(x)      putc(x, stdout)
446
447 #if __BSD_VISIBLE
448 /*
449  * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
450  * B.8.2.7 for the rationale behind the *_unlocked() macros.
451  */
452 #define feof_unlocked(p)        __sfeof(p)
453 #define ferror_unlocked(p)      __sferror(p)
454 #define clearerr_unlocked(p)    __sclearerr(p)
455 #define fileno_unlocked(p)      __sfileno(p)
456 #endif
457 #if __POSIX_VISIBLE >= 199506
458 #define getc_unlocked(fp)       __sgetc(fp)
459 #define putc_unlocked(x, fp)    __sputc(x, fp)
460
461 #define getchar_unlocked()      getc_unlocked(stdin)
462 #define putchar_unlocked(x)     putc_unlocked(x, stdout)
463 #endif
464 #endif /* __cplusplus */
465
466 __END_DECLS
467 #endif /* !_STDIO_H_ */