1199767fToomas Soome/*-
2199767fToomas Soome * Copyright (c) 1990, 1993
3199767fToomas Soome *	The Regents of the University of California.  All rights reserved.
4199767fToomas Soome *
5199767fToomas Soome * This code is derived from software contributed to Berkeley by
6199767fToomas Soome * Chris Torek.
7199767fToomas Soome *
8199767fToomas Soome * Redistribution and use in source and binary forms, with or without
9199767fToomas Soome * modification, are permitted provided that the following conditions
10199767fToomas Soome * are met:
11199767fToomas Soome * 1. Redistributions of source code must retain the above copyright
12199767fToomas Soome *    notice, this list of conditions and the following disclaimer.
13199767fToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
14199767fToomas Soome *    notice, this list of conditions and the following disclaimer in the
15199767fToomas Soome *    documentation and/or other materials provided with the distribution.
16199767fToomas Soome * 3. Neither the name of the University nor the names of its contributors
17199767fToomas Soome *    may be used to endorse or promote products derived from this software
18199767fToomas Soome *    without specific prior written permission.
19199767fToomas Soome *
20199767fToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21199767fToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22199767fToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23199767fToomas Soome * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24199767fToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25199767fToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26199767fToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27199767fToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28199767fToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29199767fToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30199767fToomas Soome * SUCH DAMAGE.
31199767fToomas Soome *
32199767fToomas Soome *	@(#)stdio.h	8.5 (Berkeley) 4/29/95
33199767fToomas Soome * $FreeBSD$
34199767fToomas Soome */
35199767fToomas Soome
36199767fToomas Soome#ifndef	_STDIO_H_
37199767fToomas Soome#define	_STDIO_H_
38199767fToomas Soome
39199767fToomas Soome#include <sys/cdefs.h>
40199767fToomas Soome#include <sys/_null.h>
41199767fToomas Soome#include <sys/_types.h>
42199767fToomas Soome
43199767fToomas Soometypedef	__off_t		fpos_t;
44199767fToomas Soome
45199767fToomas Soome#ifndef _SIZE_T_DECLARED
46199767fToomas Soometypedef	__size_t	size_t;
47199767fToomas Soome#define	_SIZE_T_DECLARED
48199767fToomas Soome#endif
49199767fToomas Soome
50199767fToomas Soome#if __POSIX_VISIBLE >= 200809
51199767fToomas Soome#ifndef _OFF_T_DECLARED
52199767fToomas Soome#define	_OFF_T_DECLARED
53199767fToomas Soometypedef	__off_t		off_t;
54199767fToomas Soome#endif
55199767fToomas Soome#ifndef _SSIZE_T_DECLARED
56199767fToomas Soome#define	_SSIZE_T_DECLARED
57199767fToomas Soometypedef	__ssize_t	ssize_t;
58199767fToomas Soome#endif
59199767fToomas Soome#endif
60199767fToomas Soome
61199767fToomas Soome#if __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
62199767fToomas Soome#ifndef _VA_LIST_DECLARED
63199767fToomas Soometypedef	__va_list	va_list;
64199767fToomas Soome#define	_VA_LIST_DECLARED
65199767fToomas Soome#endif
66199767fToomas Soome#endif
67199767fToomas Soome
68199767fToomas Soome#define	_FSTDIO			/* Define for new stdio with functions. */
69199767fToomas Soome
70199767fToomas Soome/*
71199767fToomas Soome * NB: to fit things in six character monocase externals, the stdio
72199767fToomas Soome * code uses the prefix `__s' for stdio objects, typically followed
73199767fToomas Soome * by a three-character attempt at a mnemonic.
74199767fToomas Soome */
75199767fToomas Soome
76199767fToomas Soome/* stdio buffers */
77199767fToomas Soomestruct __sbuf {
78199767fToomas Soome	unsigned char *_base;
79199767fToomas Soome	int	_size;
80199767fToomas Soome};
81199767fToomas Soome
82199767fToomas Soome/*
83199767fToomas Soome * stdio state variables.
84199767fToomas Soome *
85199767fToomas Soome * The following always hold:
86199767fToomas Soome *
87199767fToomas Soome *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
88199767fToomas Soome *		_lbfsize is -_bf._size, else _lbfsize is 0
89199767fToomas Soome *	if _flags&__SRD, _w is 0
90199767fToomas Soome *	if _flags&__SWR, _r is 0
91199767fToomas Soome *
92199767fToomas Soome * This ensures that the getc and putc macros (or inline functions) never
93199767fToomas Soome * try to write or read from a file that is in `read' or `write' mode.
94199767fToomas Soome * (Moreover, they can, and do, automatically switch from read mode to
95199767fToomas Soome * write mode, and back, on "r+" and "w+" files.)
96199767fToomas Soome *
97199767fToomas Soome * _lbfsize is used only to make the inline line-buffered output stream
98199767fToomas Soome * code as compact as possible.
99199767fToomas Soome *
100199767fToomas Soome * _ub, _up, and _ur are used when ungetc() pushes back more characters
101199767fToomas Soome * than fit in the current _bf, or when ungetc() pushes back a character
102199767fToomas Soome * that does not match the previous one in _bf.  When this happens,
103199767fToomas Soome * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
104199767fToomas Soome * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
105199767fToomas Soome *
106199767fToomas Soome * Certain members of __sFILE are accessed directly via macros or
107199767fToomas Soome * inline functions.  To preserve ABI compat, these members must not
108199767fToomas Soome * be disturbed.  These members are marked below with (*).
109199767fToomas Soome */
110199767fToomas Soomestruct __sFILE {
111199767fToomas Soome	unsigned char *_p;	/* (*) current position in (some) buffer */
112199767fToomas Soome	int	_r;		/* (*) read space left for getc() */
113199767fToomas Soome	int	_w;		/* (*) write space left for putc() */
114199767fToomas Soome	short	_flags;		/* (*) flags, below; this FILE is free if 0 */
115199767fToomas Soome	short	_file;		/* (*) fileno, if Unix descriptor, else -1 */
116199767fToomas Soome	struct	__sbuf _bf;	/* (*) the buffer (at least 1 byte, if !NULL) */
117199767fToomas Soome	int	_lbfsize;	/* (*) 0 or -_bf._size, for inline putc */
118199767fToomas Soome
119199767fToomas Soome	/* operations */
120199767fToomas Soome	void	*_cookie;	/* (*) cookie passed to io functions */
121199767fToomas Soome	int	(*_close)(void *);
122199767fToomas Soome	int	(*_read)(void *, char *, int);
123199767fToomas Soome	fpos_t	(*_seek)(void *, fpos_t, int);
124199767fToomas Soome	int	(*_write)(void *, const char *, int);
125199767fToomas Soome
126199767fToomas Soome	/* separate buffer for long sequences of ungetc() */
127199767fToomas Soome	struct	__sbuf _ub;	/* ungetc buffer */
128199767fToomas Soome	unsigned char	*_up;	/* saved _p when _p is doing ungetc data */
129199767fToomas Soome	int	_ur;		/* saved _r when _r is counting ungetc data */
130199767fToomas Soome
131199767fToomas Soome	/* tricks to meet minimum requirements even when malloc() fails */
132199767fToomas Soome	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
133199767fToomas Soome	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
134199767fToomas Soome
135199767fToomas Soome	/* separate buffer for fgetln() when line crosses buffer boundary */
136199767fToomas Soome	struct	__sbuf _lb;	/* buffer for fgetln() */
137199767fToomas Soome
138199767fToomas Soome	/* Unix stdio files get aligned to block boundaries on fseek() */
139199767fToomas Soome	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
140199767fToomas Soome	fpos_t	_offset;	/* current lseek offset */
141199767fToomas Soome
142199767fToomas Soome	struct pthread_mutex *_fl_mutex;	/* used for MT-safety */
143199767fToomas Soome	struct pthread *_fl_owner;	/* current owner */
144199767fToomas Soome	int	_fl_count;	/* recursive lock count */
145199767fToomas Soome	int	_orientation;	/* orientation for fwide() */
146199767fToomas Soome	__mbstate_t _mbstate;	/* multibyte conversion state */
147199767fToomas Soome	int	_flags2;	/* additional flags */
148199767fToomas Soome};
149199767fToomas Soome#ifndef _STDFILE_DECLARED
150199767fToomas Soome#define _STDFILE_DECLARED
151199767fToomas Soometypedef struct __sFILE FILE;
152199767fToomas Soome#endif
153199767fToomas Soome#ifndef _STDSTREAM_DECLARED
154199767fToomas Soome__BEGIN_DECLS
155199767fToomas Soomeextern FILE *__stdinp;
156199767fToomas Soomeextern FILE *__stdoutp;
157199767fToomas Soomeextern FILE *__stderrp;
158199767fToomas Soome__END_DECLS
159199767fToomas Soome#define	_STDSTREAM_DECLARED
160199767fToomas Soome#endif
161199767fToomas Soome
162199767fToomas Soome#define	__SLBF	0x0001		/* line buffered */
163199767fToomas Soome#define	__SNBF	0x0002		/* unbuffered */
164199767fToomas Soome#define	__SRD	0x0004		/* OK to read */
165199767fToomas Soome#define	__SWR	0x0008		/* OK to write */
166199767fToomas Soome	/* RD and WR are never simultaneously asserted */
167199767fToomas Soome#define	__SRW	0x0010		/* open for reading & writing */
168199767fToomas Soome#define	__SEOF	0x0020		/* found EOF */
169199767fToomas Soome#define	__SERR	0x0040		/* found error */
170199767fToomas Soome#define	__SMBF	0x0080		/* _bf._base is from malloc */
171199767fToomas Soome#define	__SAPP	0x0100		/* fdopen()ed in append mode */
172199767fToomas Soome#define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
173199767fToomas Soome#define	__SOPT	0x0400		/* do fseek() optimization */
174199767fToomas Soome#define	__SNPT	0x0800		/* do not do fseek() optimization */
175199767fToomas Soome#define	__SOFF	0x1000		/* set iff _offset is in fact correct */
176199767fToomas Soome#define	__SMOD	0x2000		/* true => fgetln modified _p text */
177199767fToomas Soome#define	__SALC	0x4000		/* allocate string space dynamically */
178199767fToomas Soome#define	__SIGN	0x8000		/* ignore this file in _fwalk */
179199767fToomas Soome
180199767fToomas Soome#define	__S2OAP	0x0001		/* O_APPEND mode is set */
181199767fToomas Soome
182199767fToomas Soome/*
183199767fToomas Soome * The following three definitions are for ANSI C, which took them
184199767fToomas Soome * from System V, which brilliantly took internal interface macros and
185199767fToomas Soome * made them official arguments to setvbuf(), without renaming them.
186199767fToomas Soome * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
187199767fToomas Soome *
188199767fToomas Soome * Although numbered as their counterparts above, the implementation
189199767fToomas Soome * does not rely on this.
190199767fToomas Soome */
191199767fToomas Soome#define	_IOFBF	0		/* setvbuf should set fully buffered */
192199767fToomas Soome#define	_IOLBF	1		/* setvbuf should set line buffered */
193199767fToomas Soome#define	_IONBF	2		/* setvbuf should set unbuffered */
194199767fToomas Soome
195199767fToomas Soome#define	BUFSIZ	1024		/* size of buffer used by setbuf */
196199767fToomas Soome#define	EOF	(-1)
197199767fToomas Soome
198199767fToomas Soome/*
199199767fToomas Soome * FOPEN_MAX is a minimum maximum, and is the number of streams that
200199767fToomas Soome * stdio can provide without attempting to allocate further resources
201199767fToomas Soome * (which could fail).  Do not use this for anything.
202199767fToomas Soome */
203199767fToomas Soome				/* must be == _POSIX_STREAM_MAX <limits.h> */
204199767fToomas Soome#ifndef FOPEN_MAX
205199767fToomas Soome#define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
206199767fToomas Soome#endif
207199767fToomas Soome#define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
208199767fToomas Soome
209199767fToomas Soome/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
210199767fToomas Soome#if __XSI_VISIBLE
211199767fToomas Soome#define	P_tmpdir	"/tmp/"
212199767fToomas Soome#endif
213199767fToomas Soome#define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
214199767fToomas Soome#define	TMP_MAX		308915776
215199767fToomas Soome
216199767fToomas Soome#ifndef SEEK_SET
217199767fToomas Soome#define	SEEK_SET	0	/* set file offset to offset */
218199767fToomas Soome#endif
219199767fToomas Soome#ifndef SEEK_CUR
220199767fToomas Soome#define	SEEK_CUR	1	/* set file offset to current plus offset */
221199767fToomas Soome#endif
222199767fToomas Soome#ifndef SEEK_END
223199767fToomas Soome#define	SEEK_END	2	/* set file offset to EOF plus offset */
224199767fToomas Soome#endif
225199767fToomas Soome
226199767fToomas Soome#define	stdin	__stdinp
227199767fToomas Soome#define	stdout	__stdoutp
228199767fToomas Soome#define	stderr	__stderrp
229199767fToomas Soome
230199767fToomas Soome__BEGIN_DECLS
231199767fToomas Soome#ifdef _XLOCALE_H_
232199767fToomas Soome#include <xlocale/_stdio.h>
233199767fToomas Soome#endif
234199767fToomas Soome/*
235199767fToomas Soome * Functions defined in ANSI C standard.
236199767fToomas Soome */
237199767fToomas Soomevoid	 clearerr(FILE *);
238199767fToomas Soomeint	 fclose(FILE *);
239199767fToomas Soomeint	 feof(FILE *);
240199767fToomas Soomeint	 ferror(FILE *);
241199767fToomas Soomeint	 fflush(FILE *);
242199767fToomas Soomeint	 fgetc(FILE *);
243199767fToomas Soomeint	 fgetpos(FILE * __restrict, fpos_t * __restrict);
244199767fToomas Soomechar	*fgets(char * __restrict, int, FILE * __restrict);
245199767fToomas SoomeFILE	*fopen(const char * __restrict, const char * __restrict);
246199767fToomas Soomeint	 fprintf(FILE * __restrict, const char * __restrict, ...);
247199767fToomas Soomeint	 fputc(int, FILE *);
248199767fToomas Soomeint	 fputs(const char * __restrict, FILE * __restrict);
249199767fToomas Soomesize_t	 fread(void * __restrict, size_t, size_t, FILE * __restrict);
250199767fToomas SoomeFILE	*freopen(const char * __restrict, const char * __restrict, FILE * __restrict);
251199767fToomas Soomeint	 fscanf(FILE * __restrict, const char * __restrict, ...);
252199767fToomas Soomeint	 fseek(FILE *, long, int);
253199767fToomas Soomeint	 fsetpos(FILE *, const fpos_t *);
254199767fToomas Soomelong	 ftell(FILE *);
255199767fToomas Soomesize_t	 fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
256199767fToomas Soomeint	 getc(FILE *);
257199767fToomas Soomeint	 getchar(void);
258199767fToomas Soomechar	*gets(char *);
259199767fToomas Soomevoid	 perror(const char *);
260199767fToomas Soomeint	 printf(const char * __restrict, ...);
261199767fToomas Soomeint	 putc(int, FILE *);
262199767fToomas Soomeint	 putchar(int);
263199767fToomas Soomeint	 puts(const char *);
264199767fToomas Soomeint	 remove(const char *);
265199767fToomas Soomeint	 rename(const char *, const char *);
266199767fToomas Soomevoid	 rewind(FILE *);
267199767fToomas Soomeint	 scanf(const char * __restrict, ...);
268199767fToomas Soomevoid	 setbuf(FILE * __restrict, char * __restrict);
269199767fToomas Soomeint	 setvbuf(FILE * __restrict, char * __restrict, int, size_t);
270199767fToomas Soomeint	 sprintf(char * __restrict, const char * __restrict, ...);
271199767fToomas Soomeint	 sscanf(const char * __restrict, const char * __restrict, ...);
272199767fToomas SoomeFILE	*tmpfile(void);
273199767fToomas Soomechar	*tmpnam(char *);
274199767fToomas Soomeint	 ungetc(int, FILE *);
275199767fToomas Soomeint	 vfprintf(FILE * __restrict, const char * __restrict,
276199767fToomas Soome	    __va_list);
277199767fToomas Soomeint	 vprintf(const char * __restrict, __va_list);
278199767fToomas Soomeint	 vsprintf(char * __restrict, const char * __restrict,
279199767fToomas Soome	    __va_list);
280199767fToomas Soome
281199767fToomas Soome#if __ISO_C_VISIBLE >= 1999
282199767fToomas Soomeint	 snprintf(char * __restrict, size_t, const char * __restrict,
283199767fToomas Soome	    ...) __printflike(3, 4);
284199767fToomas Soomeint	 vfscanf(FILE * __restrict, const char * __restrict, __va_list)
285199767fToomas Soome	    __scanflike(2, 0);
286199767fToomas Soomeint	 vscanf(const char * __restrict, __va_list) __scanflike(1, 0);
287199767fToomas Soomeint	 vsnprintf(char * __restrict, size_t, const char * __restrict,
288199767fToomas Soome	    __va_list) __printflike(3, 0);
289199767fToomas Soomeint	 vsscanf(const char * __restrict, const char * __restrict, __va_list)
290199767fToomas Soome	    __scanflike(2, 0);
291199767fToomas Soome#endif
292199767fToomas Soome
293199767fToomas Soome/*
294199767fToomas Soome * Functions defined in all versions of POSIX 1003.1.
295199767fToomas Soome */
296199767fToomas Soome#if __BSD_VISIBLE || (__POSIX_VISIBLE && __POSIX_VISIBLE <= 199506)
297199767fToomas Soome#define	L_cuserid	17	/* size for cuserid(3); MAXLOGNAME, legacy */
298199767fToomas Soome#endif
299199767fToomas Soome
300199767fToomas Soome#if __POSIX_VISIBLE
301199767fToomas Soome#define	L_ctermid	1024	/* size for ctermid(3); PATH_MAX */
302199767fToomas Soome
303199767fToomas Soomechar	*ctermid(char *);
304199767fToomas SoomeFILE	*fdopen(int, const char *);
305199767fToomas Soomeint	 fileno(FILE *);
306199767fToomas Soome#endif /* __POSIX_VISIBLE */
307199767fToomas Soome
308199767fToomas Soome#if __POSIX_VISIBLE >= 199209
309199767fToomas Soomeint	 pclose(FILE *);
310199767fToomas SoomeFILE	*popen(const char *, const char *);
311199767fToomas Soome#endif
312199767fToomas Soome
313199767fToomas Soome#if __POSIX_VISIBLE >= 199506
314199767fToomas Soomeint	 ftrylockfile(FILE *);
315199767fToomas Soomevoid	 flockfile(FILE *);
316199767fToomas Soomevoid	 funlockfile(FILE *);
317199767fToomas Soome
318199767fToomas Soome/*
319199767fToomas Soome * These are normally used through macros as defined below, but POSIX
320199767fToomas Soome * requires functions as well.
321199767fToomas Soome */
322199767fToomas Soomeint	 getc_unlocked(FILE *);
323199767fToomas Soomeint	 getchar_unlocked(void);
324199767fToomas Soomeint	 putc_unlocked(int, FILE *);
325199767fToomas Soomeint	 putchar_unlocked(int);
326199767fToomas Soome#endif
327199767fToomas Soome#if __BSD_VISIBLE
328199767fToomas Soomevoid	 clearerr_unlocked(FILE *);
329199767fToomas Soomeint	 feof_unlocked(FILE *);
330199767fToomas Soomeint	 ferror_unlocked(FILE *);
331199767fToomas Soomeint	 fileno_unlocked(FILE *);
332199767fToomas Soome#endif
333199767fToomas Soome
334199767fToomas Soome#if __POSIX_VISIBLE >= 200112
335199767fToomas Soomeint	 fseeko(FILE *, __off_t, int);
336199767fToomas Soome__off_t	 ftello(FILE *);
337199767fToomas Soome#endif
338199767fToomas Soome
339199767fToomas Soome#if __BSD_VISIBLE || __XSI_VISIBLE > 0 && __XSI_VISIBLE < 600
340199767fToomas Soomeint	 getw(FILE *);
341199767fToomas Soomeint	 putw(int, FILE *);
342199767fToomas Soome#endif /* BSD or X/Open before issue 6 */
343199767fToomas Soome
344199767fToomas Soome#if __XSI_VISIBLE
345199767fToomas Soomechar	*tempnam(const char *, const char *);
346199767fToomas Soome#endif
347199767fToomas Soome
348199767fToomas Soome#if __POSIX_VISIBLE >= 200809
349199767fToomas SoomeFILE	*fmemopen(void * __restrict, size_t, const char * __restrict);
350199767fToomas Soomessize_t	 getdelim(char ** __restrict, size_t * __restrict, int,
351199767fToomas Soome	    FILE * __restrict);
352199767fToomas SoomeFILE	*open_memstream(char **, size_t *);
353199767fToomas Soomeint	 renameat(int, const char *, int, const char *);
354199767fToomas Soomeint	 vdprintf(int, const char * __restrict, __va_list);
355199767fToomas Soome
356199767fToomas Soome/*
357199767fToomas Soome * Every programmer and his dog wrote functions called getline() and dprintf()
358199767fToomas Soome * before POSIX.1-2008 came along and decided to usurp the names, so we
359199767fToomas Soome * don't prototype them by default unless one of the following is true:
360199767fToomas Soome *   a) the app has requested them specifically by defining _WITH_GETLINE or
361199767fToomas Soome *      _WITH_DPRINTF, respectively
362199767fToomas Soome *   b) the app has requested a POSIX.1-2008 environment via _POSIX_C_SOURCE
363199767fToomas Soome *   c) the app defines a GNUism such as _BSD_SOURCE or _GNU_SOURCE
364199767fToomas Soome */
365199767fToomas Soome#ifndef _WITH_GETLINE
366199767fToomas Soome#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
367199767fToomas Soome#define	_WITH_GETLINE
368199767fToomas Soome#elif defined(_POSIX_C_SOURCE)
369199767fToomas Soome#if _POSIX_C_SOURCE >= 200809
370199767fToomas Soome#define	_WITH_GETLINE
371199767fToomas Soome#endif
372199767fToomas Soome#endif
373199767fToomas Soome#endif
374199767fToomas Soome
375199767fToomas Soome#ifdef _WITH_GETLINE
376199767fToomas Soomessize_t	 getline(char ** __restrict, size_t * __restrict, FILE * __restrict);
377199767fToomas Soome#endif
378199767fToomas Soome
379199767fToomas Soome#ifndef _WITH_DPRINTF
380199767fToomas Soome#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
381199767fToomas Soome#define	_WITH_DPRINTF
382199767fToomas Soome#elif defined(_POSIX_C_SOURCE)
383199767fToomas Soome#if _POSIX_C_SOURCE >= 200809
384199767fToomas Soome#define	_WITH_DPRINTF
385199767fToomas Soome#endif
386199767fToomas Soome#endif
387199767fToomas Soome#endif
388199767fToomas Soome
389199767fToomas Soome#ifdef _WITH_DPRINTF
390199767fToomas Soomeint	 (dprintf)(int, const char * __restrict, ...);
391199767fToomas Soome#endif
392199767fToomas Soome
393199767fToomas Soome#endif /* __POSIX_VISIBLE >= 200809 */
394199767fToomas Soome
395199767fToomas Soome/*
396199767fToomas Soome * Routines that are purely local.
397199767fToomas Soome */
398199767fToomas Soome#if __BSD_VISIBLE
399199767fToomas Soomeint	 asprintf(char **, const char *, ...) __printflike(2, 3);
400199767fToomas Soomechar	*ctermid_r(char *);
401199767fToomas Soomevoid	 fcloseall(void);
402199767fToomas Soomeint	 fdclose(FILE *, int *);
403199767fToomas Soomechar	*fgetln(FILE *, size_t *);
404199767fToomas Soomeconst char *fmtcheck(const char *, const char *) __format_arg(2);
405199767fToomas Soomeint	 fpurge(FILE *);
406199767fToomas Soomevoid	 setbuffer(FILE *, char *, int);
407199767fToomas Soomeint	 setlinebuf(FILE *);
408199767fToomas Soomeint	 vasprintf(char **, const char *, __va_list)
409199767fToomas Soome	    __printflike(2, 0);
410199767fToomas Soome
411199767fToomas Soome/*
412199767fToomas Soome * The system error table contains messages for the first sys_nerr
413199767fToomas Soome * positive errno values.  Use strerror() or strerror_r() from <string.h>
414199767fToomas Soome * instead.
415199767fToomas Soome */
416199767fToomas Soomeextern const int sys_nerr;
417199767fToomas Soomeextern const char * const sys_errlist[];
418199767fToomas Soome
419199767fToomas Soome/*
420199767fToomas Soome * Stdio function-access interface.
421199767fToomas Soome */
422199767fToomas SoomeFILE	*funopen(const void *,
423199767fToomas Soome	    int (*)(void *, char *, int),
424199767fToomas Soome	    int (*)(void *, const char *, int),
425199767fToomas Soome	    fpos_t (*)(void *, fpos_t, int),
426199767fToomas Soome	    int (*)(void *));
427199767fToomas Soome#define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
428199767fToomas Soome#define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
429199767fToomas Soome
430199767fToomas Soome/*
431199767fToomas Soome * Portability hacks.  See <sys/types.h>.
432199767fToomas Soome */
433199767fToomas Soome#ifndef _FTRUNCATE_DECLARED
434199767fToomas Soome#define	_FTRUNCATE_DECLARED
435199767fToomas Soomeint	 ftruncate(int, __off_t);
436199767fToomas Soome#endif
437199767fToomas Soome#ifndef _LSEEK_DECLARED
438199767fToomas Soome#define	_LSEEK_DECLARED
439199767fToomas Soome__off_t	 lseek(int, __off_t, int);
440199767fToomas Soome#endif
441199767fToomas Soome#ifndef _MMAP_DECLARED
442199767fToomas Soome#define	_MMAP_DECLARED
443199767fToomas Soomevoid	*mmap(void *, size_t, int, int, int, __off_t);
444199767fToomas Soome#endif
445199767fToomas Soome#ifndef _TRUNCATE_DECLARED
446199767fToomas Soome#define	_TRUNCATE_DECLARED
447199767fToomas Soomeint	 truncate(const char *, __off_t);
448199767fToomas Soome#endif
449199767fToomas Soome#endif /* __BSD_VISIBLE */
450199767fToomas Soome
451199767fToomas Soome/*
452199767fToomas Soome * Functions internal to the implementation.
453199767fToomas Soome */
454199767fToomas Soomeint	__srget(FILE *);
455199767fToomas Soomeint	__swbuf(int, FILE *);
456199767fToomas Soome
457199767fToomas Soome/*
458199767fToomas Soome * The __sfoo macros are here so that we can
459199767fToomas Soome * define function versions in the C library.
460199767fToomas Soome */
461199767fToomas Soome#define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
462199767fToomas Soome#if defined(__GNUC__) && defined(__STDC__)
463199767fToomas Soomestatic __inline int __sputc(int _c, FILE *_p) {
464199767fToomas Soome	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
465199767fToomas Soome		return (*_p->_p++ = _c);
466199767fToomas Soome	else
467199767fToomas Soome		return (__swbuf(_c, _p));
468199767fToomas Soome}
469199767fToomas Soome#else
470199767fToomas Soome/*
471199767fToomas Soome * This has been tuned to generate reasonable code on the vax using pcc.
472199767fToomas Soome */
473199767fToomas Soome#define	__sputc(c, p) \
474199767fToomas Soome	(--(p)->_w < 0 ? \
475199767fToomas Soome		(p)->_w >= (p)->_lbfsize ? \
476199767fToomas Soome			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
477199767fToomas Soome				(int)*(p)->_p++ : \
478199767fToomas Soome				__swbuf('\n', p) : \
479199767fToomas Soome			__swbuf((int)(c), p) : \
480199767fToomas Soome		(*(p)->_p = (c), (int)*(p)->_p++))
481199767fToomas Soome#endif
482199767fToomas Soome
483199767fToomas Soomeextern int __isthreaded;
484199767fToomas Soome
485199767fToomas Soome#ifndef __cplusplus
486199767fToomas Soome
487199767fToomas Soome#define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
488199767fToomas Soome#define	__sferror(p)	(((p)->_flags & __SERR) != 0)
489199767fToomas Soome#define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
490199767fToomas Soome#define	__sfileno(p)	((p)->_file)
491199767fToomas Soome
492199767fToomas Soome
493199767fToomas Soome#define	feof(p)		(!__isthreaded ? __sfeof(p) : (feof)(p))
494199767fToomas Soome#define	ferror(p)	(!__isthreaded ? __sferror(p) : (ferror)(p))
495199767fToomas Soome#define	clearerr(p)	(!__isthreaded ? __sclearerr(p) : (clearerr)(p))
496199767fToomas Soome
497199767fToomas Soome#if __POSIX_VISIBLE
498199767fToomas Soome#define	fileno(p)	(!__isthreaded ? __sfileno(p) : (fileno)(p))
499199767fToomas Soome#endif
500199767fToomas Soome
501199767fToomas Soome#define	getc(fp)	(!__isthreaded ? __sgetc(fp) : (getc)(fp))
502199767fToomas Soome#define	putc(x, fp)	(!__isthreaded ? __sputc(x, fp) : (putc)(x, fp))
503199767fToomas Soome
504199767fToomas Soome#define	getchar()	getc(stdin)
505199767fToomas Soome#define	putchar(x)	putc(x, stdout)
506199767fToomas Soome
507199767fToomas Soome#if __BSD_VISIBLE
508199767fToomas Soome/*
509199767fToomas Soome * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
510199767fToomas Soome * B.8.2.7 for the rationale behind the *_unlocked() macros.
511199767fToomas Soome */
512199767fToomas Soome#define	feof_unlocked(p)	__sfeof(p)
513199767fToomas Soome#define	ferror_unlocked(p)	__sferror(p)
514199767fToomas Soome#define	clearerr_unlocked(p)	__sclearerr(p)
515199767fToomas Soome#define	fileno_unlocked(p)	__sfileno(p)
516199767fToomas Soome#endif
517199767fToomas Soome#if __POSIX_VISIBLE >= 199506
518199767fToomas Soome#define	getc_unlocked(fp)	__sgetc(fp)
519199767fToomas Soome#define	putc_unlocked(x, fp)	__sputc(x, fp)
520199767fToomas Soome
521199767fToomas Soome#define	getchar_unlocked()	getc_unlocked(stdin)
522199767fToomas Soome#define	putchar_unlocked(x)	putc_unlocked(x, stdout)
523199767fToomas Soome#endif
524199767fToomas Soome#endif /* __cplusplus */
525199767fToomas Soome
526199767fToomas Soome__END_DECLS
527199767fToomas Soome#endif /* !_STDIO_H_ */
528