1a6ce65drgrimes/*-
26b22553pfg * SPDX-License-Identifier: BSD-3-Clause
36b22553pfg *
4a6ce65drgrimes * Copyright (c) 1990, 1993
5a6ce65drgrimes *	The Regents of the University of California.  All rights reserved.
6a6ce65drgrimes *
7a6ce65drgrimes * This code is derived from software contributed to Berkeley by
8a6ce65drgrimes * Chris Torek.
9a6ce65drgrimes *
10a6ce65drgrimes * Redistribution and use in source and binary forms, with or without
11a6ce65drgrimes * modification, are permitted provided that the following conditions
12a6ce65drgrimes * are met:
13a6ce65drgrimes * 1. Redistributions of source code must retain the above copyright
14a6ce65drgrimes *    notice, this list of conditions and the following disclaimer.
15a6ce65drgrimes * 2. Redistributions in binary form must reproduce the above copyright
16a6ce65drgrimes *    notice, this list of conditions and the following disclaimer in the
17a6ce65drgrimes *    documentation and/or other materials provided with the distribution.
187d79e30imp * 3. Neither the name of the University nor the names of its contributors
19a6ce65drgrimes *    may be used to endorse or promote products derived from this software
20a6ce65drgrimes *    without specific prior written permission.
21a6ce65drgrimes *
22a6ce65drgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23a6ce65drgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24a6ce65drgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25a6ce65drgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26a6ce65drgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27a6ce65drgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28a6ce65drgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29a6ce65drgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30a6ce65drgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31a6ce65drgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32a6ce65drgrimes * SUCH DAMAGE.
33a6ce65drgrimes *
349eaf75fpeter *	@(#)stdio.h	8.5 (Berkeley) 4/29/95
35d4e3ebapeter * $FreeBSD$
36a6ce65drgrimes */
37a6ce65drgrimes
38a6ce65drgrimes#ifndef	_STDIO_H_
39a6ce65drgrimes#define	_STDIO_H_
40a6ce65drgrimes
41a6ce65drgrimes#include <sys/cdefs.h>
4289b1ef2marcel#include <sys/_null.h>
439e6f796mike#include <sys/_types.h>
443db3e02bde
4574d3e29pfg__NULLABILITY_PRAGMA_PUSH
4674d3e29pfg
476b78701miketypedef	__off_t		fpos_t;
486b78701mike
499e6f796mike#ifndef _SIZE_T_DECLARED
509e6f796miketypedef	__size_t	size_t;
519e6f796mike#define	_SIZE_T_DECLARED
52a6ce65drgrimes#endif
53a6ce65drgrimes
54cfd2b9ccy#ifndef _RSIZE_T_DEFINED
55cfd2b9ccy#define _RSIZE_T_DEFINED
56cfd2b9ccytypedef size_t rsize_t;
57cfd2b9ccy#endif
58cfd2b9ccy
59e47308fjilles#if __POSIX_VISIBLE >= 200809
60a67fbaadas#ifndef _OFF_T_DECLARED
61a67fbaadas#define	_OFF_T_DECLARED
62a67fbaadastypedef	__off_t		off_t;
63a67fbaadas#endif
64a67fbaadas#ifndef _SSIZE_T_DECLARED
65a67fbaadas#define	_SSIZE_T_DECLARED
66a67fbaadastypedef	__ssize_t	ssize_t;
67a67fbaadas#endif
68a67fbaadas#endif
69a67fbaadas
7097a44a8cem#ifndef _OFF64_T_DECLARED
7197a44a8cem#define	_OFF64_T_DECLARED
72c482916cemtypedef	__off64_t	off64_t;
7397a44a8cem#endif
7497a44a8cem
75e47308fjilles#if __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
766b78701mike#ifndef _VA_LIST_DECLARED
776b78701miketypedef	__va_list	va_list;
786b78701mike#define	_VA_LIST_DECLARED
796b78701mike#endif
806b78701mike#endif
816b78701mike
82a6ce65drgrimes#define	_FSTDIO			/* Define for new stdio with functions. */
83a6ce65drgrimes
84d878a40jhb/*
85d878a40jhb * NB: to fit things in six character monocase externals, the stdio
86d878a40jhb * code uses the prefix `__s' for stdio objects, typically followed
87d878a40jhb * by a three-character attempt at a mnemonic.
88d878a40jhb */
89d878a40jhb
90d878a40jhb/* stdio buffers */
91d878a40jhbstruct __sbuf {
92d878a40jhb	unsigned char *_base;
93d878a40jhb	int	_size;
94d878a40jhb};
95d878a40jhb
96d878a40jhb/*
97d878a40jhb * stdio state variables.
98d878a40jhb *
99d878a40jhb * The following always hold:
100d878a40jhb *
101d878a40jhb *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
102d878a40jhb *		_lbfsize is -_bf._size, else _lbfsize is 0
103d878a40jhb *	if _flags&__SRD, _w is 0
104d878a40jhb *	if _flags&__SWR, _r is 0
105d878a40jhb *
106d878a40jhb * This ensures that the getc and putc macros (or inline functions) never
107d878a40jhb * try to write or read from a file that is in `read' or `write' mode.
108d878a40jhb * (Moreover, they can, and do, automatically switch from read mode to
109d878a40jhb * write mode, and back, on "r+" and "w+" files.)
110d878a40jhb *
111d878a40jhb * _lbfsize is used only to make the inline line-buffered output stream
112d878a40jhb * code as compact as possible.
113d878a40jhb *
114d878a40jhb * _ub, _up, and _ur are used when ungetc() pushes back more characters
115d878a40jhb * than fit in the current _bf, or when ungetc() pushes back a character
116d878a40jhb * that does not match the previous one in _bf.  When this happens,
117d878a40jhb * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
118d878a40jhb * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
119d878a40jhb *
120d878a40jhb * Certain members of __sFILE are accessed directly via macros or
121d878a40jhb * inline functions.  To preserve ABI compat, these members must not
122d878a40jhb * be disturbed.  These members are marked below with (*).
123d878a40jhb */
124dd1ce6ftheravenstruct __sFILE {
125d878a40jhb	unsigned char *_p;	/* (*) current position in (some) buffer */
126d878a40jhb	int	_r;		/* (*) read space left for getc() */
127d878a40jhb	int	_w;		/* (*) write space left for putc() */
128d878a40jhb	short	_flags;		/* (*) flags, below; this FILE is free if 0 */
129d878a40jhb	short	_file;		/* (*) fileno, if Unix descriptor, else -1 */
130c6ca1bfjhb	struct	__sbuf _bf;	/* (*) the buffer (at least 1 byte, if !NULL) */
131d878a40jhb	int	_lbfsize;	/* (*) 0 or -_bf._size, for inline putc */
132d878a40jhb
133d878a40jhb	/* operations */
134f0ddfb8jhb	void	*_cookie;	/* (*) cookie passed to io functions */
13574d3e29pfg	int	(* _Nullable _close)(void *);
13674d3e29pfg	int	(* _Nullable _read)(void *, char *, int);
13774d3e29pfg	fpos_t	(* _Nullable _seek)(void *, fpos_t, int);
13874d3e29pfg	int	(* _Nullable _write)(void *, const char *, int);
139d878a40jhb
140d878a40jhb	/* separate buffer for long sequences of ungetc() */
141d878a40jhb	struct	__sbuf _ub;	/* ungetc buffer */
142d878a40jhb	unsigned char	*_up;	/* saved _p when _p is doing ungetc data */
143d878a40jhb	int	_ur;		/* saved _r when _r is counting ungetc data */
144d878a40jhb
145d878a40jhb	/* tricks to meet minimum requirements even when malloc() fails */
146d878a40jhb	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
147d878a40jhb	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
148d878a40jhb
149d878a40jhb	/* separate buffer for fgetln() when line crosses buffer boundary */
150d878a40jhb	struct	__sbuf _lb;	/* buffer for fgetln() */
151d878a40jhb
152d878a40jhb	/* Unix stdio files get aligned to block boundaries on fseek() */
153d878a40jhb	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
154d878a40jhb	fpos_t	_offset;	/* current lseek offset */
155d878a40jhb
156d878a40jhb	struct pthread_mutex *_fl_mutex;	/* used for MT-safety */
157d878a40jhb	struct pthread *_fl_owner;	/* current owner */
158d878a40jhb	int	_fl_count;	/* recursive lock count */
159d878a40jhb	int	_orientation;	/* orientation for fwide() */
160d878a40jhb	__mbstate_t _mbstate;	/* multibyte conversion state */
16164dac23ache	int	_flags2;	/* additional flags */
162dd1ce6ftheraven};
163dd1ce6ftheraven#ifndef _STDFILE_DECLARED
164dd1ce6ftheraven#define _STDFILE_DECLARED
165dd1ce6ftheraventypedef struct __sFILE FILE;
166dd1ce6ftheraven#endif
167e1421e9tjr#ifndef _STDSTREAM_DECLARED
168a6ce65drgrimes__BEGIN_DECLS
1693e5894fpeterextern FILE *__stdinp;
1703e5894fpeterextern FILE *__stdoutp;
1713e5894fpeterextern FILE *__stderrp;
172a6ce65drgrimes__END_DECLS
173e1421e9tjr#define	_STDSTREAM_DECLARED
174e1421e9tjr#endif
175a6ce65drgrimes
176d878a40jhb#define	__SLBF	0x0001		/* line buffered */
177d878a40jhb#define	__SNBF	0x0002		/* unbuffered */
178d878a40jhb#define	__SRD	0x0004		/* OK to read */
179d878a40jhb#define	__SWR	0x0008		/* OK to write */
180d878a40jhb	/* RD and WR are never simultaneously asserted */
181d878a40jhb#define	__SRW	0x0010		/* open for reading & writing */
182d878a40jhb#define	__SEOF	0x0020		/* found EOF */
183d878a40jhb#define	__SERR	0x0040		/* found error */
184778a613bdrewery#define	__SMBF	0x0080		/* _bf._base is from malloc */
185d878a40jhb#define	__SAPP	0x0100		/* fdopen()ed in append mode */
186d878a40jhb#define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
187d878a40jhb#define	__SOPT	0x0400		/* do fseek() optimization */
188d878a40jhb#define	__SNPT	0x0800		/* do not do fseek() optimization */
189d878a40jhb#define	__SOFF	0x1000		/* set iff _offset is in fact correct */
190d878a40jhb#define	__SMOD	0x2000		/* true => fgetln modified _p text */
191d878a40jhb#define	__SALC	0x4000		/* allocate string space dynamically */
192d878a40jhb#define	__SIGN	0x8000		/* ignore this file in _fwalk */
193d878a40jhb
19464dac23ache#define	__S2OAP	0x0001		/* O_APPEND mode is set */
19564dac23ache
196a6ce65drgrimes/*
197a6ce65drgrimes * The following three definitions are for ANSI C, which took them
198a6ce65drgrimes * from System V, which brilliantly took internal interface macros and
199a6ce65drgrimes * made them official arguments to setvbuf(), without renaming them.
200a6ce65drgrimes * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
201d878a40jhb *
202d878a40jhb * Although numbered as their counterparts above, the implementation
203d878a40jhb * does not rely on this.
204a6ce65drgrimes */
205a6ce65drgrimes#define	_IOFBF	0		/* setvbuf should set fully buffered */
206a6ce65drgrimes#define	_IOLBF	1		/* setvbuf should set line buffered */
207a6ce65drgrimes#define	_IONBF	2		/* setvbuf should set unbuffered */
208a6ce65drgrimes
209a6ce65drgrimes#define	BUFSIZ	1024		/* size of buffer used by setbuf */
210a6ce65drgrimes#define	EOF	(-1)
211a6ce65drgrimes
212a6ce65drgrimes/*
213a6ce65drgrimes * FOPEN_MAX is a minimum maximum, and is the number of streams that
214a6ce65drgrimes * stdio can provide without attempting to allocate further resources
215a6ce65drgrimes * (which could fail).  Do not use this for anything.
216a6ce65drgrimes */
217a6ce65drgrimes				/* must be == _POSIX_STREAM_MAX <limits.h> */
2180a735e0jb#ifndef FOPEN_MAX
219a6ce65drgrimes#define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
2200a735e0jb#endif
221a6ce65drgrimes#define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
222a6ce65drgrimes
223a6ce65drgrimes/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
224c559b89wollman#if __XSI_VISIBLE
22541d91b4delphij#define	P_tmpdir	"/tmp/"
226a6ce65drgrimes#endif
227a6ce65drgrimes#define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
228a6ce65drgrimes#define	TMP_MAX		308915776
229a6ce65drgrimes
230a6ce65drgrimes#ifndef SEEK_SET
231a6ce65drgrimes#define	SEEK_SET	0	/* set file offset to offset */
232a6ce65drgrimes#endif
233a6ce65drgrimes#ifndef SEEK_CUR
234a6ce65drgrimes#define	SEEK_CUR	1	/* set file offset to current plus offset */
235a6ce65drgrimes#endif
236a6ce65drgrimes#ifndef SEEK_END
237a6ce65drgrimes#define	SEEK_END	2	/* set file offset to EOF plus offset */
238a6ce65drgrimes#endif
239a6ce65drgrimes
2403e5894fpeter#define	stdin	__stdinp
2413e5894fpeter#define	stdout	__stdoutp
2423e5894fpeter#define	stderr	__stderrp
243a6ce65drgrimes
244c559b89wollman__BEGIN_DECLS
245de5013dtheraven#ifdef _XLOCALE_H_
246de5013dtheraven#include <xlocale/_stdio.h>
247de5013dtheraven#endif
248a6ce65drgrimes/*
249a6ce65drgrimes * Functions defined in ANSI C standard.
250a6ce65drgrimes */
251300518cimpvoid	 clearerr(FILE *);
252300518cimpint	 fclose(FILE *);
253300518cimpint	 feof(FILE *);
254300518cimpint	 ferror(FILE *);
255300518cimpint	 fflush(FILE *);
256300518cimpint	 fgetc(FILE *);
2574d6bf1cmikeint	 fgetpos(FILE * __restrict, fpos_t * __restrict);
2584d6bf1cmikechar	*fgets(char * __restrict, int, FILE * __restrict);
2594d6bf1cmikeFILE	*fopen(const char * __restrict, const char * __restrict);
260cd5ca96tjrint	 fprintf(FILE * __restrict, const char * __restrict, ...);
261300518cimpint	 fputc(int, FILE *);
2624d6bf1cmikeint	 fputs(const char * __restrict, FILE * __restrict);
2634d6bf1cmikesize_t	 fread(void * __restrict, size_t, size_t, FILE * __restrict);
2644d6bf1cmikeFILE	*freopen(const char * __restrict, const char * __restrict, FILE * __restrict);
2654d6bf1cmikeint	 fscanf(FILE * __restrict, const char * __restrict, ...);
266300518cimpint	 fseek(FILE *, long, int);
267300518cimpint	 fsetpos(FILE *, const fpos_t *);
268300518cimplong	 ftell(FILE *);
2694d6bf1cmikesize_t	 fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
270300518cimpint	 getc(FILE *);
271300518cimpint	 getchar(void);
272de31a1acy#if __EXT1_VISIBLE
273cfd2b9ccychar	*gets_s(char *, rsize_t);
274cfd2b9ccy#endif
275300518cimpvoid	 perror(const char *);
276cd5ca96tjrint	 printf(const char * __restrict, ...);
277300518cimpint	 putc(int, FILE *);
278300518cimpint	 putchar(int);
279300518cimpint	 puts(const char *);
280300518cimpint	 remove(const char *);
281300518cimpint	 rename(const char *, const char *);
282300518cimpvoid	 rewind(FILE *);
2834d6bf1cmikeint	 scanf(const char * __restrict, ...);
284cd5ca96tjrvoid	 setbuf(FILE * __restrict, char * __restrict);
285cd5ca96tjrint	 setvbuf(FILE * __restrict, char * __restrict, int, size_t);
286cd5ca96tjrint	 sprintf(char * __restrict, const char * __restrict, ...);
2874d6bf1cmikeint	 sscanf(const char * __restrict, const char * __restrict, ...);
288300518cimpFILE	*tmpfile(void);
289300518cimpchar	*tmpnam(char *);
290300518cimpint	 ungetc(int, FILE *);
291cd5ca96tjrint	 vfprintf(FILE * __restrict, const char * __restrict,
2929e6f796mike	    __va_list);
293cd5ca96tjrint	 vprintf(const char * __restrict, __va_list);
294cd5ca96tjrint	 vsprintf(char * __restrict, const char * __restrict,
2959e6f796mike	    __va_list);
296c559b89wollman
29759c8399kib#if __ISO_C_VISIBLE >= 1999 || __POSIX_VISIBLE >= 199506
298cd5ca96tjrint	 snprintf(char * __restrict, size_t, const char * __restrict,
299eef3b2frobert	    ...) __printflike(3, 4);
30059c8399kibint	 vsnprintf(char * __restrict, size_t, const char * __restrict,
30159c8399kib	    __va_list) __printflike(3, 0);
30259c8399kib#endif
30359c8399kib#if __ISO_C_VISIBLE >= 1999
30483b297ftjrint	 vfscanf(FILE * __restrict, const char * __restrict, __va_list)
30583b297ftjr	    __scanflike(2, 0);
3064d6bf1cmikeint	 vscanf(const char * __restrict, __va_list) __scanflike(1, 0);
3074d6bf1cmikeint	 vsscanf(const char * __restrict, const char * __restrict, __va_list)
3086b78701mike	    __scanflike(2, 0);
309c559b89wollman#endif
310a6ce65drgrimes
311a6ce65drgrimes/*
312c559b89wollman * Functions defined in all versions of POSIX 1003.1.
313a6ce65drgrimes */
3146fa4f73jilles#if __BSD_VISIBLE || (__POSIX_VISIBLE && __POSIX_VISIBLE <= 199506)
315933d2e8ed#define	L_cuserid	17	/* size for cuserid(3); MAXLOGNAME, legacy */
3166b78701mike#endif
31786907d3wosch
3186b78701mike#if __POSIX_VISIBLE
31986907d3wosch#define	L_ctermid	1024	/* size for ctermid(3); PATH_MAX */
320a6ce65drgrimes
321300518cimpchar	*ctermid(char *);
322300518cimpFILE	*fdopen(int, const char *);
323300518cimpint	 fileno(FILE *);
324c559b89wollman#endif /* __POSIX_VISIBLE */
325c559b89wollman
326c559b89wollman#if __POSIX_VISIBLE >= 199209
327c559b89wollmanint	 pclose(FILE *);
328c559b89wollmanFILE	*popen(const char *, const char *);
329c559b89wollman#endif
330c559b89wollman
331c559b89wollman#if __POSIX_VISIBLE >= 199506
332300518cimpint	 ftrylockfile(FILE *);
333300518cimpvoid	 flockfile(FILE *);
334300518cimpvoid	 funlockfile(FILE *);
335a6ce65drgrimes
336a6ce65drgrimes/*
337d878a40jhb * These are normally used through macros as defined below, but POSIX
338d878a40jhb * requires functions as well.
3393db3e02bde */
340c559b89wollmanint	 getc_unlocked(FILE *);
341c559b89wollmanint	 getchar_unlocked(void);
342c559b89wollmanint	 putc_unlocked(int, FILE *);
343c559b89wollmanint	 putchar_unlocked(int);
3443db3e02bde#endif
345a8ca4b7tjr#if __BSD_VISIBLE
34669b7992jhbvoid	 clearerr_unlocked(FILE *);
34769b7992jhbint	 feof_unlocked(FILE *);
34869b7992jhbint	 ferror_unlocked(FILE *);
349a508eb3kevansint	 fflush_unlocked(FILE *);
35069b7992jhbint	 fileno_unlocked(FILE *);
35166a605bkevansint	 fputc_unlocked(int, FILE *);
352a508eb3kevansint	 fputs_unlocked(const char * __restrict, FILE * __restrict);
353a508eb3kevanssize_t	 fread_unlocked(void * __restrict, size_t, size_t, FILE * __restrict);
354a508eb3kevanssize_t	 fwrite_unlocked(const void * __restrict, size_t, size_t,
355a508eb3kevans    FILE * __restrict);
356a8ca4b7tjr#endif
357c559b89wollman
3587bdd7f8pfg#if __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE >= 500
3599e6f796mikeint	 fseeko(FILE *, __off_t, int);
3609e6f796mike__off_t	 ftello(FILE *);
3613db3e02bde#endif
362c559b89wollman
363c559b89wollman#if __BSD_VISIBLE || __XSI_VISIBLE > 0 && __XSI_VISIBLE < 600
364c559b89wollmanint	 getw(FILE *);
365c559b89wollmanint	 putw(int, FILE *);
366c559b89wollman#endif /* BSD or X/Open before issue 6 */
367c559b89wollman
368c559b89wollman#if __XSI_VISIBLE
369c559b89wollmanchar	*tempnam(const char *, const char *);
3703db3e02bde#endif
3713db3e02bde
372e47308fjilles#if __POSIX_VISIBLE >= 200809
373e7ca48dgahrFILE	*fmemopen(void * __restrict, size_t, const char * __restrict);
374a67fbaadasssize_t	 getdelim(char ** __restrict, size_t * __restrict, int,
375a67fbaadas	    FILE * __restrict);
3762b2e634jhbFILE	*open_memstream(char **, size_t *);
377ee4adcfdasint	 renameat(int, const char *, int, const char *);
378ef243d7vangyzenint	 vdprintf(int, const char * __restrict, __va_list) __printflike(2, 0);
379e925cabimp/* _WITH_GETLINE to allow pre 11 sources to build on 11+ systems */
380a67fbaadasssize_t	 getline(char ** __restrict, size_t * __restrict, FILE * __restrict);
3811542a38vangyzenint	 dprintf(int, const char * __restrict, ...) __printflike(2, 3);
382e47308fjilles#endif /* __POSIX_VISIBLE >= 200809 */
383a67fbaadas
3843db3e02bde/*
385a6ce65drgrimes * Routines that are purely local.
386a6ce65drgrimes */
387c559b89wollman#if __BSD_VISIBLE
388300518cimpint	 asprintf(char **, const char *, ...) __printflike(2, 3);
389300518cimpchar	*ctermid_r(char *);
39069b7992jhbvoid	 fcloseall(void);
391e1ffe0foshogboint	 fdclose(FILE *, int *);
392300518cimpchar	*fgetln(FILE *, size_t *);
39338c7b9fdelphijconst char *fmtcheck(const char *, const char *) __format_arg(2);
394300518cimpint	 fpurge(FILE *);
395300518cimpvoid	 setbuffer(FILE *, char *, int);
396300518cimpint	 setlinebuf(FILE *);
3979e6f796mikeint	 vasprintf(char **, const char *, __va_list)
398bf1a9b4bde	    __printflike(2, 0);
399c559b89wollman
400c559b89wollman/*
401c559b89wollman * The system error table contains messages for the first sys_nerr
402c559b89wollman * positive errno values.  Use strerror() or strerror_r() from <string.h>
403c559b89wollman * instead.
404c559b89wollman */
4050bcfe62edextern const int sys_nerr;
4060bcfe62edextern const char * const sys_errlist[];
407a6ce65drgrimes
408a6ce65drgrimes/*
409a6ce65drgrimes * Stdio function-access interface.
410a6ce65drgrimes */
411300518cimpFILE	*funopen(const void *,
41274d3e29pfg	    int (* _Nullable)(void *, char *, int),
41374d3e29pfg	    int (* _Nullable)(void *, const char *, int),
41474d3e29pfg	    fpos_t (* _Nullable)(void *, fpos_t, int),
41574d3e29pfg	    int (* _Nullable)(void *));
416a6ce65drgrimes#define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
417a6ce65drgrimes#define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
418c559b89wollman
41931330e2cemtypedef __ssize_t cookie_read_function_t(void *, char *, size_t);
42031330e2cemtypedef __ssize_t cookie_write_function_t(void *, const char *, size_t);
42131330e2cemtypedef int cookie_seek_function_t(void *, off64_t *, int);
42231330e2cemtypedef int cookie_close_function_t(void *);
42397a44a8cemtypedef struct {
42497a44a8cem	cookie_read_function_t	*read;
42597a44a8cem	cookie_write_function_t	*write;
42697a44a8cem	cookie_seek_function_t	*seek;
42797a44a8cem	cookie_close_function_t	*close;
42897a44a8cem} cookie_io_functions_t;
42997a44a8cemFILE	*fopencookie(void *, const char *, cookie_io_functions_t);
43097a44a8cem
431c559b89wollman/*
432c559b89wollman * Portability hacks.  See <sys/types.h>.
433c559b89wollman */
434c559b89wollman#ifndef _FTRUNCATE_DECLARED
435c559b89wollman#define	_FTRUNCATE_DECLARED
4369e6f796mikeint	 ftruncate(int, __off_t);
437c559b89wollman#endif
438c559b89wollman#ifndef _LSEEK_DECLARED
439c559b89wollman#define	_LSEEK_DECLARED
4409e6f796mike__off_t	 lseek(int, __off_t, int);
441c559b89wollman#endif
442c559b89wollman#ifndef _MMAP_DECLARED
443c559b89wollman#define	_MMAP_DECLARED
4449e6f796mikevoid	*mmap(void *, size_t, int, int, int, __off_t);
445c559b89wollman#endif
446c559b89wollman#ifndef _TRUNCATE_DECLARED
447c559b89wollman#define	_TRUNCATE_DECLARED
4489e6f796mikeint	 truncate(const char *, __off_t);
449c559b89wollman#endif
450c559b89wollman#endif /* __BSD_VISIBLE */
451a6ce65drgrimes
452d878a40jhb/*
453d878a40jhb * Functions internal to the implementation.
454d878a40jhb */
455d878a40jhbint	__srget(FILE *);
456d878a40jhbint	__swbuf(int, FILE *);
457d878a40jhb
458d878a40jhb/*
459d878a40jhb * The __sfoo macros are here so that we can
460d878a40jhb * define function versions in the C library.
461d878a40jhb */
462d878a40jhb#define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
463d878a40jhb#if defined(__GNUC__) && defined(__STDC__)
464d878a40jhbstatic __inline int __sputc(int _c, FILE *_p) {
465d878a40jhb	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
466d878a40jhb		return (*_p->_p++ = _c);
467d878a40jhb	else
468d878a40jhb		return (__swbuf(_c, _p));
469d878a40jhb}
470d878a40jhb#else
471d878a40jhb/*
472d878a40jhb * This has been tuned to generate reasonable code on the vax using pcc.
473d878a40jhb */
474d878a40jhb#define	__sputc(c, p) \
475d878a40jhb	(--(p)->_w < 0 ? \
476d878a40jhb		(p)->_w >= (p)->_lbfsize ? \
477d878a40jhb			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
478d878a40jhb				(int)*(p)->_p++ : \
479d878a40jhb				__swbuf('\n', p) : \
480d878a40jhb			__swbuf((int)(c), p) : \
481d878a40jhb		(*(p)->_p = (c), (int)*(p)->_p++))
482d878a40jhb#endif
48331d1910theraven
48499a8da4vangyzen#ifndef __LIBC_ISTHREADED_DECLARED
48599a8da4vangyzen#define __LIBC_ISTHREADED_DECLARED
48631d1910theravenextern int __isthreaded;
48799a8da4vangyzen#endif
48831d1910theraven
489dd1ce6ftheraven#ifndef __cplusplus
490d878a40jhb
491d878a40jhb#define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
492d878a40jhb#define	__sferror(p)	(((p)->_flags & __SERR) != 0)
493d878a40jhb#define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
494d878a40jhb#define	__sfileno(p)	((p)->_file)
495d878a40jhb
496d878a40jhb
497d878a40jhb#define	feof(p)		(!__isthreaded ? __sfeof(p) : (feof)(p))
498d878a40jhb#define	ferror(p)	(!__isthreaded ? __sferror(p) : (ferror)(p))
499d878a40jhb#define	clearerr(p)	(!__isthreaded ? __sclearerr(p) : (clearerr)(p))
500d878a40jhb
501d878a40jhb#if __POSIX_VISIBLE
502d878a40jhb#define	fileno(p)	(!__isthreaded ? __sfileno(p) : (fileno)(p))
503d878a40jhb#endif
504d878a40jhb
505d878a40jhb#define	getc(fp)	(!__isthreaded ? __sgetc(fp) : (getc)(fp))
506d878a40jhb#define	putc(x, fp)	(!__isthreaded ? __sputc(x, fp) : (putc)(x, fp))
507d878a40jhb
508d878a40jhb#define	getchar()	getc(stdin)
509d878a40jhb#define	putchar(x)	putc(x, stdout)
510d878a40jhb
511d878a40jhb#if __BSD_VISIBLE
512d878a40jhb/*
513d878a40jhb * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
514d878a40jhb * B.8.2.7 for the rationale behind the *_unlocked() macros.
515d878a40jhb */
516a508eb3kevans#define	clearerr_unlocked(p)	__sclearerr(p)
517d878a40jhb#define	feof_unlocked(p)	__sfeof(p)
518d878a40jhb#define	ferror_unlocked(p)	__sferror(p)
519d878a40jhb#define	fileno_unlocked(p)	__sfileno(p)
520a508eb3kevans#define	fputc_unlocked(s, p)	__sputc(s, p)
521d878a40jhb#endif
522d878a40jhb#if __POSIX_VISIBLE >= 199506
523d878a40jhb#define	getc_unlocked(fp)	__sgetc(fp)
524d878a40jhb#define	putc_unlocked(x, fp)	__sputc(x, fp)
525d878a40jhb
526d878a40jhb#define	getchar_unlocked()	getc_unlocked(stdin)
527d878a40jhb#define	putchar_unlocked(x)	putc_unlocked(x, stdout)
528d878a40jhb#endif
529dd1ce6ftheraven#endif /* __cplusplus */
530d878a40jhb
531c559b89wollman__END_DECLS
53274d3e29pfg__NULLABILITY_PRAGMA_POP
53374d3e29pfg
5343db3e02bde#endif /* !_STDIO_H_ */
535