stdio.h revision 190409
11539Srgrimes/*-
21539Srgrimes * Copyright (c) 1990, 1993
31539Srgrimes *	The Regents of the University of California.  All rights reserved.
41539Srgrimes *
51539Srgrimes * This code is derived from software contributed to Berkeley by
61539Srgrimes * Chris Torek.
71539Srgrimes *
81539Srgrimes * Redistribution and use in source and binary forms, with or without
91539Srgrimes * modification, are permitted provided that the following conditions
101539Srgrimes * are met:
111539Srgrimes * 1. Redistributions of source code must retain the above copyright
121539Srgrimes *    notice, this list of conditions and the following disclaimer.
131539Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141539Srgrimes *    notice, this list of conditions and the following disclaimer in the
151539Srgrimes *    documentation and/or other materials provided with the distribution.
161539Srgrimes * 3. All advertising materials mentioning features or use of this software
171539Srgrimes *    must display the following acknowledgement:
181539Srgrimes *	This product includes software developed by the University of
191539Srgrimes *	California, Berkeley and its contributors.
201539Srgrimes * 4. Neither the name of the University nor the names of its contributors
211539Srgrimes *    may be used to endorse or promote products derived from this software
221539Srgrimes *    without specific prior written permission.
231539Srgrimes *
241539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271539Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341539Srgrimes * SUCH DAMAGE.
351539Srgrimes *
3623655Speter *	@(#)stdio.h	8.5 (Berkeley) 4/29/95
3750473Speter * $FreeBSD: head/include/stdio.h 190409 2009-03-25 08:07:52Z das $
381539Srgrimes */
391539Srgrimes
401539Srgrimes#ifndef	_STDIO_H_
411539Srgrimes#define	_STDIO_H_
421539Srgrimes
431539Srgrimes#include <sys/cdefs.h>
44123257Smarcel#include <sys/_null.h>
45102227Smike#include <sys/_types.h>
461539Srgrimes
47104585Smiketypedef	__off_t		fpos_t;
48104585Smike
49102227Smike#ifndef _SIZE_T_DECLARED
50102227Smiketypedef	__size_t	size_t;
51102227Smike#define	_SIZE_T_DECLARED
521539Srgrimes#endif
531539Srgrimes
54189136Sdas#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809
55189136Sdas#ifndef _OFF_T_DECLARED
56189136Sdas#define	_OFF_T_DECLARED
57189136Sdastypedef	__off_t		off_t;
58189136Sdas#endif
59189136Sdas#ifndef _SSIZE_T_DECLARED
60189136Sdas#define	_SSIZE_T_DECLARED
61189136Sdastypedef	__ssize_t	ssize_t;
62189136Sdas#endif
63189136Sdas#endif
64189136Sdas
65104585Smike#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
66104585Smike#ifndef _VA_LIST_DECLARED
67104585Smiketypedef	__va_list	va_list;
68104585Smike#define	_VA_LIST_DECLARED
69104585Smike#endif
70104585Smike#endif
71104585Smike
721539Srgrimes#define	_FSTDIO			/* Define for new stdio with functions. */
731539Srgrimes
74178778Sjhb/*
75178778Sjhb * NB: to fit things in six character monocase externals, the stdio
76178778Sjhb * code uses the prefix `__s' for stdio objects, typically followed
77178778Sjhb * by a three-character attempt at a mnemonic.
78178778Sjhb */
791539Srgrimes
80178778Sjhb/* stdio buffers */
81178778Sjhbstruct __sbuf {
82178778Sjhb	unsigned char *_base;
83178778Sjhb	int	_size;
84178778Sjhb};
85178778Sjhb
86178778Sjhb/*
87178778Sjhb * stdio state variables.
88178778Sjhb *
89178778Sjhb * The following always hold:
90178778Sjhb *
91178778Sjhb *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
92178778Sjhb *		_lbfsize is -_bf._size, else _lbfsize is 0
93178778Sjhb *	if _flags&__SRD, _w is 0
94178778Sjhb *	if _flags&__SWR, _r is 0
95178778Sjhb *
96178778Sjhb * This ensures that the getc and putc macros (or inline functions) never
97178778Sjhb * try to write or read from a file that is in `read' or `write' mode.
98178778Sjhb * (Moreover, they can, and do, automatically switch from read mode to
99178778Sjhb * write mode, and back, on "r+" and "w+" files.)
100178778Sjhb *
101178778Sjhb * _lbfsize is used only to make the inline line-buffered output stream
102178778Sjhb * code as compact as possible.
103178778Sjhb *
104178778Sjhb * _ub, _up, and _ur are used when ungetc() pushes back more characters
105178778Sjhb * than fit in the current _bf, or when ungetc() pushes back a character
106178778Sjhb * that does not match the previous one in _bf.  When this happens,
107178778Sjhb * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
108178778Sjhb * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
109178778Sjhb *
110178778Sjhb * Certain members of __sFILE are accessed directly via macros or
111178778Sjhb * inline functions.  To preserve ABI compat, these members must not
112178778Sjhb * be disturbed.  These members are marked below with (*).
113178778Sjhb */
114178778Sjhbtypedef	struct __sFILE {
115178778Sjhb	unsigned char *_p;	/* (*) current position in (some) buffer */
116178778Sjhb	int	_r;		/* (*) read space left for getc() */
117178778Sjhb	int	_w;		/* (*) write space left for putc() */
118178778Sjhb	short	_flags;		/* (*) flags, below; this FILE is free if 0 */
119178778Sjhb	short	_file;		/* (*) fileno, if Unix descriptor, else -1 */
120178829Sjhb	struct	__sbuf _bf;	/* (*) the buffer (at least 1 byte, if !NULL) */
121178778Sjhb	int	_lbfsize;	/* (*) 0 or -_bf._size, for inline putc */
122178778Sjhb
123178778Sjhb	/* operations */
124178779Sjhb	void	*_cookie;	/* (*) cookie passed to io functions */
125178778Sjhb	int	(*_close)(void *);
126178778Sjhb	int	(*_read)(void *, char *, int);
127178778Sjhb	fpos_t	(*_seek)(void *, fpos_t, int);
128178778Sjhb	int	(*_write)(void *, const char *, int);
129178778Sjhb
130178778Sjhb	/* separate buffer for long sequences of ungetc() */
131178778Sjhb	struct	__sbuf _ub;	/* ungetc buffer */
132178778Sjhb	unsigned char	*_up;	/* saved _p when _p is doing ungetc data */
133178778Sjhb	int	_ur;		/* saved _r when _r is counting ungetc data */
134178778Sjhb
135178778Sjhb	/* tricks to meet minimum requirements even when malloc() fails */
136178778Sjhb	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
137178778Sjhb	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
138178778Sjhb
139178778Sjhb	/* separate buffer for fgetln() when line crosses buffer boundary */
140178778Sjhb	struct	__sbuf _lb;	/* buffer for fgetln() */
141178778Sjhb
142178778Sjhb	/* Unix stdio files get aligned to block boundaries on fseek() */
143178778Sjhb	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
144178778Sjhb	fpos_t	_offset;	/* current lseek offset */
145178778Sjhb
146178778Sjhb	struct pthread_mutex *_fl_mutex;	/* used for MT-safety */
147178778Sjhb	struct pthread *_fl_owner;	/* current owner */
148178778Sjhb	int	_fl_count;	/* recursive lock count */
149178778Sjhb	int	_orientation;	/* orientation for fwide() */
150178778Sjhb	__mbstate_t _mbstate;	/* multibyte conversion state */
151178778Sjhb} FILE;
152178778Sjhb
153129774Stjr#ifndef _STDSTREAM_DECLARED
1541539Srgrimes__BEGIN_DECLS
15581600Speterextern FILE *__stdinp;
15681600Speterextern FILE *__stdoutp;
15781600Speterextern FILE *__stderrp;
1581539Srgrimes__END_DECLS
159129774Stjr#define	_STDSTREAM_DECLARED
160129774Stjr#endif
1611539Srgrimes
162178778Sjhb#define	__SLBF	0x0001		/* line buffered */
163178778Sjhb#define	__SNBF	0x0002		/* unbuffered */
164178778Sjhb#define	__SRD	0x0004		/* OK to read */
165178778Sjhb#define	__SWR	0x0008		/* OK to write */
166178778Sjhb	/* RD and WR are never simultaneously asserted */
167178778Sjhb#define	__SRW	0x0010		/* open for reading & writing */
168178778Sjhb#define	__SEOF	0x0020		/* found EOF */
169178778Sjhb#define	__SERR	0x0040		/* found error */
170178778Sjhb#define	__SMBF	0x0080		/* _buf is from malloc */
171178778Sjhb#define	__SAPP	0x0100		/* fdopen()ed in append mode */
172178778Sjhb#define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
173178778Sjhb#define	__SOPT	0x0400		/* do fseek() optimization */
174178778Sjhb#define	__SNPT	0x0800		/* do not do fseek() optimization */
175178778Sjhb#define	__SOFF	0x1000		/* set iff _offset is in fact correct */
176178778Sjhb#define	__SMOD	0x2000		/* true => fgetln modified _p text */
177178778Sjhb#define	__SALC	0x4000		/* allocate string space dynamically */
178178778Sjhb#define	__SIGN	0x8000		/* ignore this file in _fwalk */
179178778Sjhb
1801539Srgrimes/*
1811539Srgrimes * The following three definitions are for ANSI C, which took them
1821539Srgrimes * from System V, which brilliantly took internal interface macros and
1831539Srgrimes * made them official arguments to setvbuf(), without renaming them.
1841539Srgrimes * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
185178778Sjhb *
186178778Sjhb * Although numbered as their counterparts above, the implementation
187178778Sjhb * does not rely on this.
1881539Srgrimes */
1891539Srgrimes#define	_IOFBF	0		/* setvbuf should set fully buffered */
1901539Srgrimes#define	_IOLBF	1		/* setvbuf should set line buffered */
1911539Srgrimes#define	_IONBF	2		/* setvbuf should set unbuffered */
1921539Srgrimes
1931539Srgrimes#define	BUFSIZ	1024		/* size of buffer used by setbuf */
1941539Srgrimes#define	EOF	(-1)
1951539Srgrimes
1961539Srgrimes/*
1971539Srgrimes * FOPEN_MAX is a minimum maximum, and is the number of streams that
1981539Srgrimes * stdio can provide without attempting to allocate further resources
1991539Srgrimes * (which could fail).  Do not use this for anything.
2001539Srgrimes */
2011539Srgrimes				/* must be == _POSIX_STREAM_MAX <limits.h> */
202177653Sjb#ifndef FOPEN_MAX
2031539Srgrimes#define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
204177653Sjb#endif
2051539Srgrimes#define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
2061539Srgrimes
2071539Srgrimes/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
208100133Swollman#if __XSI_VISIBLE
2091539Srgrimes#define	P_tmpdir	"/var/tmp/"
2101539Srgrimes#endif
2111539Srgrimes#define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
2121539Srgrimes#define	TMP_MAX		308915776
2131539Srgrimes
2141539Srgrimes#ifndef SEEK_SET
2151539Srgrimes#define	SEEK_SET	0	/* set file offset to offset */
2161539Srgrimes#endif
2171539Srgrimes#ifndef SEEK_CUR
2181539Srgrimes#define	SEEK_CUR	1	/* set file offset to current plus offset */
2191539Srgrimes#endif
2201539Srgrimes#ifndef SEEK_END
2211539Srgrimes#define	SEEK_END	2	/* set file offset to EOF plus offset */
2221539Srgrimes#endif
2231539Srgrimes
22481600Speter#define	stdin	__stdinp
22581600Speter#define	stdout	__stdoutp
22681600Speter#define	stderr	__stderrp
2271539Srgrimes
228100133Swollman__BEGIN_DECLS
2291539Srgrimes/*
2301539Srgrimes * Functions defined in ANSI C standard.
2311539Srgrimes */
23293032Simpvoid	 clearerr(FILE *);
23393032Simpint	 fclose(FILE *);
23493032Simpint	 feof(FILE *);
23593032Simpint	 ferror(FILE *);
23693032Simpint	 fflush(FILE *);
23793032Simpint	 fgetc(FILE *);
238104989Smikeint	 fgetpos(FILE * __restrict, fpos_t * __restrict);
239104989Smikechar	*fgets(char * __restrict, int, FILE * __restrict);
240104989SmikeFILE	*fopen(const char * __restrict, const char * __restrict);
241103012Stjrint	 fprintf(FILE * __restrict, const char * __restrict, ...);
24293032Simpint	 fputc(int, FILE *);
243104989Smikeint	 fputs(const char * __restrict, FILE * __restrict);
244104989Smikesize_t	 fread(void * __restrict, size_t, size_t, FILE * __restrict);
245104989SmikeFILE	*freopen(const char * __restrict, const char * __restrict, FILE * __restrict);
246104989Smikeint	 fscanf(FILE * __restrict, const char * __restrict, ...);
24793032Simpint	 fseek(FILE *, long, int);
24893032Simpint	 fsetpos(FILE *, const fpos_t *);
24993032Simplong	 ftell(FILE *);
250104989Smikesize_t	 fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
25193032Simpint	 getc(FILE *);
25293032Simpint	 getchar(void);
25393032Simpchar	*gets(char *);
25493032Simpvoid	 perror(const char *);
255103012Stjrint	 printf(const char * __restrict, ...);
25693032Simpint	 putc(int, FILE *);
25793032Simpint	 putchar(int);
25893032Simpint	 puts(const char *);
25993032Simpint	 remove(const char *);
26093032Simpint	 rename(const char *, const char *);
26193032Simpvoid	 rewind(FILE *);
262104989Smikeint	 scanf(const char * __restrict, ...);
263103012Stjrvoid	 setbuf(FILE * __restrict, char * __restrict);
264103012Stjrint	 setvbuf(FILE * __restrict, char * __restrict, int, size_t);
265103012Stjrint	 sprintf(char * __restrict, const char * __restrict, ...);
266104989Smikeint	 sscanf(const char * __restrict, const char * __restrict, ...);
26793032SimpFILE	*tmpfile(void);
26893032Simpchar	*tmpnam(char *);
26993032Simpint	 ungetc(int, FILE *);
270103012Stjrint	 vfprintf(FILE * __restrict, const char * __restrict,
271102227Smike	    __va_list);
272103012Stjrint	 vprintf(const char * __restrict, __va_list);
273103012Stjrint	 vsprintf(char * __restrict, const char * __restrict,
274102227Smike	    __va_list);
2751539Srgrimes
276100133Swollman#if __ISO_C_VISIBLE >= 1999
277103012Stjrint	 snprintf(char * __restrict, size_t, const char * __restrict,
278101914Srobert	    ...) __printflike(3, 4);
279105098Stjrint	 vfscanf(FILE * __restrict, const char * __restrict, __va_list)
280105098Stjr	    __scanflike(2, 0);
281104989Smikeint	 vscanf(const char * __restrict, __va_list) __scanflike(1, 0);
282103012Stjrint	 vsnprintf(char * __restrict, size_t, const char * __restrict,
283102227Smike	    __va_list) __printflike(3, 0);
284104989Smikeint	 vsscanf(const char * __restrict, const char * __restrict, __va_list)
285104585Smike	    __scanflike(2, 0);
286100133Swollman#endif
287100133Swollman
2881539Srgrimes/*
289100133Swollman * Functions defined in all versions of POSIX 1003.1.
2901539Srgrimes */
291104585Smike#if __BSD_VISIBLE || __POSIX_VISIBLE <= 199506
29219211Swosch/* size for cuserid(3); UT_NAMESIZE + 1, see <utmp.h> */
293104585Smike#define	L_cuserid	17	/* legacy */
294104585Smike#endif
2951539Srgrimes
296104585Smike#if __POSIX_VISIBLE
29719211Swosch#define	L_ctermid	1024	/* size for ctermid(3); PATH_MAX */
29819211Swosch
29993032Simpchar	*ctermid(char *);
30093032SimpFILE	*fdopen(int, const char *);
30193032Simpint	 fileno(FILE *);
302100133Swollman#endif /* __POSIX_VISIBLE */
303100133Swollman
304100133Swollman#if __POSIX_VISIBLE >= 199209
305100133Swollmanint	 pclose(FILE *);
306100133SwollmanFILE	*popen(const char *, const char *);
307100133Swollman#endif
308100133Swollman
309100133Swollman#if __POSIX_VISIBLE >= 199506
31093032Simpint	 ftrylockfile(FILE *);
31193032Simpvoid	 flockfile(FILE *);
31293032Simpvoid	 funlockfile(FILE *);
3131539Srgrimes
3141539Srgrimes/*
315178778Sjhb * These are normally used through macros as defined below, but POSIX
316178778Sjhb * requires functions as well.
31724897Sbde */
318100133Swollmanint	 getc_unlocked(FILE *);
319100133Swollmanint	 getchar_unlocked(void);
320100133Swollmanint	 putc_unlocked(int, FILE *);
321100133Swollmanint	 putchar_unlocked(int);
32224897Sbde#endif
323109168Stjr#if __BSD_VISIBLE
324178722Sjhbvoid	 clearerr_unlocked(FILE *);
325178722Sjhbint	 feof_unlocked(FILE *);
326178722Sjhbint	 ferror_unlocked(FILE *);
327178722Sjhbint	 fileno_unlocked(FILE *);
328109168Stjr#endif
329100133Swollman
330100133Swollman#if __POSIX_VISIBLE >= 200112
331102227Smikeint	 fseeko(FILE *, __off_t, int);
332102227Smike__off_t	 ftello(FILE *);
33324897Sbde#endif
334100133Swollman
335100133Swollman#if __BSD_VISIBLE || __XSI_VISIBLE > 0 && __XSI_VISIBLE < 600
336100133Swollmanint	 getw(FILE *);
337100133Swollmanint	 putw(int, FILE *);
338100133Swollman#endif /* BSD or X/Open before issue 6 */
339100133Swollman
340100133Swollman#if __XSI_VISIBLE
341100133Swollmanchar	*tempnam(const char *, const char *);
34224897Sbde#endif
34324897Sbde
344189136Sdas#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809
345189136Sdasssize_t	 getdelim(char ** __restrict, size_t * __restrict, int,
346189136Sdas	    FILE * __restrict);
347189355Sdasint	 renameat(int, const char *, int, const char *);
348189356Sdasint	 vdprintf(int, const char * __restrict, __va_list);
349189136Sdas
35024897Sbde/*
351189356Sdas * Every programmer and his dog wrote functions called getline() and dprintf()
352189356Sdas * before POSIX.1-2008 came along and decided to usurp the names, so we
353189356Sdas * don't prototype them by default unless one of the following is true:
354189356Sdas *   a) the app has requested them specifically by defining _WITH_GETLINE or
355189356Sdas *      _WITH_DPRINTF, respectively
356189136Sdas *   b) the app has requested a POSIX.1-2008 environment via _POSIX_C_SOURCE
357189136Sdas *   c) the app defines a GNUism such as _BSD_SOURCE or _GNU_SOURCE
358189136Sdas */
359189136Sdas#ifndef _WITH_GETLINE
360189136Sdas#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
361189136Sdas#define	_WITH_GETLINE
362189136Sdas#elif defined(_POSIX_C_SOURCE)
363189818Sdas#if _POSIX_C_SOURCE >= 200809
364189136Sdas#define	_WITH_GETLINE
365189136Sdas#endif
366189136Sdas#endif
367189136Sdas#endif
368189136Sdas
369189136Sdas#ifdef _WITH_GETLINE
370189136Sdasssize_t	 getline(char ** __restrict, size_t * __restrict, FILE * __restrict);
371189136Sdas#endif
372189136Sdas
373189356Sdas#ifndef _WITH_DPRINTF
374189356Sdas#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
375189356Sdas#define	_WITH_DPRINTF
376189356Sdas#elif defined(_POSIX_C_SOURCE)
377189818Sdas#if _POSIX_C_SOURCE >= 200809
378189356Sdas#define	_WITH_DPRINTF
379189356Sdas#endif
380189356Sdas#endif
381189356Sdas#endif
382189356Sdas
383189356Sdas#ifdef _WITH_DPRINTF
384190409Sdasint	 (dprintf)(int, const char * __restrict, ...);
385189356Sdas#endif
386189356Sdas
387189136Sdas#endif /* __BSD_VISIBLE || __POSIX_VISIBLE >= 200809 */
388189136Sdas
389189136Sdas/*
3901539Srgrimes * Routines that are purely local.
3911539Srgrimes */
392100133Swollman#if __BSD_VISIBLE
39393032Simpint	 asprintf(char **, const char *, ...) __printflike(2, 3);
39493032Simpchar	*ctermid_r(char *);
395178722Sjhbvoid	 fcloseall(void);
39693032Simpchar	*fgetln(FILE *, size_t *);
397154867Sstefanf__const char *fmtcheck(const char *, const char *) __format_arg(2);
39893032Simpint	 fpurge(FILE *);
39993032Simpvoid	 setbuffer(FILE *, char *, int);
40093032Simpint	 setlinebuf(FILE *);
401102227Smikeint	 vasprintf(char **, const char *, __va_list)
40237614Sbde	    __printflike(2, 0);
4031539Srgrimes
4041539Srgrimes/*
405100133Swollman * The system error table contains messages for the first sys_nerr
406100133Swollman * positive errno values.  Use strerror() or strerror_r() from <string.h>
407100133Swollman * instead.
408100133Swollman */
409100133Swollmanextern __const int sys_nerr;
410100133Swollmanextern __const char *__const sys_errlist[];
411100133Swollman
412100133Swollman/*
4131539Srgrimes * Stdio function-access interface.
4141539Srgrimes */
41593032SimpFILE	*funopen(const void *,
41675818Sobrien	    int (*)(void *, char *, int),
41775818Sobrien	    int (*)(void *, const char *, int),
41875818Sobrien	    fpos_t (*)(void *, fpos_t, int),
41993032Simp	    int (*)(void *));
4201539Srgrimes#define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
4211539Srgrimes#define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
4221539Srgrimes
4231539Srgrimes/*
424100133Swollman * Portability hacks.  See <sys/types.h>.
425100133Swollman */
426100133Swollman#ifndef _FTRUNCATE_DECLARED
427100133Swollman#define	_FTRUNCATE_DECLARED
428102227Smikeint	 ftruncate(int, __off_t);
429100133Swollman#endif
430100133Swollman#ifndef _LSEEK_DECLARED
431100133Swollman#define	_LSEEK_DECLARED
432102227Smike__off_t	 lseek(int, __off_t, int);
433100133Swollman#endif
434100133Swollman#ifndef _MMAP_DECLARED
435100133Swollman#define	_MMAP_DECLARED
436102227Smikevoid	*mmap(void *, size_t, int, int, int, __off_t);
437100133Swollman#endif
438100133Swollman#ifndef _TRUNCATE_DECLARED
439100133Swollman#define	_TRUNCATE_DECLARED
440102227Smikeint	 truncate(const char *, __off_t);
441100133Swollman#endif
442100133Swollman#endif /* __BSD_VISIBLE */
443100133Swollman
444178778Sjhb/*
445178778Sjhb * Functions internal to the implementation.
446178778Sjhb */
447178778Sjhbint	__srget(FILE *);
448178778Sjhbint	__swbuf(int, FILE *);
449178778Sjhb
450178778Sjhb/*
451178778Sjhb * The __sfoo macros are here so that we can
452178778Sjhb * define function versions in the C library.
453178778Sjhb */
454178778Sjhb#define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
455178778Sjhb#if defined(__GNUC__) && defined(__STDC__)
456178778Sjhbstatic __inline int __sputc(int _c, FILE *_p) {
457178778Sjhb	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
458178778Sjhb		return (*_p->_p++ = _c);
459178778Sjhb	else
460178778Sjhb		return (__swbuf(_c, _p));
461178778Sjhb}
462178778Sjhb#else
463178778Sjhb/*
464178778Sjhb * This has been tuned to generate reasonable code on the vax using pcc.
465178778Sjhb */
466178778Sjhb#define	__sputc(c, p) \
467178778Sjhb	(--(p)->_w < 0 ? \
468178778Sjhb		(p)->_w >= (p)->_lbfsize ? \
469178778Sjhb			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
470178778Sjhb				(int)*(p)->_p++ : \
471178778Sjhb				__swbuf('\n', p) : \
472178778Sjhb			__swbuf((int)(c), p) : \
473178778Sjhb		(*(p)->_p = (c), (int)*(p)->_p++))
474178778Sjhb#endif
475178778Sjhb
476178778Sjhb#define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
477178778Sjhb#define	__sferror(p)	(((p)->_flags & __SERR) != 0)
478178778Sjhb#define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
479178778Sjhb#define	__sfileno(p)	((p)->_file)
480178778Sjhb
481178778Sjhbextern int __isthreaded;
482178778Sjhb
483178778Sjhb#define	feof(p)		(!__isthreaded ? __sfeof(p) : (feof)(p))
484178778Sjhb#define	ferror(p)	(!__isthreaded ? __sferror(p) : (ferror)(p))
485178778Sjhb#define	clearerr(p)	(!__isthreaded ? __sclearerr(p) : (clearerr)(p))
486178778Sjhb
487178778Sjhb#if __POSIX_VISIBLE
488178778Sjhb#define	fileno(p)	(!__isthreaded ? __sfileno(p) : (fileno)(p))
489178778Sjhb#endif
490178778Sjhb
491178778Sjhb#define	getc(fp)	(!__isthreaded ? __sgetc(fp) : (getc)(fp))
492178778Sjhb#define	putc(x, fp)	(!__isthreaded ? __sputc(x, fp) : (putc)(x, fp))
493178778Sjhb
494178778Sjhb#define	getchar()	getc(stdin)
495178778Sjhb#define	putchar(x)	putc(x, stdout)
496178778Sjhb
497178778Sjhb#if __BSD_VISIBLE
498178778Sjhb/*
499178778Sjhb * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
500178778Sjhb * B.8.2.7 for the rationale behind the *_unlocked() macros.
501178778Sjhb */
502178778Sjhb#define	feof_unlocked(p)	__sfeof(p)
503178778Sjhb#define	ferror_unlocked(p)	__sferror(p)
504178778Sjhb#define	clearerr_unlocked(p)	__sclearerr(p)
505178778Sjhb#define	fileno_unlocked(p)	__sfileno(p)
506178778Sjhb#endif
507178778Sjhb#if __POSIX_VISIBLE >= 199506
508178778Sjhb#define	getc_unlocked(fp)	__sgetc(fp)
509178778Sjhb#define	putc_unlocked(x, fp)	__sputc(x, fp)
510178778Sjhb
511178778Sjhb#define	getchar_unlocked()	getc_unlocked(stdin)
512178778Sjhb#define	putchar_unlocked(x)	putc_unlocked(x, stdout)
513178778Sjhb#endif
514178778Sjhb
515100133Swollman__END_DECLS
51624897Sbde#endif /* !_STDIO_H_ */
517