stdio.h revision 19211
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 *
361539Srgrimes *	@(#)stdio.h	8.4 (Berkeley) 1/4/94
3719211Swosch *	$Id$
381539Srgrimes */
391539Srgrimes
401539Srgrimes#ifndef	_STDIO_H_
411539Srgrimes#define	_STDIO_H_
421539Srgrimes
431539Srgrimes#if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
441539Srgrimes#include <sys/types.h>
451539Srgrimes#endif
461539Srgrimes
471539Srgrimes#include <sys/cdefs.h>
481539Srgrimes
491539Srgrimes#include <machine/ansi.h>
501539Srgrimes#ifdef	_BSD_SIZE_T_
511539Srgrimestypedef	_BSD_SIZE_T_	size_t;
521539Srgrimes#undef	_BSD_SIZE_T_
531539Srgrimes#endif
541539Srgrimes
551539Srgrimes#ifndef NULL
561539Srgrimes#define	NULL	0
571539Srgrimes#endif
581539Srgrimes
591539Srgrimes/*
601539Srgrimes * This is fairly grotesque, but pure ANSI code must not inspect the
611539Srgrimes * innards of an fpos_t anyway.  The library internally uses off_t,
621539Srgrimes * which we assume is exactly as big as eight chars.  (When we switch
631539Srgrimes * to gcc 2.4 we will use __attribute__ here.)
641539Srgrimes *
651539Srgrimes * WARNING: the alignment constraints on an off_t and the struct below
661539Srgrimes * differ on (e.g.) the SPARC.  Hence, the placement of an fpos_t object
671539Srgrimes * in a structure will change if fpos_t's are not aligned on 8-byte
681539Srgrimes * boundaries.  THIS IS A CROCK, but for now there is no way around it.
691539Srgrimes */
701539Srgrimes#if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
711539Srgrimestypedef off_t fpos_t;
721539Srgrimes#else
731539Srgrimestypedef struct __sfpos {
741539Srgrimes	char	_pos[8];
751539Srgrimes} fpos_t;
761539Srgrimes#endif
771539Srgrimes
781539Srgrimes#define	_FSTDIO			/* Define for new stdio with functions. */
791539Srgrimes
801539Srgrimes/*
811539Srgrimes * NB: to fit things in six character monocase externals, the stdio
821539Srgrimes * code uses the prefix `__s' for stdio objects, typically followed
831539Srgrimes * by a three-character attempt at a mnemonic.
841539Srgrimes */
851539Srgrimes
861539Srgrimes/* stdio buffers */
871539Srgrimesstruct __sbuf {
881539Srgrimes	unsigned char *_base;
891539Srgrimes	int	_size;
901539Srgrimes};
911539Srgrimes
921539Srgrimes/*
931539Srgrimes * stdio state variables.
941539Srgrimes *
951539Srgrimes * The following always hold:
961539Srgrimes *
971539Srgrimes *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
981539Srgrimes *		_lbfsize is -_bf._size, else _lbfsize is 0
991539Srgrimes *	if _flags&__SRD, _w is 0
1001539Srgrimes *	if _flags&__SWR, _r is 0
1011539Srgrimes *
1021539Srgrimes * This ensures that the getc and putc macros (or inline functions) never
1031539Srgrimes * try to write or read from a file that is in `read' or `write' mode.
1041539Srgrimes * (Moreover, they can, and do, automatically switch from read mode to
1051539Srgrimes * write mode, and back, on "r+" and "w+" files.)
1061539Srgrimes *
1071539Srgrimes * _lbfsize is used only to make the inline line-buffered output stream
1081539Srgrimes * code as compact as possible.
1091539Srgrimes *
1101539Srgrimes * _ub, _up, and _ur are used when ungetc() pushes back more characters
1111539Srgrimes * than fit in the current _bf, or when ungetc() pushes back a character
1121539Srgrimes * that does not match the previous one in _bf.  When this happens,
1131539Srgrimes * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
1141539Srgrimes * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
1151539Srgrimes *
1161539Srgrimes * NB: see WARNING above before changing the layout of this structure!
1171539Srgrimes */
1181539Srgrimestypedef	struct __sFILE {
1191539Srgrimes	unsigned char *_p;	/* current position in (some) buffer */
1201539Srgrimes	int	_r;		/* read space left for getc() */
1211539Srgrimes	int	_w;		/* write space left for putc() */
1221539Srgrimes	short	_flags;		/* flags, below; this FILE is free if 0 */
1231539Srgrimes	short	_file;		/* fileno, if Unix descriptor, else -1 */
1241539Srgrimes	struct	__sbuf _bf;	/* the buffer (at least 1 byte, if !NULL) */
1251539Srgrimes	int	_lbfsize;	/* 0 or -_bf._size, for inline putc */
1261539Srgrimes
1271539Srgrimes	/* operations */
1281539Srgrimes	void	*_cookie;	/* cookie passed to io functions */
1291539Srgrimes	int	(*_close) __P((void *));
1301539Srgrimes	int	(*_read)  __P((void *, char *, int));
1311539Srgrimes	fpos_t	(*_seek)  __P((void *, fpos_t, int));
1321539Srgrimes	int	(*_write) __P((void *, const char *, int));
1331539Srgrimes
1341539Srgrimes	/* separate buffer for long sequences of ungetc() */
1351539Srgrimes	struct	__sbuf _ub;	/* ungetc buffer */
1361539Srgrimes	unsigned char *_up;	/* saved _p when _p is doing ungetc data */
1371539Srgrimes	int	_ur;		/* saved _r when _r is counting ungetc data */
1381539Srgrimes
1391539Srgrimes	/* tricks to meet minimum requirements even when malloc() fails */
1401539Srgrimes	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
1411539Srgrimes	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
1421539Srgrimes
1431539Srgrimes	/* separate buffer for fgetln() when line crosses buffer boundary */
1441539Srgrimes	struct	__sbuf _lb;	/* buffer for fgetln() */
1451539Srgrimes
1461539Srgrimes	/* Unix stdio files get aligned to block boundaries on fseek() */
1471539Srgrimes	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
1481539Srgrimes	fpos_t	_offset;	/* current lseek offset (see WARNING) */
1491539Srgrimes} FILE;
1501539Srgrimes
1511539Srgrimes__BEGIN_DECLS
1521539Srgrimesextern FILE __sF[];
1531539Srgrimes__END_DECLS
1541539Srgrimes
1551539Srgrimes#define	__SLBF	0x0001		/* line buffered */
1561539Srgrimes#define	__SNBF	0x0002		/* unbuffered */
1571539Srgrimes#define	__SRD	0x0004		/* OK to read */
1581539Srgrimes#define	__SWR	0x0008		/* OK to write */
1591539Srgrimes	/* RD and WR are never simultaneously asserted */
1601539Srgrimes#define	__SRW	0x0010		/* open for reading & writing */
1611539Srgrimes#define	__SEOF	0x0020		/* found EOF */
1621539Srgrimes#define	__SERR	0x0040		/* found error */
1631539Srgrimes#define	__SMBF	0x0080		/* _buf is from malloc */
1641539Srgrimes#define	__SAPP	0x0100		/* fdopen()ed in append mode */
1651539Srgrimes#define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
16613771Smpp#define	__SOPT	0x0400		/* do fseek() optimization */
16713771Smpp#define	__SNPT	0x0800		/* do not do fseek() optimization */
1681539Srgrimes#define	__SOFF	0x1000		/* set iff _offset is in fact correct */
1691539Srgrimes#define	__SMOD	0x2000		/* true => fgetln modified _p text */
1701539Srgrimes
1711539Srgrimes/*
1721539Srgrimes * The following three definitions are for ANSI C, which took them
1731539Srgrimes * from System V, which brilliantly took internal interface macros and
1741539Srgrimes * made them official arguments to setvbuf(), without renaming them.
1751539Srgrimes * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
1761539Srgrimes *
1771539Srgrimes * Although numbered as their counterparts above, the implementation
1781539Srgrimes * does not rely on this.
1791539Srgrimes */
1801539Srgrimes#define	_IOFBF	0		/* setvbuf should set fully buffered */
1811539Srgrimes#define	_IOLBF	1		/* setvbuf should set line buffered */
1821539Srgrimes#define	_IONBF	2		/* setvbuf should set unbuffered */
1831539Srgrimes
1841539Srgrimes#define	BUFSIZ	1024		/* size of buffer used by setbuf */
1851539Srgrimes#define	EOF	(-1)
1861539Srgrimes
1871539Srgrimes/*
1881539Srgrimes * FOPEN_MAX is a minimum maximum, and is the number of streams that
1891539Srgrimes * stdio can provide without attempting to allocate further resources
1901539Srgrimes * (which could fail).  Do not use this for anything.
1911539Srgrimes */
1921539Srgrimes				/* must be == _POSIX_STREAM_MAX <limits.h> */
1931539Srgrimes#define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
1941539Srgrimes#define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
1951539Srgrimes
1961539Srgrimes/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
1971539Srgrimes#ifndef _ANSI_SOURCE
1981539Srgrimes#define	P_tmpdir	"/var/tmp/"
1991539Srgrimes#endif
2001539Srgrimes#define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
2011539Srgrimes#define	TMP_MAX		308915776
2021539Srgrimes
2031539Srgrimes#ifndef SEEK_SET
2041539Srgrimes#define	SEEK_SET	0	/* set file offset to offset */
2051539Srgrimes#endif
2061539Srgrimes#ifndef SEEK_CUR
2071539Srgrimes#define	SEEK_CUR	1	/* set file offset to current plus offset */
2081539Srgrimes#endif
2091539Srgrimes#ifndef SEEK_END
2101539Srgrimes#define	SEEK_END	2	/* set file offset to EOF plus offset */
2111539Srgrimes#endif
2121539Srgrimes
2131539Srgrimes#define	stdin	(&__sF[0])
2141539Srgrimes#define	stdout	(&__sF[1])
2151539Srgrimes#define	stderr	(&__sF[2])
2161539Srgrimes
2171539Srgrimes/*
2181539Srgrimes * Functions defined in ANSI C standard.
2191539Srgrimes */
2201539Srgrimes__BEGIN_DECLS
2211539Srgrimesvoid	 clearerr __P((FILE *));
2221539Srgrimesint	 fclose __P((FILE *));
2231539Srgrimesint	 feof __P((FILE *));
2241539Srgrimesint	 ferror __P((FILE *));
2251539Srgrimesint	 fflush __P((FILE *));
2261539Srgrimesint	 fgetc __P((FILE *));
2271539Srgrimesint	 fgetpos __P((FILE *, fpos_t *));
22814791Spaulchar	*fgets __P((char *, int, FILE *));
2291539SrgrimesFILE	*fopen __P((const char *, const char *));
2301539Srgrimesint	 fprintf __P((FILE *, const char *, ...));
2311539Srgrimesint	 fputc __P((int, FILE *));
2321539Srgrimesint	 fputs __P((const char *, FILE *));
2331539Srgrimessize_t	 fread __P((void *, size_t, size_t, FILE *));
2341539SrgrimesFILE	*freopen __P((const char *, const char *, FILE *));
2351539Srgrimesint	 fscanf __P((FILE *, const char *, ...));
2361539Srgrimesint	 fseek __P((FILE *, long, int));
2371539Srgrimesint	 fsetpos __P((FILE *, const fpos_t *));
23814791Spaullong	 ftell __P((FILE *));
2391539Srgrimessize_t	 fwrite __P((const void *, size_t, size_t, FILE *));
2401539Srgrimesint	 getc __P((FILE *));
2411539Srgrimesint	 getchar __P((void));
2421539Srgrimeschar	*gets __P((char *));
2431539Srgrimes#if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
2446895Sphkextern __const int sys_nerr;		/* perror(3) external variables */
2451539Srgrimesextern __const char *__const sys_errlist[];
2461539Srgrimes#endif
2471539Srgrimesvoid	 perror __P((const char *));
2481539Srgrimesint	 printf __P((const char *, ...));
2491539Srgrimesint	 putc __P((int, FILE *));
2501539Srgrimesint	 putchar __P((int));
2511539Srgrimesint	 puts __P((const char *));
2521539Srgrimesint	 remove __P((const char *));
2531539Srgrimesint	 rename  __P((const char *, const char *));
2541539Srgrimesvoid	 rewind __P((FILE *));
2551539Srgrimesint	 scanf __P((const char *, ...));
2561539Srgrimesvoid	 setbuf __P((FILE *, char *));
2571539Srgrimesint	 setvbuf __P((FILE *, char *, int, size_t));
2581539Srgrimesint	 sprintf __P((char *, const char *, ...));
2591539Srgrimesint	 sscanf __P((const char *, const char *, ...));
2601539SrgrimesFILE	*tmpfile __P((void));
2611539Srgrimeschar	*tmpnam __P((char *));
2621539Srgrimesint	 ungetc __P((int, FILE *));
2631539Srgrimesint	 vfprintf __P((FILE *, const char *, _BSD_VA_LIST_));
2641539Srgrimesint	 vprintf __P((const char *, _BSD_VA_LIST_));
2651539Srgrimesint	 vsprintf __P((char *, const char *, _BSD_VA_LIST_));
2661539Srgrimes__END_DECLS
2671539Srgrimes
2681539Srgrimes/*
2691539Srgrimes * Functions defined in POSIX 1003.1.
2701539Srgrimes */
2711539Srgrimes#ifndef _ANSI_SOURCE
27219211Swosch/* size for cuserid(3); UT_NAMESIZE + 1, see <utmp.h> */
27319211Swosch#define	L_cuserid	9
2741539Srgrimes
27519211Swosch#define	L_ctermid	1024	/* size for ctermid(3); PATH_MAX */
27619211Swosch
2771539Srgrimes__BEGIN_DECLS
2781539Srgrimeschar	*ctermid __P((char *));
2791539SrgrimesFILE	*fdopen __P((int, const char *));
2801539Srgrimesint	 fileno __P((FILE *));
2811539Srgrimes__END_DECLS
2821539Srgrimes#endif /* not ANSI */
2831539Srgrimes
2841539Srgrimes/*
2851539Srgrimes * Routines that are purely local.
2861539Srgrimes */
2871539Srgrimes#if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
2881539Srgrimes__BEGIN_DECLS
28915931Speterint	 asprintf __P((char **, const char *, ...));
2901539Srgrimeschar	*fgetln __P((FILE *, size_t *));
2911539Srgrimesint	 fpurge __P((FILE *));
2921539Srgrimesint	 getw __P((FILE *));
2931539Srgrimesint	 pclose __P((FILE *));
2941539SrgrimesFILE	*popen __P((const char *, const char *));
2951539Srgrimesint	 putw __P((int, FILE *));
2961539Srgrimesvoid	 setbuffer __P((FILE *, char *, int));
2971539Srgrimesint	 setlinebuf __P((FILE *));
2981539Srgrimeschar	*tempnam __P((const char *, const char *));
2991539Srgrimesint	 snprintf __P((char *, size_t, const char *, ...));
30015931Speterint	 vasprintf __P((char **, const char *, _BSD_VA_LIST_));
3011539Srgrimesint	 vsnprintf __P((char *, size_t, const char *, _BSD_VA_LIST_));
3021539Srgrimesint	 vscanf __P((const char *, _BSD_VA_LIST_));
3031539Srgrimesint	 vsscanf __P((const char *, const char *, _BSD_VA_LIST_));
3041539Srgrimes__END_DECLS
3051539Srgrimes
3061539Srgrimes/*
3071539Srgrimes * This is a #define because the function is used internally and
3081539Srgrimes * (unlike vfscanf) the name __svfscanf is guaranteed not to collide
3091539Srgrimes * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined.
3101539Srgrimes */
3111539Srgrimes#define	 vfscanf	__svfscanf
3121539Srgrimes
3131539Srgrimes/*
3141539Srgrimes * Stdio function-access interface.
3151539Srgrimes */
3161539Srgrimes__BEGIN_DECLS
3171539SrgrimesFILE	*funopen __P((const void *,
3181539Srgrimes		int (*)(void *, char *, int),
3191539Srgrimes		int (*)(void *, const char *, int),
3201539Srgrimes		fpos_t (*)(void *, fpos_t, int),
3211539Srgrimes		int (*)(void *)));
3221539Srgrimes__END_DECLS
3231539Srgrimes#define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
3241539Srgrimes#define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
3251539Srgrimes#endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
3261539Srgrimes
3271539Srgrimes/*
3281539Srgrimes * Functions internal to the implementation.
3291539Srgrimes */
3301539Srgrimes__BEGIN_DECLS
3311539Srgrimesint	__srget __P((FILE *));
3321539Srgrimesint	__svfscanf __P((FILE *, const char *, _BSD_VA_LIST_));
3331539Srgrimesint	__swbuf __P((int, FILE *));
3341539Srgrimes__END_DECLS
3351539Srgrimes
3361539Srgrimes/*
3378858Srgrimes * The __sfoo macros are here so that we can
3381539Srgrimes * define function versions in the C library.
3391539Srgrimes */
3401539Srgrimes#define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
3411539Srgrimes#if defined(__GNUC__) && defined(__STDC__)
3421539Srgrimesstatic __inline int __sputc(int _c, FILE *_p) {
3431539Srgrimes	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
3441539Srgrimes		return (*_p->_p++ = _c);
3451539Srgrimes	else
3461539Srgrimes		return (__swbuf(_c, _p));
3471539Srgrimes}
3481539Srgrimes#else
3491539Srgrimes/*
3501539Srgrimes * This has been tuned to generate reasonable code on the vax using pcc.
3511539Srgrimes */
3521539Srgrimes#define	__sputc(c, p) \
3531539Srgrimes	(--(p)->_w < 0 ? \
3541539Srgrimes		(p)->_w >= (p)->_lbfsize ? \
3551539Srgrimes			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
3561539Srgrimes				(int)*(p)->_p++ : \
3571539Srgrimes				__swbuf('\n', p) : \
3581539Srgrimes			__swbuf((int)(c), p) : \
3591539Srgrimes		(*(p)->_p = (c), (int)*(p)->_p++))
3601539Srgrimes#endif
3611539Srgrimes
3621539Srgrimes#define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
3631539Srgrimes#define	__sferror(p)	(((p)->_flags & __SERR) != 0)
3641539Srgrimes#define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
3651539Srgrimes#define	__sfileno(p)	((p)->_file)
3661539Srgrimes
3671539Srgrimes#define	feof(p)		__sfeof(p)
3681539Srgrimes#define	ferror(p)	__sferror(p)
3691539Srgrimes#define	clearerr(p)	__sclearerr(p)
3701539Srgrimes
3711539Srgrimes#ifndef _ANSI_SOURCE
3721539Srgrimes#define	fileno(p)	__sfileno(p)
3731539Srgrimes#endif
3741539Srgrimes
3751539Srgrimes#ifndef lint
3761539Srgrimes#define	getc(fp)	__sgetc(fp)
3771539Srgrimes#define putc(x, fp)	__sputc(x, fp)
3781539Srgrimes#endif /* lint */
3791539Srgrimes
3801539Srgrimes#define	getchar()	getc(stdin)
3811539Srgrimes#define	putchar(x)	putc(x, stdout)
3821539Srgrimes#endif /* _STDIO_H_ */
383