xref: /original-bsd/include/stdio.h (revision 01e8f48f)
18c1325ffSbostic /*-
27faaa7b5Sbostic  * Copyright (c) 1990, 1993
37faaa7b5Sbostic  *	The Regents of the University of California.  All rights reserved.
46db880efSdist  *
58c1325ffSbostic  * This code is derived from software contributed to Berkeley by
68c1325ffSbostic  * Chris Torek.
78c1325ffSbostic  *
88c1325ffSbostic  * %sccs.include.redist.c%
98c1325ffSbostic  *
10*01e8f48fSbostic  *	@(#)stdio.h	8.5 (Berkeley) 04/29/95
116db880efSdist  */
126db880efSdist 
138c1325ffSbostic #ifndef	_STDIO_H_
148c1325ffSbostic #define	_STDIO_H_
158c1325ffSbostic 
16e058e875Storek #if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
17a28a5819Sbostic #include <sys/types.h>
18e058e875Storek #endif
19e058e875Storek 
207bde068dSbostic #include <sys/cdefs.h>
218c1325ffSbostic 
222b9924ebSbostic #include <machine/ansi.h>
234e5efec7Sbostic #ifdef	_BSD_SIZE_T_
244e5efec7Sbostic typedef	_BSD_SIZE_T_	size_t;
254e5efec7Sbostic #undef	_BSD_SIZE_T_
268c1325ffSbostic #endif
278c1325ffSbostic 
28a8c24cc3Sbostic #ifndef NULL
29a8c24cc3Sbostic #define	NULL	0
30a8c24cc3Sbostic #endif
31a8c24cc3Sbostic 
32e058e875Storek /*
33e058e875Storek  * This is fairly grotesque, but pure ANSI code must not inspect the
34e058e875Storek  * innards of an fpos_t anyway.  The library internally uses off_t,
3501aaaee2Storek  * which we assume is exactly as big as eight chars.  (When we switch
3601aaaee2Storek  * to gcc 2.4 we will use __attribute__ here.)
3701aaaee2Storek  *
3801aaaee2Storek  * WARNING: the alignment constraints on an off_t and the struct below
3901aaaee2Storek  * differ on (e.g.) the SPARC.  Hence, the placement of an fpos_t object
4001aaaee2Storek  * in a structure will change if fpos_t's are not aligned on 8-byte
4101aaaee2Storek  * boundaries.  THIS IS A CROCK, but for now there is no way around it.
42e058e875Storek  */
43e058e875Storek #if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
44e058e875Storek typedef off_t fpos_t;
45e058e875Storek #else
46e058e875Storek typedef struct __sfpos {
47e058e875Storek 	char	_pos[8];
48e058e875Storek } fpos_t;
49e058e875Storek #endif
505fb3de76Stoy 
518c1325ffSbostic #define	_FSTDIO			/* Define for new stdio with functions. */
525fb3de76Stoy 
537bde068dSbostic /*
547bde068dSbostic  * NB: to fit things in six character monocase externals, the stdio
557bde068dSbostic  * code uses the prefix `__s' for stdio objects, typically followed
567bde068dSbostic  * by a three-character attempt at a mnemonic.
577bde068dSbostic  */
587bde068dSbostic 
598c1325ffSbostic /* stdio buffers */
608c1325ffSbostic struct __sbuf {
618c1325ffSbostic 	unsigned char *_base;
628c1325ffSbostic 	int	_size;
638c1325ffSbostic };
645fb3de76Stoy 
658c1325ffSbostic /*
668c1325ffSbostic  * stdio state variables.
678c1325ffSbostic  *
688c1325ffSbostic  * The following always hold:
698c1325ffSbostic  *
708c1325ffSbostic  *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
718c1325ffSbostic  *		_lbfsize is -_bf._size, else _lbfsize is 0
728c1325ffSbostic  *	if _flags&__SRD, _w is 0
738c1325ffSbostic  *	if _flags&__SWR, _r is 0
748c1325ffSbostic  *
758c1325ffSbostic  * This ensures that the getc and putc macros (or inline functions) never
768c1325ffSbostic  * try to write or read from a file that is in `read' or `write' mode.
778c1325ffSbostic  * (Moreover, they can, and do, automatically switch from read mode to
788c1325ffSbostic  * write mode, and back, on "r+" and "w+" files.)
798c1325ffSbostic  *
808c1325ffSbostic  * _lbfsize is used only to make the inline line-buffered output stream
818c1325ffSbostic  * code as compact as possible.
828c1325ffSbostic  *
838c1325ffSbostic  * _ub, _up, and _ur are used when ungetc() pushes back more characters
848c1325ffSbostic  * than fit in the current _bf, or when ungetc() pushes back a character
858c1325ffSbostic  * that does not match the previous one in _bf.  When this happens,
868c1325ffSbostic  * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
878c1325ffSbostic  * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
8801aaaee2Storek  *
8901aaaee2Storek  * NB: see WARNING above before changing the layout of this structure!
908c1325ffSbostic  */
918c1325ffSbostic typedef	struct __sFILE {
928c1325ffSbostic 	unsigned char *_p;	/* current position in (some) buffer */
938c1325ffSbostic 	int	_r;		/* read space left for getc() */
948c1325ffSbostic 	int	_w;		/* write space left for putc() */
958c1325ffSbostic 	short	_flags;		/* flags, below; this FILE is free if 0 */
968c1325ffSbostic 	short	_file;		/* fileno, if Unix descriptor, else -1 */
978c1325ffSbostic 	struct	__sbuf _bf;	/* the buffer (at least 1 byte, if !NULL) */
988c1325ffSbostic 	int	_lbfsize;	/* 0 or -_bf._size, for inline putc */
998c1325ffSbostic 
1008c1325ffSbostic 	/* operations */
1018c1325ffSbostic 	void	*_cookie;	/* cookie passed to io functions */
102e505d896Sdonn 	int	(*_close) __P((void *));
103e505d896Sdonn 	int	(*_read)  __P((void *, char *, int));
104e505d896Sdonn 	fpos_t	(*_seek)  __P((void *, fpos_t, int));
105e505d896Sdonn 	int	(*_write) __P((void *, const char *, int));
106cbae850eSkfall 
1078c1325ffSbostic 	/* separate buffer for long sequences of ungetc() */
1088c1325ffSbostic 	struct	__sbuf _ub;	/* ungetc buffer */
1098c1325ffSbostic 	unsigned char *_up;	/* saved _p when _p is doing ungetc data */
1108c1325ffSbostic 	int	_ur;		/* saved _r when _r is counting ungetc data */
1118c1325ffSbostic 
1128c1325ffSbostic 	/* tricks to meet minimum requirements even when malloc() fails */
1138c1325ffSbostic 	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
1148c1325ffSbostic 	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
1158c1325ffSbostic 
116eef10579Sbostic 	/* separate buffer for fgetln() when line crosses buffer boundary */
117eef10579Sbostic 	struct	__sbuf _lb;	/* buffer for fgetln() */
1188c1325ffSbostic 
1198c1325ffSbostic 	/* Unix stdio files get aligned to block boundaries on fseek() */
1208c1325ffSbostic 	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
12101aaaee2Storek 	fpos_t	_offset;	/* current lseek offset (see WARNING) */
1228c1325ffSbostic } FILE;
1238c1325ffSbostic 
1244b0a96f3Sdonn __BEGIN_DECLS
1258c1325ffSbostic extern FILE __sF[];
1264b0a96f3Sdonn __END_DECLS
1278c1325ffSbostic 
1288c1325ffSbostic #define	__SLBF	0x0001		/* line buffered */
1298c1325ffSbostic #define	__SNBF	0x0002		/* unbuffered */
1308c1325ffSbostic #define	__SRD	0x0004		/* OK to read */
1318c1325ffSbostic #define	__SWR	0x0008		/* OK to write */
1328c1325ffSbostic 	/* RD and WR are never simultaneously asserted */
1338c1325ffSbostic #define	__SRW	0x0010		/* open for reading & writing */
1348c1325ffSbostic #define	__SEOF	0x0020		/* found EOF */
1358c1325ffSbostic #define	__SERR	0x0040		/* found error */
1368c1325ffSbostic #define	__SMBF	0x0080		/* _buf is from malloc */
1378c1325ffSbostic #define	__SAPP	0x0100		/* fdopen()ed in append mode */
1388c1325ffSbostic #define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
1398c1325ffSbostic #define	__SOPT	0x0400		/* do fseek() optimisation */
1408c1325ffSbostic #define	__SNPT	0x0800		/* do not do fseek() optimisation */
1418c1325ffSbostic #define	__SOFF	0x1000		/* set iff _offset is in fact correct */
142eef10579Sbostic #define	__SMOD	0x2000		/* true => fgetln modified _p text */
1438c1325ffSbostic 
1448c1325ffSbostic /*
1458c1325ffSbostic  * The following three definitions are for ANSI C, which took them
1468c1325ffSbostic  * from System V, which brilliantly took internal interface macros and
1478c1325ffSbostic  * made them official arguments to setvbuf(), without renaming them.
1488c1325ffSbostic  * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
1498c1325ffSbostic  *
1508c1325ffSbostic  * Although numbered as their counterparts above, the implementation
1518c1325ffSbostic  * does not rely on this.
1528c1325ffSbostic  */
1538c1325ffSbostic #define	_IOFBF	0		/* setvbuf should set fully buffered */
1548c1325ffSbostic #define	_IOLBF	1		/* setvbuf should set line buffered */
1558c1325ffSbostic #define	_IONBF	2		/* setvbuf should set unbuffered */
1568c1325ffSbostic 
1578c1325ffSbostic #define	BUFSIZ	1024		/* size of buffer used by setbuf */
1588c1325ffSbostic #define	EOF	(-1)
1598c1325ffSbostic 
1608c1325ffSbostic /*
161012ed3b4Sbostic  * FOPEN_MAX is a minimum maximum, and is the number of streams that
162012ed3b4Sbostic  * stdio can provide without attempting to allocate further resources
163012ed3b4Sbostic  * (which could fail).  Do not use this for anything.
1648c1325ffSbostic  */
165012ed3b4Sbostic 				/* must be == _POSIX_STREAM_MAX <limits.h> */
1668c1325ffSbostic #define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
1678c1325ffSbostic #define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
1688c1325ffSbostic 
1698c1325ffSbostic /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
1708c1325ffSbostic #ifndef _ANSI_SOURCE
171e57e7607Sbostic #define	P_tmpdir	"/var/tmp/"
1728c1325ffSbostic #endif
1738c1325ffSbostic #define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
1748c1325ffSbostic #define	TMP_MAX		308915776
1758c1325ffSbostic 
1768c1325ffSbostic #ifndef SEEK_SET
1778c1325ffSbostic #define	SEEK_SET	0	/* set file offset to offset */
1788c1325ffSbostic #endif
1798c1325ffSbostic #ifndef SEEK_CUR
1808c1325ffSbostic #define	SEEK_CUR	1	/* set file offset to current plus offset */
1818c1325ffSbostic #endif
1828c1325ffSbostic #ifndef SEEK_END
1838c1325ffSbostic #define	SEEK_END	2	/* set file offset to EOF plus offset */
1848c1325ffSbostic #endif
1858c1325ffSbostic 
1868c1325ffSbostic #define	stdin	(&__sF[0])
1878c1325ffSbostic #define	stdout	(&__sF[1])
1888c1325ffSbostic #define	stderr	(&__sF[2])
1898c1325ffSbostic 
1908c1325ffSbostic /*
1918c1325ffSbostic  * Functions defined in ANSI C standard.
1928c1325ffSbostic  */
1937bde068dSbostic __BEGIN_DECLS
1947bde068dSbostic void	 clearerr __P((FILE *));
1957bde068dSbostic int	 fclose __P((FILE *));
1967bde068dSbostic int	 feof __P((FILE *));
1977bde068dSbostic int	 ferror __P((FILE *));
1987bde068dSbostic int	 fflush __P((FILE *));
1997bde068dSbostic int	 fgetc __P((FILE *));
2007bde068dSbostic int	 fgetpos __P((FILE *, fpos_t *));
2017bde068dSbostic char	*fgets __P((char *, size_t, FILE *));
202e505d896Sdonn FILE	*fopen __P((const char *, const char *));
2037bde068dSbostic int	 fprintf __P((FILE *, const char *, ...));
2047bde068dSbostic int	 fputc __P((int, FILE *));
2057bde068dSbostic int	 fputs __P((const char *, FILE *));
206d852a0c9Sbostic size_t	 fread __P((void *, size_t, size_t, FILE *));
207e505d896Sdonn FILE	*freopen __P((const char *, const char *, FILE *));
2087bde068dSbostic int	 fscanf __P((FILE *, const char *, ...));
2097bde068dSbostic int	 fseek __P((FILE *, long, int));
2107bde068dSbostic int	 fsetpos __P((FILE *, const fpos_t *));
211*01e8f48fSbostic long	 ftell __P((FILE *));
212d852a0c9Sbostic size_t	 fwrite __P((const void *, size_t, size_t, FILE *));
2137bde068dSbostic int	 getc __P((FILE *));
2147bde068dSbostic int	 getchar __P((void));
2157bde068dSbostic char	*gets __P((char *));
216d603dd2eSbostic #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
217d603dd2eSbostic extern int sys_nerr;			/* perror(3) external variables */
218fd635c53Sbostic extern __const char *__const sys_errlist[];
219d603dd2eSbostic #endif
2207bde068dSbostic void	 perror __P((const char *));
2217bde068dSbostic int	 printf __P((const char *, ...));
2227bde068dSbostic int	 putc __P((int, FILE *));
2237bde068dSbostic int	 putchar __P((int));
2247bde068dSbostic int	 puts __P((const char *));
2257bde068dSbostic int	 remove __P((const char *));
226e505d896Sdonn int	 rename  __P((const char *, const char *));
2277bde068dSbostic void	 rewind __P((FILE *));
2287bde068dSbostic int	 scanf __P((const char *, ...));
2297bde068dSbostic void	 setbuf __P((FILE *, char *));
2307bde068dSbostic int	 setvbuf __P((FILE *, char *, int, size_t));
2317bde068dSbostic int	 sprintf __P((char *, const char *, ...));
232e600f3eaStorek int	 sscanf __P((const char *, const char *, ...));
2337bde068dSbostic FILE	*tmpfile __P((void));
2347bde068dSbostic char	*tmpnam __P((char *));
2357bde068dSbostic int	 ungetc __P((int, FILE *));
2364e5efec7Sbostic int	 vfprintf __P((FILE *, const char *, _BSD_VA_LIST_));
2374e5efec7Sbostic int	 vprintf __P((const char *, _BSD_VA_LIST_));
2384e5efec7Sbostic int	 vsprintf __P((char *, const char *, _BSD_VA_LIST_));
2397bde068dSbostic __END_DECLS
2408c1325ffSbostic 
2418c1325ffSbostic /*
2428c1325ffSbostic  * Functions defined in POSIX 1003.1.
2438c1325ffSbostic  */
2448c1325ffSbostic #ifndef _ANSI_SOURCE
2458c1325ffSbostic #define	L_cuserid	9	/* size for cuserid(); UT_NAMESIZE + 1 */
2468c1325ffSbostic #define	L_ctermid	1024	/* size for ctermid(); PATH_MAX */
2478c1325ffSbostic 
2487bde068dSbostic __BEGIN_DECLS
2490db5e65cSbostic char	*ctermid __P((char *));
2507bde068dSbostic FILE	*fdopen __P((int, const char *));
2517bde068dSbostic int	 fileno __P((FILE *));
2527bde068dSbostic __END_DECLS
253e505d896Sdonn #endif /* not ANSI */
2548c1325ffSbostic 
2558c1325ffSbostic /*
2568c1325ffSbostic  * Routines that are purely local.
2578c1325ffSbostic  */
258e505d896Sdonn #if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
2597bde068dSbostic __BEGIN_DECLS
260eef10579Sbostic char	*fgetln __P((FILE *, size_t *));
2617bde068dSbostic int	 fpurge __P((FILE *));
2627bde068dSbostic int	 getw __P((FILE *));
2637bde068dSbostic int	 pclose __P((FILE *));
264e505d896Sdonn FILE	*popen __P((const char *, const char *));
2657bde068dSbostic int	 putw __P((int, FILE *));
2667bde068dSbostic void	 setbuffer __P((FILE *, char *, int));
2677bde068dSbostic int	 setlinebuf __P((FILE *));
268e57e7607Sbostic char	*tempnam __P((const char *, const char *));
2697bde068dSbostic int	 snprintf __P((char *, size_t, const char *, ...));
2704e5efec7Sbostic int	 vsnprintf __P((char *, size_t, const char *, _BSD_VA_LIST_));
2714e5efec7Sbostic int	 vscanf __P((const char *, _BSD_VA_LIST_));
2724e5efec7Sbostic int	 vsscanf __P((const char *, const char *, _BSD_VA_LIST_));
273d812d6a1Sbostic FILE	*zopen __P((const char *, const char *, int));
2747bde068dSbostic __END_DECLS
2758c1325ffSbostic 
2768c1325ffSbostic /*
277433618cdSdonn  * This is a #define because the function is used internally and
278433618cdSdonn  * (unlike vfscanf) the name __svfscanf is guaranteed not to collide
279433618cdSdonn  * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined.
280433618cdSdonn  */
281433618cdSdonn #define	 vfscanf	__svfscanf
282433618cdSdonn 
283433618cdSdonn /*
2848c1325ffSbostic  * Stdio function-access interface.
2858c1325ffSbostic  */
2867bde068dSbostic __BEGIN_DECLS
287e505d896Sdonn FILE	*funopen __P((const void *,
288e505d896Sdonn 		int (*)(void *, char *, int),
289e505d896Sdonn 		int (*)(void *, const char *, int),
290e505d896Sdonn 		fpos_t (*)(void *, fpos_t, int),
291e505d896Sdonn 		int (*)(void *)));
2927bde068dSbostic __END_DECLS
2938c1325ffSbostic #define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
2948c1325ffSbostic #define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
2954b0a96f3Sdonn #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
2968c1325ffSbostic 
2978c1325ffSbostic /*
2988c1325ffSbostic  * Functions internal to the implementation.
2998c1325ffSbostic  */
3004b0a96f3Sdonn __BEGIN_DECLS
3017bde068dSbostic int	__srget __P((FILE *));
3024e5efec7Sbostic int	__svfscanf __P((FILE *, const char *, _BSD_VA_LIST_));
3037bde068dSbostic int	__swbuf __P((int, FILE *));
3044b0a96f3Sdonn __END_DECLS
3058c1325ffSbostic 
3068c1325ffSbostic /*
3078c1325ffSbostic  * The __sfoo macros are here so that we can
3088c1325ffSbostic  * define function versions in the C library.
3098c1325ffSbostic  */
3108c1325ffSbostic #define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
311433618cdSdonn #if defined(__GNUC__) && defined(__STDC__)
__sputc(int _c,FILE * _p)312552d4617Sbostic static __inline int __sputc(int _c, FILE *_p) {
3138c1325ffSbostic 	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
3148c1325ffSbostic 		return (*_p->_p++ = _c);
3158c1325ffSbostic 	else
3168c1325ffSbostic 		return (__swbuf(_c, _p));
3178c1325ffSbostic }
3188c1325ffSbostic #else
3198c1325ffSbostic /*
3208c1325ffSbostic  * This has been tuned to generate reasonable code on the vax using pcc.
3218c1325ffSbostic  */
3228c1325ffSbostic #define	__sputc(c, p) \
3238c1325ffSbostic 	(--(p)->_w < 0 ? \
3248c1325ffSbostic 		(p)->_w >= (p)->_lbfsize ? \
3258c1325ffSbostic 			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
3268c1325ffSbostic 				(int)*(p)->_p++ : \
3278c1325ffSbostic 				__swbuf('\n', p) : \
3288c1325ffSbostic 			__swbuf((int)(c), p) : \
3298c1325ffSbostic 		(*(p)->_p = (c), (int)*(p)->_p++))
3308c1325ffSbostic #endif
3318c1325ffSbostic 
3328c1325ffSbostic #define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
3338c1325ffSbostic #define	__sferror(p)	(((p)->_flags & __SERR) != 0)
3348c1325ffSbostic #define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
3358c1325ffSbostic #define	__sfileno(p)	((p)->_file)
3368c1325ffSbostic 
3378c1325ffSbostic #define	feof(p)		__sfeof(p)
3388c1325ffSbostic #define	ferror(p)	__sferror(p)
3398c1325ffSbostic #define	clearerr(p)	__sclearerr(p)
3408c1325ffSbostic 
3418c1325ffSbostic #ifndef _ANSI_SOURCE
3428c1325ffSbostic #define	fileno(p)	__sfileno(p)
3438c1325ffSbostic #endif
3448c1325ffSbostic 
3458c1325ffSbostic #ifndef lint
3468c1325ffSbostic #define	getc(fp)	__sgetc(fp)
3478c1325ffSbostic #define putc(x, fp)	__sputc(x, fp)
3488c1325ffSbostic #endif /* lint */
3498c1325ffSbostic 
3508c1325ffSbostic #define	getchar()	getc(stdin)
3518c1325ffSbostic #define	putchar(x)	putc(x, stdout)
3528c1325ffSbostic #endif /* _STDIO_H_ */
353