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