stdio.h revision 189355
157429Smarkm/*-
257429Smarkm * Copyright (c) 1990, 1993
357429Smarkm *	The Regents of the University of California.  All rights reserved.
457429Smarkm *
557429Smarkm * This code is derived from software contributed to Berkeley by
657429Smarkm * Chris Torek.
757429Smarkm *
860573Skris * Redistribution and use in source and binary forms, with or without
965668Skris * modification, are permitted provided that the following conditions
1065668Skris * are met:
1165668Skris * 1. Redistributions of source code must retain the above copyright
1265668Skris *    notice, this list of conditions and the following disclaimer.
1365668Skris * 2. Redistributions in binary form must reproduce the above copyright
1465668Skris *    notice, this list of conditions and the following disclaimer in the
1560573Skris *    documentation and/or other materials provided with the distribution.
1692559Sdes * 3. All advertising materials mentioning features or use of this software
1765668Skris *    must display the following acknowledgement:
1865668Skris *	This product includes software developed by the University of
1965668Skris *	California, Berkeley and its contributors.
2065668Skris * 4. Neither the name of the University nor the names of its contributors
2165668Skris *    may be used to endorse or promote products derived from this software
2265668Skris *    without specific prior written permission.
2365668Skris *
2465668Skris * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2565668Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2665668Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2765668Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2865668Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2965668Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3065668Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3165668Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3265668Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3365668Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3465668Skris * SUCH DAMAGE.
3565668Skris *
3665668Skris *	@(#)stdio.h	8.5 (Berkeley) 4/29/95
3765668Skris * $FreeBSD: head/include/stdio.h 189355 2009-03-04 03:35:03Z das $
3865668Skris */
3957429Smarkm
4057429Smarkm#ifndef	_STDIO_H_
4157429Smarkm#define	_STDIO_H_
42149753Sdes
4357429Smarkm#include <sys/cdefs.h>
4457429Smarkm#include <sys/_null.h>
4576262Sgreen#include <sys/_types.h>
4676262Sgreen
4757429Smarkmtypedef	__off_t		fpos_t;
4857429Smarkm
4976262Sgreen#ifndef _SIZE_T_DECLARED
5076262Sgreentypedef	__size_t	size_t;
5157429Smarkm#define	_SIZE_T_DECLARED
5257429Smarkm#endif
5376262Sgreen
5465668Skris#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809
5565668Skris#ifndef _OFF_T_DECLARED
5692559Sdes#define	_OFF_T_DECLARED
57124207Sdestypedef	__off_t		off_t;
5865668Skris#endif
5992559Sdes#ifndef _SSIZE_T_DECLARED
6057429Smarkm#define	_SSIZE_T_DECLARED
61147005Sdestypedef	__ssize_t	ssize_t;
62147005Sdes#endif
6357429Smarkm#endif
6457429Smarkm
6557429Smarkm#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
6657429Smarkm#ifndef _VA_LIST_DECLARED
6792559Sdestypedef	__va_list	va_list;
6857429Smarkm#define	_VA_LIST_DECLARED
6957429Smarkm#endif
7057429Smarkm#endif
7192559Sdes
7257429Smarkm#define	_FSTDIO			/* Define for new stdio with functions. */
73137019Sdes
7457429Smarkm/*
7557429Smarkm * NB: to fit things in six character monocase externals, the stdio
7657429Smarkm * code uses the prefix `__s' for stdio objects, typically followed
7792559Sdes * by a three-character attempt at a mnemonic.
7857429Smarkm */
7976262Sgreen
8057429Smarkm/* stdio buffers */
8157429Smarkmstruct __sbuf {
8292559Sdes	unsigned char *_base;
8357429Smarkm	int	_size;
8457429Smarkm};
8557429Smarkm
8657429Smarkm/*
8757429Smarkm * stdio state variables.
8857429Smarkm *
8957429Smarkm * The following always hold:
9057429Smarkm *
9160573Skris *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
9260573Skris *		_lbfsize is -_bf._size, else _lbfsize is 0
9360573Skris *	if _flags&__SRD, _w is 0
9457429Smarkm *	if _flags&__SWR, _r is 0
9557429Smarkm *
9657429Smarkm * This ensures that the getc and putc macros (or inline functions) never
9757429Smarkm * try to write or read from a file that is in `read' or `write' mode.
9892559Sdes * (Moreover, they can, and do, automatically switch from read mode to
9957429Smarkm * write mode, and back, on "r+" and "w+" files.)
10057429Smarkm *
10157429Smarkm * _lbfsize is used only to make the inline line-buffered output stream
10257429Smarkm * code as compact as possible.
10357429Smarkm *
10457429Smarkm * _ub, _up, and _ur are used when ungetc() pushes back more characters
10557429Smarkm * than fit in the current _bf, or when ungetc() pushes back a character
10657429Smarkm * that does not match the previous one in _bf.  When this happens,
10757429Smarkm * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
10857429Smarkm * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
10992559Sdes *
11092559Sdes * Certain members of __sFILE are accessed directly via macros or
11192559Sdes * inline functions.  To preserve ABI compat, these members must not
11292559Sdes * be disturbed.  These members are marked below with (*).
11392559Sdes */
114149753Sdestypedef	struct __sFILE {
115149753Sdes	unsigned char *_p;	/* (*) current position in (some) buffer */
116149753Sdes	int	_r;		/* (*) read space left for getc() */
11792559Sdes	int	_w;		/* (*) write space left for putc() */
11892559Sdes	short	_flags;		/* (*) flags, below; this FILE is free if 0 */
11992559Sdes	short	_file;		/* (*) fileno, if Unix descriptor, else -1 */
12092559Sdes	struct	__sbuf _bf;	/* (*) the buffer (at least 1 byte, if !NULL) */
12192559Sdes	int	_lbfsize;	/* (*) 0 or -_bf._size, for inline putc */
12292559Sdes
12392559Sdes	/* operations */
12492559Sdes	void	*_cookie;	/* (*) cookie passed to io functions */
12592559Sdes	int	(*_close)(void *);
12692559Sdes	int	(*_read)(void *, char *, int);
12792559Sdes	fpos_t	(*_seek)(void *, fpos_t, int);
12892559Sdes	int	(*_write)(void *, const char *, int);
12992559Sdes
13092559Sdes	/* separate buffer for long sequences of ungetc() */
13192559Sdes	struct	__sbuf _ub;	/* ungetc buffer */
13292559Sdes	unsigned char	*_up;	/* saved _p when _p is doing ungetc data */
13392559Sdes	int	_ur;		/* saved _r when _r is counting ungetc data */
13492559Sdes
13592559Sdes	/* tricks to meet minimum requirements even when malloc() fails */
13676262Sgreen	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
13798941Sdes	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
13876262Sgreen
13992559Sdes	/* separate buffer for fgetln() when line crosses buffer boundary */
14092559Sdes	struct	__sbuf _lb;	/* buffer for fgetln() */
14176262Sgreen
14292559Sdes	/* Unix stdio files get aligned to block boundaries on fseek() */
14357429Smarkm	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
14460573Skris	fpos_t	_offset;	/* current lseek offset */
14560573Skris
14660573Skris	struct pthread_mutex *_fl_mutex;	/* used for MT-safety */
14760573Skris	struct pthread *_fl_owner;	/* current owner */
14892559Sdes	int	_fl_count;	/* recursive lock count */
149137019Sdes	int	_orientation;	/* orientation for fwide() */
150124207Sdes	__mbstate_t _mbstate;	/* multibyte conversion state */
15160573Skris} FILE;
15260573Skris
15392559Sdes#ifndef _STDSTREAM_DECLARED
15492559Sdes__BEGIN_DECLS
155124207Sdesextern FILE *__stdinp;
15660573Skrisextern FILE *__stdoutp;
15760573Skrisextern FILE *__stderrp;
15860573Skris__END_DECLS
15960573Skris#define	_STDSTREAM_DECLARED
16060573Skris#endif
16157429Smarkm
16260573Skris#define	__SLBF	0x0001		/* line buffered */
16360573Skris#define	__SNBF	0x0002		/* unbuffered */
16460573Skris#define	__SRD	0x0004		/* OK to read */
16560573Skris#define	__SWR	0x0008		/* OK to write */
16692559Sdes	/* RD and WR are never simultaneously asserted */
16769587Sgreen#define	__SRW	0x0010		/* open for reading & writing */
16869587Sgreen#define	__SEOF	0x0020		/* found EOF */
16960573Skris#define	__SERR	0x0040		/* found error */
17060573Skris#define	__SMBF	0x0080		/* _buf is from malloc */
17176262Sgreen#define	__SAPP	0x0100		/* fdopen()ed in append mode */
17276262Sgreen#define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
17376262Sgreen#define	__SOPT	0x0400		/* do fseek() optimization */
17476262Sgreen#define	__SNPT	0x0800		/* do not do fseek() optimization */
17560573Skris#define	__SOFF	0x1000		/* set iff _offset is in fact correct */
17660573Skris#define	__SMOD	0x2000		/* true => fgetln modified _p text */
17760573Skris#define	__SALC	0x4000		/* allocate string space dynamically */
17860573Skris#define	__SIGN	0x8000		/* ignore this file in _fwalk */
17960573Skris
180137019Sdes/*
18160573Skris * The following three definitions are for ANSI C, which took them
18260573Skris * from System V, which brilliantly took internal interface macros and
18369587Sgreen * made them official arguments to setvbuf(), without renaming them.
18474500Sgreen * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
18574500Sgreen *
186124207Sdes * Although numbered as their counterparts above, the implementation
18774500Sgreen * does not rely on this.
18874500Sgreen */
18976262Sgreen#define	_IOFBF	0		/* setvbuf should set fully buffered */
19074500Sgreen#define	_IOLBF	1		/* setvbuf should set line buffered */
19174500Sgreen#define	_IONBF	2		/* setvbuf should set unbuffered */
19274500Sgreen
19374500Sgreen#define	BUFSIZ	1024		/* size of buffer used by setbuf */
19474500Sgreen#define	EOF	(-1)
195106130Sdes
19674500Sgreen/*
19769587Sgreen * FOPEN_MAX is a minimum maximum, and is the number of streams that
19869587Sgreen * stdio can provide without attempting to allocate further resources
19969587Sgreen * (which could fail).  Do not use this for anything.
20069587Sgreen */
20169587Sgreen				/* must be == _POSIX_STREAM_MAX <limits.h> */
20269587Sgreen#ifndef FOPEN_MAX
20369587Sgreen#define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
20469587Sgreen#endif
20569587Sgreen#define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
20660573Skris
20760573Skris/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
20860573Skris#if __XSI_VISIBLE
20957429Smarkm#define	P_tmpdir	"/var/tmp/"
21057429Smarkm#endif
21157429Smarkm#define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
21257429Smarkm#define	TMP_MAX		308915776
21392559Sdes
21460573Skris#ifndef SEEK_SET
21599063Sdes#define	SEEK_SET	0	/* set file offset to offset */
21657429Smarkm#endif
217137019Sdes#ifndef SEEK_CUR
218137019Sdes#define	SEEK_CUR	1	/* set file offset to current plus offset */
21957429Smarkm#endif
22057429Smarkm#ifndef SEEK_END
22157429Smarkm#define	SEEK_END	2	/* set file offset to EOF plus offset */
22257429Smarkm#endif
22357429Smarkm
22492559Sdes#define	stdin	__stdinp
22557429Smarkm#define	stdout	__stdoutp
22692559Sdes#define	stderr	__stderrp
22757429Smarkm
22857429Smarkm__BEGIN_DECLS
22957429Smarkm/*
23092559Sdes * Functions defined in ANSI C standard.
23157429Smarkm */
232137019Sdesvoid	 clearerr(FILE *);
23357429Smarkmint	 fclose(FILE *);
23457429Smarkmint	 feof(FILE *);
235137019Sdesint	 ferror(FILE *);
23657429Smarkmint	 fflush(FILE *);
23757429Smarkmint	 fgetc(FILE *);
23899063Sdesint	 fgetpos(FILE * __restrict, fpos_t * __restrict);
23999063Sdeschar	*fgets(char * __restrict, int, FILE * __restrict);
24099063SdesFILE	*fopen(const char * __restrict, const char * __restrict);
241120489Sjoeint	 fprintf(FILE * __restrict, const char * __restrict, ...);
242120489Sjoeint	 fputc(int, FILE *);
243120489Sjoeint	 fputs(const char * __restrict, FILE * __restrict);
24469587Sgreensize_t	 fread(void * __restrict, size_t, size_t, FILE * __restrict);
24557429SmarkmFILE	*freopen(const char * __restrict, const char * __restrict, FILE * __restrict);
24692559Sdesint	 fscanf(FILE * __restrict, const char * __restrict, ...);
24757429Smarkmint	 fseek(FILE *, long, int);
24892559Sdesint	 fsetpos(FILE *, const fpos_t *);
24992559Sdeslong	 ftell(FILE *);
25092559Sdessize_t	 fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
25157429Smarkmint	 getc(FILE *);
25257429Smarkmint	 getchar(void);
25360573Skrischar	*gets(char *);
25492559Sdesvoid	 perror(const char *);
25592559Sdesint	 printf(const char * __restrict, ...);
25692559Sdesint	 putc(int, FILE *);
25769587Sgreenint	 putchar(int);
25857429Smarkmint	 puts(const char *);
25957429Smarkmint	 remove(const char *);
26060573Skrisint	 rename(const char *, const char *);
26160573Skrisvoid	 rewind(FILE *);
26260573Skrisint	 scanf(const char * __restrict, ...);
26360573Skrisvoid	 setbuf(FILE * __restrict, char * __restrict);
26460573Skrisint	 setvbuf(FILE * __restrict, char * __restrict, int, size_t);
26557429Smarkmint	 sprintf(char * __restrict, const char * __restrict, ...);
266124207Sdesint	 sscanf(const char * __restrict, const char * __restrict, ...);
26760573SkrisFILE	*tmpfile(void);
26860573Skrischar	*tmpnam(char *);
26992559Sdesint	 ungetc(int, FILE *);
27092559Sdesint	 vfprintf(FILE * __restrict, const char * __restrict,
27192559Sdes	    __va_list);
27292559Sdesint	 vprintf(const char * __restrict, __va_list);
273137019Sdesint	 vsprintf(char * __restrict, const char * __restrict,
27465668Skris	    __va_list);
27557429Smarkm
27692559Sdes#if __ISO_C_VISIBLE >= 1999
27757429Smarkmint	 snprintf(char * __restrict, size_t, const char * __restrict,
27892559Sdes	    ...) __printflike(3, 4);
27992559Sdesint	 vfscanf(FILE * __restrict, const char * __restrict, __va_list)
28092559Sdes	    __scanflike(2, 0);
28192559Sdesint	 vscanf(const char * __restrict, __va_list) __scanflike(1, 0);
282137019Sdesint	 vsnprintf(char * __restrict, size_t, const char * __restrict,
283137019Sdes	    __va_list) __printflike(3, 0);
28492559Sdesint	 vsscanf(const char * __restrict, const char * __restrict, __va_list)
28592559Sdes	    __scanflike(2, 0);
28692559Sdes#endif
28792559Sdes
28892559Sdes/*
28992559Sdes * Functions defined in all versions of POSIX 1003.1.
29092559Sdes */
29192559Sdes#if __BSD_VISIBLE || __POSIX_VISIBLE <= 199506
29292559Sdes/* size for cuserid(3); UT_NAMESIZE + 1, see <utmp.h> */
29392559Sdes#define	L_cuserid	17	/* legacy */
29492559Sdes#endif
29592559Sdes
29692559Sdes#if __POSIX_VISIBLE
29760573Skris#define	L_ctermid	1024	/* size for ctermid(3); PATH_MAX */
29892559Sdes
29960573Skrischar	*ctermid(char *);
30092559SdesFILE	*fdopen(int, const char *);
30192559Sdesint	 fileno(FILE *);
30292559Sdes#endif /* __POSIX_VISIBLE */
30392559Sdes
30492559Sdes#if __POSIX_VISIBLE >= 199209
30592559Sdesint	 pclose(FILE *);
30692559SdesFILE	*popen(const char *, const char *);
30792559Sdes#endif
30892559Sdes
30960573Skris#if __POSIX_VISIBLE >= 199506
31057429Smarkmint	 ftrylockfile(FILE *);
31160573Skrisvoid	 flockfile(FILE *);
31260573Skrisvoid	 funlockfile(FILE *);
31392559Sdes
31460573Skris/*
31557429Smarkm * These are normally used through macros as defined below, but POSIX
316137019Sdes * requires functions as well.
317137019Sdes */
31892559Sdesint	 getc_unlocked(FILE *);
31992559Sdesint	 getchar_unlocked(void);
320137019Sdesint	 putc_unlocked(int, FILE *);
32192559Sdesint	 putchar_unlocked(int);
32292559Sdes#endif
32392559Sdes#if __BSD_VISIBLE
32460573Skrisvoid	 clearerr_unlocked(FILE *);
32557429Smarkmint	 feof_unlocked(FILE *);
32660573Skrisint	 ferror_unlocked(FILE *);
32760573Skrisint	 fileno_unlocked(FILE *);
32860573Skris#endif
32992559Sdes
33060573Skris#if __POSIX_VISIBLE >= 200112
33192559Sdesint	 fseeko(FILE *, __off_t, int);
332137019Sdes__off_t	 ftello(FILE *);
33376262Sgreen#endif
33492559Sdes
33592559Sdes#if __BSD_VISIBLE || __XSI_VISIBLE > 0 && __XSI_VISIBLE < 600
33692559Sdesint	 getw(FILE *);
337137019Sdesint	 putw(int, FILE *);
33892559Sdes#endif /* BSD or X/Open before issue 6 */
33992559Sdes
34092559Sdes#if __XSI_VISIBLE
341124207Sdeschar	*tempnam(const char *, const char *);
34276262Sgreen#endif
34376262Sgreen
34460573Skris#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809
34560573Skrisssize_t	 getdelim(char ** __restrict, size_t * __restrict, int,
346137019Sdes	    FILE * __restrict);
347137019Sdesint	 renameat(int, const char *, int, const char *);
34860573Skris
34960573Skris/*
35060573Skris * Every programmer and his dog wrote functions called getline()
35160573Skris * before POSIX.1-2008 came along and decided to usurp the name, so we
35260573Skris * don't prototype getline() by default unless one of the following is true:
35360573Skris *   a) the app has requested it specifically by defining _WITH_GETLINE
35460573Skris *   b) the app has requested a POSIX.1-2008 environment via _POSIX_C_SOURCE
35560573Skris *   c) the app defines a GNUism such as _BSD_SOURCE or _GNU_SOURCE
35692559Sdes */
35792559Sdes#ifndef _WITH_GETLINE
35857429Smarkm#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
35957429Smarkm#define	_WITH_GETLINE
36092559Sdes#elif defined(_POSIX_C_SOURCE)
36192559Sdes#if _POSIX_C_SOURCE > 200809
36292559Sdes#define	_WITH_GETLINE
363137019Sdes#endif
36492559Sdes#endif
36592559Sdes#endif
36692559Sdes
36792559Sdes#ifdef _WITH_GETLINE
36892559Sdesssize_t	 getline(char ** __restrict, size_t * __restrict, FILE * __restrict);
36992559Sdes#endif
37057429Smarkm
37192559Sdes#endif /* __BSD_VISIBLE || __POSIX_VISIBLE >= 200809 */
37292559Sdes
37392559Sdes/*
37492559Sdes * Routines that are purely local.
37592559Sdes */
37692559Sdes#if __BSD_VISIBLE
37792559Sdesint	 asprintf(char **, const char *, ...) __printflike(2, 3);
378137019Sdeschar	*ctermid_r(char *);
37992559Sdesvoid	 fcloseall(void);
38092559Sdeschar	*fgetln(FILE *, size_t *);
38192559Sdes__const char *fmtcheck(const char *, const char *) __format_arg(2);
38292559Sdesint	 fpurge(FILE *);
38392559Sdesvoid	 setbuffer(FILE *, char *, int);
38492559Sdesint	 setlinebuf(FILE *);
38592559Sdesint	 vasprintf(char **, const char *, __va_list)
38692559Sdes	    __printflike(2, 0);
38792559Sdes
38892559Sdes/*
38992559Sdes * The system error table contains messages for the first sys_nerr
39092559Sdes * positive errno values.  Use strerror() or strerror_r() from <string.h>
39192559Sdes * instead.
392137019Sdes */
39392559Sdesextern __const int sys_nerr;
39492559Sdesextern __const char *__const sys_errlist[];
39592559Sdes
39692559Sdes/*
39792559Sdes * Stdio function-access interface.
39892559Sdes */
39992559SdesFILE	*funopen(const void *,
40092559Sdes	    int (*)(void *, char *, int),
40192559Sdes	    int (*)(void *, const char *, int),
40292559Sdes	    fpos_t (*)(void *, fpos_t, int),
40392559Sdes	    int (*)(void *));
40492559Sdes#define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
40592559Sdes#define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
40692559Sdes
40792559Sdes/*
40892559Sdes * Portability hacks.  See <sys/types.h>.
40992559Sdes */
41092559Sdes#ifndef _FTRUNCATE_DECLARED
41192559Sdes#define	_FTRUNCATE_DECLARED
41292559Sdesint	 ftruncate(int, __off_t);
41392559Sdes#endif
41492559Sdes#ifndef _LSEEK_DECLARED
41592559Sdes#define	_LSEEK_DECLARED
41692559Sdes__off_t	 lseek(int, __off_t, int);
41792559Sdes#endif
41892559Sdes#ifndef _MMAP_DECLARED
41992559Sdes#define	_MMAP_DECLARED
42092559Sdesvoid	*mmap(void *, size_t, int, int, int, __off_t);
42192559Sdes#endif
42292559Sdes#ifndef _TRUNCATE_DECLARED
42392559Sdes#define	_TRUNCATE_DECLARED
42492559Sdesint	 truncate(const char *, __off_t);
42592559Sdes#endif
42692559Sdes#endif /* __BSD_VISIBLE */
42792559Sdes
428113911Sdes/*
42992559Sdes * Functions internal to the implementation.
43092559Sdes */
43192559Sdesint	__srget(FILE *);
43292559Sdesint	__swbuf(int, FILE *);
43392559Sdes
434124207Sdes/*
43592559Sdes * The __sfoo macros are here so that we can
43692559Sdes * define function versions in the C library.
43792559Sdes */
43892559Sdes#define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
43992559Sdes#if defined(__GNUC__) && defined(__STDC__)
44092559Sdesstatic __inline int __sputc(int _c, FILE *_p) {
44192559Sdes	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
44292559Sdes		return (*_p->_p++ = _c);
44392559Sdes	else
44492559Sdes		return (__swbuf(_c, _p));
44592559Sdes}
44692559Sdes#else
44792559Sdes/*
44892559Sdes * This has been tuned to generate reasonable code on the vax using pcc.
449137019Sdes */
45092559Sdes#define	__sputc(c, p) \
45192559Sdes	(--(p)->_w < 0 ? \
45292559Sdes		(p)->_w >= (p)->_lbfsize ? \
45392559Sdes			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
45492559Sdes				(int)*(p)->_p++ : \
45592559Sdes				__swbuf('\n', p) : \
45692559Sdes			__swbuf((int)(c), p) : \
45792559Sdes		(*(p)->_p = (c), (int)*(p)->_p++))
45892559Sdes#endif
45992559Sdes
46092559Sdes#define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
46192559Sdes#define	__sferror(p)	(((p)->_flags & __SERR) != 0)
46292559Sdes#define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
46392559Sdes#define	__sfileno(p)	((p)->_file)
46492559Sdes
46592559Sdesextern int __isthreaded;
46692559Sdes
46792559Sdes#define	feof(p)		(!__isthreaded ? __sfeof(p) : (feof)(p))
46892559Sdes#define	ferror(p)	(!__isthreaded ? __sferror(p) : (ferror)(p))
46992559Sdes#define	clearerr(p)	(!__isthreaded ? __sclearerr(p) : (clearerr)(p))
47092559Sdes
47192559Sdes#if __POSIX_VISIBLE
47292559Sdes#define	fileno(p)	(!__isthreaded ? __sfileno(p) : (fileno)(p))
47392559Sdes#endif
47492559Sdes
47592559Sdes#define	getc(fp)	(!__isthreaded ? __sgetc(fp) : (getc)(fp))
47692559Sdes#define	putc(x, fp)	(!__isthreaded ? __sputc(x, fp) : (putc)(x, fp))
47792559Sdes
47892559Sdes#define	getchar()	getc(stdin)
47992559Sdes#define	putchar(x)	putc(x, stdout)
48092559Sdes
48192559Sdes#if __BSD_VISIBLE
48292559Sdes/*
48392559Sdes * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
48492559Sdes * B.8.2.7 for the rationale behind the *_unlocked() macros.
48592559Sdes */
48692559Sdes#define	feof_unlocked(p)	__sfeof(p)
48792559Sdes#define	ferror_unlocked(p)	__sferror(p)
48892559Sdes#define	clearerr_unlocked(p)	__sclearerr(p)
48992559Sdes#define	fileno_unlocked(p)	__sfileno(p)
49092559Sdes#endif
49192559Sdes#if __POSIX_VISIBLE >= 199506
492137019Sdes#define	getc_unlocked(fp)	__sgetc(fp)
49392559Sdes#define	putc_unlocked(x, fp)	__sputc(x, fp)
49492559Sdes
49592559Sdes#define	getchar_unlocked()	getc_unlocked(stdin)
49692559Sdes#define	putchar_unlocked(x)	putc_unlocked(x, stdout)
497137019Sdes#endif
49892559Sdes
49992559Sdes__END_DECLS
50092559Sdes#endif /* !_STDIO_H_ */
50192559Sdes