stdio.h revision 228468
152419Sjulian/*-
252419Sjulian * Copyright (c) 1990, 1993
352419Sjulian *	The Regents of the University of California.  All rights reserved.
452419Sjulian *
552419Sjulian * This code is derived from software contributed to Berkeley by
670700Sjulian * Chris Torek.
752419Sjulian *
852419Sjulian * Redistribution and use in source and binary forms, with or without
952419Sjulian * modification, are permitted provided that the following conditions
1052419Sjulian * are met:
1152419Sjulian * 1. Redistributions of source code must retain the above copyright
1252419Sjulian *    notice, this list of conditions and the following disclaimer.
1352419Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1452419Sjulian *    notice, this list of conditions and the following disclaimer in the
1552419Sjulian *    documentation and/or other materials provided with the distribution.
1652419Sjulian * 3. Neither the name of the University nor the names of its contributors
1770700Sjulian *    may be used to endorse or promote products derived from this software
1852419Sjulian *    without specific prior written permission.
1952419Sjulian *
2052419Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2152419Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2252419Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2352419Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2452419Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2552419Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2652419Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2752419Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2852419Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2952419Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3052419Sjulian * SUCH DAMAGE.
3152419Sjulian *
3252419Sjulian *	@(#)stdio.h	8.5 (Berkeley) 4/29/95
3352419Sjulian * $FreeBSD: head/include/stdio.h 228468 2011-12-13 13:32:56Z ed $
3452419Sjulian */
3552419Sjulian
3667506Sjulian#ifndef	_STDIO_H_
3767506Sjulian#define	_STDIO_H_
3852419Sjulian
3952419Sjulian#include <sys/cdefs.h>
4052419Sjulian#include <sys/_null.h>
4152419Sjulian#include <sys/_types.h>
4252419Sjulian
4352419Sjuliantypedef	__off_t		fpos_t;
4452419Sjulian
4552419Sjulian#ifndef _SIZE_T_DECLARED
4652419Sjuliantypedef	__size_t	size_t;
4752419Sjulian#define	_SIZE_T_DECLARED
4852419Sjulian#endif
4952419Sjulian
5052419Sjulian#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809
5152419Sjulian#ifndef _OFF_T_DECLARED
5252419Sjulian#define	_OFF_T_DECLARED
5370700Sjuliantypedef	__off_t		off_t;
5452419Sjulian#endif
5552419Sjulian#ifndef _SSIZE_T_DECLARED
5652419Sjulian#define	_SSIZE_T_DECLARED
5752843Sphktypedef	__ssize_t	ssize_t;
5852816Sarchie#endif
5952419Sjulian#endif
6052419Sjulian
6152419Sjulian#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
6252419Sjulian#ifndef _VA_LIST_DECLARED
6352419Sjuliantypedef	__va_list	va_list;
6453913Sarchie#define	_VA_LIST_DECLARED
6552419Sjulian#endif
6672053Sjulian#endif
6759756Speter
6870784Sjulian#define	_FSTDIO			/* Define for new stdio with functions. */
6970700Sjulian
7070700Sjulian/*
7152419Sjulian * NB: to fit things in six character monocase externals, the stdio
7270784Sjulian * code uses the prefix `__s' for stdio objects, typically followed
7370784Sjulian * by a three-character attempt at a mnemonic.
7470784Sjulian */
7570784Sjulian
7670784Sjulian/* stdio buffers */
7770784Sjulianstruct __sbuf {
7870784Sjulian	unsigned char *_base;
7970784Sjulian	int	_size;
8070784Sjulian};
8170784Sjulian
8270784Sjulian/*
8370784Sjulian * stdio state variables.
8470935Sjulian *
8570935Sjulian * The following always hold:
8670935Sjulian *
8770935Sjulian *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
8870935Sjulian *		_lbfsize is -_bf._size, else _lbfsize is 0
8970935Sjulian *	if _flags&__SRD, _w is 0
9070935Sjulian *	if _flags&__SWR, _r is 0
9170935Sjulian *
9270935Sjulian * This ensures that the getc and putc macros (or inline functions) never
9370935Sjulian * try to write or read from a file that is in `read' or `write' mode.
9470935Sjulian * (Moreover, they can, and do, automatically switch from read mode to
9570935Sjulian * write mode, and back, on "r+" and "w+" files.)
9670935Sjulian *
9770935Sjulian * _lbfsize is used only to make the inline line-buffered output stream
9870935Sjulian * code as compact as possible.
9970935Sjulian *
10070935Sjulian * _ub, _up, and _ur are used when ungetc() pushes back more characters
10170935Sjulian * than fit in the current _bf, or when ungetc() pushes back a character
10270935Sjulian * that does not match the previous one in _bf.  When this happens,
10370935Sjulian * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
10470784Sjulian * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
10570935Sjulian *
10670935Sjulian * Certain members of __sFILE are accessed directly via macros or
10770935Sjulian * inline functions.  To preserve ABI compat, these members must not
10870935Sjulian * be disturbed.  These members are marked below with (*).
10970935Sjulian */
11070935Sjulianstruct __sFILE {
11170935Sjulian	unsigned char *_p;	/* (*) current position in (some) buffer */
11270935Sjulian	int	_r;		/* (*) read space left for getc() */
11370935Sjulian	int	_w;		/* (*) write space left for putc() */
11470935Sjulian	short	_flags;		/* (*) flags, below; this FILE is free if 0 */
11570935Sjulian	short	_file;		/* (*) fileno, if Unix descriptor, else -1 */
11670935Sjulian	struct	__sbuf _bf;	/* (*) the buffer (at least 1 byte, if !NULL) */
11770935Sjulian	int	_lbfsize;	/* (*) 0 or -_bf._size, for inline putc */
11870935Sjulian
11970935Sjulian	/* operations */
12070935Sjulian	void	*_cookie;	/* (*) cookie passed to io functions */
12170935Sjulian	int	(*_close)(void *);
12270935Sjulian	int	(*_read)(void *, char *, int);
12370935Sjulian	fpos_t	(*_seek)(void *, fpos_t, int);
12470935Sjulian	int	(*_write)(void *, const char *, int);
12570935Sjulian
12670935Sjulian	/* separate buffer for long sequences of ungetc() */
12770935Sjulian	struct	__sbuf _ub;	/* ungetc buffer */
12870935Sjulian	unsigned char	*_up;	/* saved _p when _p is doing ungetc data */
12970935Sjulian	int	_ur;		/* saved _r when _r is counting ungetc data */
13070935Sjulian
13170935Sjulian	/* tricks to meet minimum requirements even when malloc() fails */
13270935Sjulian	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
13370935Sjulian	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
13470935Sjulian
13570935Sjulian	/* separate buffer for fgetln() when line crosses buffer boundary */
13670935Sjulian	struct	__sbuf _lb;	/* buffer for fgetln() */
13770935Sjulian
13870935Sjulian	/* Unix stdio files get aligned to block boundaries on fseek() */
13971885Sjulian	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
14071885Sjulian	fpos_t	_offset;	/* current lseek offset */
14170935Sjulian
14270935Sjulian	struct pthread_mutex *_fl_mutex;	/* used for MT-safety */
14370935Sjulian	struct pthread *_fl_owner;	/* current owner */
14470935Sjulian	int	_fl_count;	/* recursive lock count */
14570935Sjulian	int	_orientation;	/* orientation for fwide() */
14670935Sjulian	__mbstate_t _mbstate;	/* multibyte conversion state */
14770935Sjulian};
14870935Sjulian#ifndef _STDFILE_DECLARED
14970935Sjulian#define _STDFILE_DECLARED
15070935Sjuliantypedef struct __sFILE FILE;
15170935Sjulian#endif
15270700Sjulian#ifndef _STDSTREAM_DECLARED
15370700Sjulian__BEGIN_DECLS
15471902Sjulianextern FILE *__stdinp;
15570700Sjulianextern FILE *__stdoutp;
15652419Sjulianextern FILE *__stderrp;
15770700Sjulian__END_DECLS
15870700Sjulian#define	_STDSTREAM_DECLARED
15952419Sjulian#endif
16070700Sjulian
16171354Sjulian#define	__SLBF	0x0001		/* line buffered */
16271354Sjulian#define	__SNBF	0x0002		/* unbuffered */
16371354Sjulian#define	__SRD	0x0004		/* OK to read */
16470700Sjulian#define	__SWR	0x0008		/* OK to write */
16571354Sjulian	/* RD and WR are never simultaneously asserted */
16671354Sjulian#define	__SRW	0x0010		/* open for reading & writing */
16771354Sjulian#define	__SEOF	0x0020		/* found EOF */
16871354Sjulian#define	__SERR	0x0040		/* found error */
16971354Sjulian#define	__SMBF	0x0080		/* _buf is from malloc */
17071354Sjulian#define	__SAPP	0x0100		/* fdopen()ed in append mode */
17171354Sjulian#define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
17271354Sjulian#define	__SOPT	0x0400		/* do fseek() optimization */
17371354Sjulian#define	__SNPT	0x0800		/* do not do fseek() optimization */
17471354Sjulian#define	__SOFF	0x1000		/* set iff _offset is in fact correct */
17571354Sjulian#define	__SMOD	0x2000		/* true => fgetln modified _p text */
17671354Sjulian#define	__SALC	0x4000		/* allocate string space dynamically */
17752722Sjulian#define	__SIGN	0x8000		/* ignore this file in _fwalk */
17870700Sjulian
17970700Sjulian/*
18070700Sjulian * The following three definitions are for ANSI C, which took them
18170700Sjulian * from System V, which brilliantly took internal interface macros and
18252419Sjulian * made them official arguments to setvbuf(), without renaming them.
18352419Sjulian * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
18470700Sjulian *
18552722Sjulian * Although numbered as their counterparts above, the implementation
18652419Sjulian * does not rely on this.
18770700Sjulian */
18852419Sjulian#define	_IOFBF	0		/* setvbuf should set fully buffered */
18971849Sjulian#define	_IOLBF	1		/* setvbuf should set line buffered */
19070700Sjulian#define	_IONBF	2		/* setvbuf should set unbuffered */
19170700Sjulian
19270700Sjulian#define	BUFSIZ	1024		/* size of buffer used by setbuf */
19371047Sjulian#define	EOF	(-1)
19471047Sjulian
19571849Sjulian/*
19671849Sjulian * FOPEN_MAX is a minimum maximum, and is the number of streams that
19771047Sjulian * stdio can provide without attempting to allocate further resources
19871047Sjulian * (which could fail).  Do not use this for anything.
19952419Sjulian */
20070784Sjulian				/* must be == _POSIX_STREAM_MAX <limits.h> */
20170700Sjulian#ifndef FOPEN_MAX
20270700Sjulian#define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
20370700Sjulian#endif
20470700Sjulian#define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
20570700Sjulian
20670700Sjulian/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
20770700Sjulian#if __XSI_VISIBLE
20870700Sjulian#define	P_tmpdir	"/var/tmp/"
20971849Sjulian#endif
21070784Sjulian#define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
21170700Sjulian#define	TMP_MAX		308915776
21270700Sjulian
21352419Sjulian#ifndef SEEK_SET
21452419Sjulian#define	SEEK_SET	0	/* set file offset to offset */
21570700Sjulian#endif
21670700Sjulian#ifndef SEEK_CUR
21770700Sjulian#define	SEEK_CUR	1	/* set file offset to current plus offset */
21870700Sjulian#endif
21970700Sjulian#ifndef SEEK_END
22052419Sjulian#define	SEEK_END	2	/* set file offset to EOF plus offset */
22170700Sjulian#endif
22270784Sjulian
22370784Sjulian#define	stdin	__stdinp
22470784Sjulian#define	stdout	__stdoutp
22570784Sjulian#define	stderr	__stderrp
22670784Sjulian
22770784Sjulian__BEGIN_DECLS
22870784Sjulian/*
22970784Sjulian * Functions defined in ANSI C standard.
23070784Sjulian */
23170784Sjulianvoid	 clearerr(FILE *);
23270784Sjulianint	 fclose(FILE *);
23370784Sjulianint	 feof(FILE *);
23470784Sjulianint	 ferror(FILE *);
23570784Sjulianint	 fflush(FILE *);
23670784Sjulianint	 fgetc(FILE *);
23770784Sjulianint	 fgetpos(FILE * __restrict, fpos_t * __restrict);
23870784Sjulianchar	*fgets(char * __restrict, int, FILE * __restrict);
23970784SjulianFILE	*fopen(const char * __restrict, const char * __restrict);
24070784Sjulianint	 fprintf(FILE * __restrict, const char * __restrict, ...);
24170784Sjulianint	 fputc(int, FILE *);
24270784Sjulianint	 fputs(const char * __restrict, FILE * __restrict);
24370784Sjuliansize_t	 fread(void * __restrict, size_t, size_t, FILE * __restrict);
24470784SjulianFILE	*freopen(const char * __restrict, const char * __restrict, FILE * __restrict);
24570784Sjulianint	 fscanf(FILE * __restrict, const char * __restrict, ...);
24670784Sjulianint	 fseek(FILE *, long, int);
24770784Sjulianint	 fsetpos(FILE *, const fpos_t *);
24870784Sjulianlong	 ftell(FILE *);
24970784Sjuliansize_t	 fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
25070784Sjulianint	 getc(FILE *);
25170784Sjulianint	 getchar(void);
25270784Sjulianchar	*gets(char *);
25370784Sjulianvoid	 perror(const char *);
25470784Sjulianint	 printf(const char * __restrict, ...);
25570784Sjulianint	 putc(int, FILE *);
25670784Sjulianint	 putchar(int);
25770784Sjulianint	 puts(const char *);
25870784Sjulianint	 remove(const char *);
25970784Sjulianint	 rename(const char *, const char *);
26070784Sjulianvoid	 rewind(FILE *);
26170784Sjulianint	 scanf(const char * __restrict, ...);
26270784Sjulianvoid	 setbuf(FILE * __restrict, char * __restrict);
26370784Sjulianint	 setvbuf(FILE * __restrict, char * __restrict, int, size_t);
26470784Sjulianint	 sprintf(char * __restrict, const char * __restrict, ...);
26570784Sjulianint	 sscanf(const char * __restrict, const char * __restrict, ...);
26670784SjulianFILE	*tmpfile(void);
26770784Sjulianchar	*tmpnam(char *);
26870784Sjulianint	 ungetc(int, FILE *);
26970784Sjulianint	 vfprintf(FILE * __restrict, const char * __restrict,
27070784Sjulian	    __va_list);
27170784Sjulianint	 vprintf(const char * __restrict, __va_list);
27270784Sjulianint	 vsprintf(char * __restrict, const char * __restrict,
27370784Sjulian	    __va_list);
27470784Sjulian
27570784Sjulian#if __ISO_C_VISIBLE >= 1999
27670784Sjulianint	 snprintf(char * __restrict, size_t, const char * __restrict,
27770784Sjulian	    ...) __printflike(3, 4);
27870784Sjulianint	 vfscanf(FILE * __restrict, const char * __restrict, __va_list)
27970784Sjulian	    __scanflike(2, 0);
28070784Sjulianint	 vscanf(const char * __restrict, __va_list) __scanflike(1, 0);
28170784Sjulianint	 vsnprintf(char * __restrict, size_t, const char * __restrict,
28270784Sjulian	    __va_list) __printflike(3, 0);
28370784Sjulianint	 vsscanf(const char * __restrict, const char * __restrict, __va_list)
28470784Sjulian	    __scanflike(2, 0);
28570784Sjulian#endif
28670784Sjulian
28770784Sjulian/*
28870784Sjulian * Functions defined in all versions of POSIX 1003.1.
28970784Sjulian */
29070784Sjulian#if __BSD_VISIBLE || __POSIX_VISIBLE <= 199506
29170784Sjulian#define	L_cuserid	17	/* size for cuserid(3); MAXLOGNAME, legacy */
29270784Sjulian#endif
29370784Sjulian
29470784Sjulian#if __POSIX_VISIBLE
29570784Sjulian#define	L_ctermid	1024	/* size for ctermid(3); PATH_MAX */
29670784Sjulian
29770784Sjulianchar	*ctermid(char *);
29870784SjulianFILE	*fdopen(int, const char *);
29970784Sjulianint	 fileno(FILE *);
30070784Sjulian#endif /* __POSIX_VISIBLE */
30170784Sjulian
30270784Sjulian#if __POSIX_VISIBLE >= 199209
30370784Sjulianint	 pclose(FILE *);
30470784SjulianFILE	*popen(const char *, const char *);
30570784Sjulian#endif
30670784Sjulian
30770784Sjulian#if __POSIX_VISIBLE >= 199506
30870784Sjulianint	 ftrylockfile(FILE *);
30970784Sjulianvoid	 flockfile(FILE *);
31070784Sjulianvoid	 funlockfile(FILE *);
31170784Sjulian
31270784Sjulian/*
31370784Sjulian * These are normally used through macros as defined below, but POSIX
31470784Sjulian * requires functions as well.
31570784Sjulian */
31670700Sjulianint	 getc_unlocked(FILE *);
31770700Sjulianint	 getchar_unlocked(void);
31870784Sjulianint	 putc_unlocked(int, FILE *);
31970784Sjulianint	 putchar_unlocked(int);
32070784Sjulian#endif
32170700Sjulian#if __BSD_VISIBLE
32270700Sjulianvoid	 clearerr_unlocked(FILE *);
32370700Sjulianint	 feof_unlocked(FILE *);
32470700Sjulianint	 ferror_unlocked(FILE *);
32552419Sjulianint	 fileno_unlocked(FILE *);
32652419Sjulian#endif
32771047Sjulian
32852419Sjulian#if __POSIX_VISIBLE >= 200112
32952419Sjulianint	 fseeko(FILE *, __off_t, int);
33052722Sjulian__off_t	 ftello(FILE *);
33152722Sjulian#endif
33253403Sarchie
33353403Sarchie#if __BSD_VISIBLE || __XSI_VISIBLE > 0 && __XSI_VISIBLE < 600
33453403Sarchieint	 getw(FILE *);
33553403Sarchieint	 putw(int, FILE *);
33653403Sarchie#endif /* BSD or X/Open before issue 6 */
33753403Sarchie
33853403Sarchie#if __XSI_VISIBLE
33953403Sarchiechar	*tempnam(const char *, const char *);
34053403Sarchie#endif
34153403Sarchie
34253403Sarchie#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809
34353403Sarchiessize_t	 getdelim(char ** __restrict, size_t * __restrict, int,
34453403Sarchie	    FILE * __restrict);
34553403Sarchieint	 renameat(int, const char *, int, const char *);
34653403Sarchieint	 vdprintf(int, const char * __restrict, __va_list);
34753403Sarchie
34853403Sarchie/*
34952722Sjulian * Every programmer and his dog wrote functions called getline() and dprintf()
35053403Sarchie * before POSIX.1-2008 came along and decided to usurp the names, so we
35152419Sjulian * don't prototype them by default unless one of the following is true:
35253913Sarchie *   a) the app has requested them specifically by defining _WITH_GETLINE or
35353913Sarchie *      _WITH_DPRINTF, respectively
35453913Sarchie *   b) the app has requested a POSIX.1-2008 environment via _POSIX_C_SOURCE
35553913Sarchie *   c) the app defines a GNUism such as _BSD_SOURCE or _GNU_SOURCE
35653913Sarchie */
35753913Sarchie#ifndef _WITH_GETLINE
35853913Sarchie#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
35953913Sarchie#define	_WITH_GETLINE
36053913Sarchie#elif defined(_POSIX_C_SOURCE)
36153913Sarchie#if _POSIX_C_SOURCE >= 200809
36253913Sarchie#define	_WITH_GETLINE
36353913Sarchie#endif
36453913Sarchie#endif
36553913Sarchie#endif
36653913Sarchie
36753913Sarchie#ifdef _WITH_GETLINE
36853913Sarchiessize_t	 getline(char ** __restrict, size_t * __restrict, FILE * __restrict);
36953913Sarchie#endif
37053913Sarchie
37153913Sarchie#ifndef _WITH_DPRINTF
37253913Sarchie#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
37353913Sarchie#define	_WITH_DPRINTF
37453913Sarchie#elif defined(_POSIX_C_SOURCE)
37553913Sarchie#if _POSIX_C_SOURCE >= 200809
37653913Sarchie#define	_WITH_DPRINTF
37753913Sarchie#endif
37853913Sarchie#endif
37953913Sarchie#endif
38053913Sarchie
38153913Sarchie#ifdef _WITH_DPRINTF
38253913Sarchieint	 (dprintf)(int, const char * __restrict, ...);
38353913Sarchie#endif
38453913Sarchie
38553913Sarchie#endif /* __BSD_VISIBLE || __POSIX_VISIBLE >= 200809 */
38653913Sarchie
38753913Sarchie/*
38853913Sarchie * Routines that are purely local.
38953913Sarchie */
39053913Sarchie#if __BSD_VISIBLE
39153913Sarchieint	 asprintf(char **, const char *, ...) __printflike(2, 3);
39253913Sarchiechar	*ctermid_r(char *);
39353913Sarchievoid	 fcloseall(void);
39453913Sarchiechar	*fgetln(FILE *, size_t *);
39553913Sarchieconst char *fmtcheck(const char *, const char *) __format_arg(2);
39653913Sarchieint	 fpurge(FILE *);
39753913Sarchievoid	 setbuffer(FILE *, char *, int);
39853913Sarchieint	 setlinebuf(FILE *);
39953913Sarchieint	 vasprintf(char **, const char *, __va_list)
40053913Sarchie	    __printflike(2, 0);
40153913Sarchie
40253913Sarchie/*
40353913Sarchie * The system error table contains messages for the first sys_nerr
40453913Sarchie * positive errno values.  Use strerror() or strerror_r() from <string.h>
40553913Sarchie * instead.
40653913Sarchie */
40753913Sarchieextern const int sys_nerr;
40853913Sarchieextern const char * const sys_errlist[];
40953913Sarchie
41053913Sarchie/*
41153913Sarchie * Stdio function-access interface.
41253913Sarchie */
41353913SarchieFILE	*funopen(const void *,
41453913Sarchie	    int (*)(void *, char *, int),
41553913Sarchie	    int (*)(void *, const char *, int),
41653913Sarchie	    fpos_t (*)(void *, fpos_t, int),
41753913Sarchie	    int (*)(void *));
41853913Sarchie#define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
41953913Sarchie#define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
42053913Sarchie
42153913Sarchie/*
42253913Sarchie * Portability hacks.  See <sys/types.h>.
42353913Sarchie */
42453913Sarchie#ifndef _FTRUNCATE_DECLARED
42553913Sarchie#define	_FTRUNCATE_DECLARED
42653913Sarchieint	 ftruncate(int, __off_t);
42753913Sarchie#endif
42853913Sarchie#ifndef _LSEEK_DECLARED
42953913Sarchie#define	_LSEEK_DECLARED
43053913Sarchie__off_t	 lseek(int, __off_t, int);
43153913Sarchie#endif
43253913Sarchie#ifndef _MMAP_DECLARED
43353913Sarchie#define	_MMAP_DECLARED
43453913Sarchievoid	*mmap(void *, size_t, int, int, int, __off_t);
43553913Sarchie#endif
43653913Sarchie#ifndef _TRUNCATE_DECLARED
43753913Sarchie#define	_TRUNCATE_DECLARED
43853913Sarchieint	 truncate(const char *, __off_t);
43953913Sarchie#endif
44053913Sarchie#endif /* __BSD_VISIBLE */
44153913Sarchie
44253913Sarchie/*
44353913Sarchie * Functions internal to the implementation.
44453913Sarchie */
44553913Sarchieint	__srget(FILE *);
44653913Sarchieint	__swbuf(int, FILE *);
44753913Sarchie
44853913Sarchie/*
44953913Sarchie * The __sfoo macros are here so that we can
45053913Sarchie * define function versions in the C library.
45153913Sarchie */
45253913Sarchie#define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
45353913Sarchie#if defined(__GNUC__) && defined(__STDC__)
45453913Sarchiestatic __inline int __sputc(int _c, FILE *_p) {
45553913Sarchie	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
45653913Sarchie		return (*_p->_p++ = _c);
45753913Sarchie	else
45853913Sarchie		return (__swbuf(_c, _p));
45953913Sarchie}
46053913Sarchie#else
46153913Sarchie/*
46253913Sarchie * This has been tuned to generate reasonable code on the vax using pcc.
46353913Sarchie */
46453913Sarchie#define	__sputc(c, p) \
46553913Sarchie	(--(p)->_w < 0 ? \
46653913Sarchie		(p)->_w >= (p)->_lbfsize ? \
46753913Sarchie			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
46853913Sarchie				(int)*(p)->_p++ : \
46953913Sarchie				__swbuf('\n', p) : \
47053913Sarchie			__swbuf((int)(c), p) : \
47153913Sarchie		(*(p)->_p = (c), (int)*(p)->_p++))
47253913Sarchie#endif
47353913Sarchie#ifndef __cplusplus
47453913Sarchie
47553913Sarchie#define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
47653913Sarchie#define	__sferror(p)	(((p)->_flags & __SERR) != 0)
47753913Sarchie#define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
47853913Sarchie#define	__sfileno(p)	((p)->_file)
47953913Sarchie
48053913Sarchieextern int __isthreaded;
48153913Sarchie
48253913Sarchie#define	feof(p)		(!__isthreaded ? __sfeof(p) : (feof)(p))
48353913Sarchie#define	ferror(p)	(!__isthreaded ? __sferror(p) : (ferror)(p))
48453913Sarchie#define	clearerr(p)	(!__isthreaded ? __sclearerr(p) : (clearerr)(p))
48553913Sarchie
48653913Sarchie#if __POSIX_VISIBLE
48753913Sarchie#define	fileno(p)	(!__isthreaded ? __sfileno(p) : (fileno)(p))
48853913Sarchie#endif
48953913Sarchie
49053913Sarchie#define	getc(fp)	(!__isthreaded ? __sgetc(fp) : (getc)(fp))
49153913Sarchie#define	putc(x, fp)	(!__isthreaded ? __sputc(x, fp) : (putc)(x, fp))
49253913Sarchie
49353913Sarchie#define	getchar()	getc(stdin)
49453913Sarchie#define	putchar(x)	putc(x, stdout)
49553913Sarchie
49653913Sarchie#if __BSD_VISIBLE
49753913Sarchie/*
49853913Sarchie * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
49953913Sarchie * B.8.2.7 for the rationale behind the *_unlocked() macros.
50053913Sarchie */
50153913Sarchie#define	feof_unlocked(p)	__sfeof(p)
50262471Sphk#define	ferror_unlocked(p)	__sferror(p)
50362471Sphk#define	clearerr_unlocked(p)	__sclearerr(p)
50462471Sphk#define	fileno_unlocked(p)	__sfileno(p)
50562471Sphk#endif
50662471Sphk#if __POSIX_VISIBLE >= 199506
50762471Sphk#define	getc_unlocked(fp)	__sgetc(fp)
50862471Sphk#define	putc_unlocked(x, fp)	__sputc(x, fp)
50953913Sarchie
51053913Sarchie#define	getchar_unlocked()	getc_unlocked(stdin)
51153913Sarchie#define	putchar_unlocked(x)	putc_unlocked(x, stdout)
51253913Sarchie#endif
51353913Sarchie#endif /* __cplusplus */
51453913Sarchie
51553913Sarchie__END_DECLS
51653913Sarchie#endif /* !_STDIO_H_ */
51753913Sarchie