xref: /original-bsd/include/stdio.h (revision 433618cd)
18c1325ffSbostic /*-
28c1325ffSbostic  * Copyright (c) 1990 The Regents of the University of California.
38c1325ffSbostic  * 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*433618cdSdonn  *	@(#)stdio.h	5.14 (Berkeley) 03/05/91
116db880efSdist  */
126db880efSdist 
138c1325ffSbostic #ifndef	_STDIO_H_
148c1325ffSbostic #define	_STDIO_H_
158c1325ffSbostic 
167bde068dSbostic #include <sys/cdefs.h>
178c1325ffSbostic 
188c1325ffSbostic #include <machine/types.h>
198c1325ffSbostic #ifdef	_SIZE_T_
208c1325ffSbostic typedef	_SIZE_T_	size_t;
218c1325ffSbostic #undef	_SIZE_T_
228c1325ffSbostic #endif
238c1325ffSbostic 
24a8c24cc3Sbostic #ifndef NULL
25a8c24cc3Sbostic #define	NULL	0
26a8c24cc3Sbostic #endif
27a8c24cc3Sbostic 
288c1325ffSbostic typedef long fpos_t;		/* Must match off_t <sys/types.h> */
295fb3de76Stoy 
308c1325ffSbostic #define	_FSTDIO			/* Define for new stdio with functions. */
315fb3de76Stoy 
327bde068dSbostic /*
337bde068dSbostic  * NB: to fit things in six character monocase externals, the stdio
347bde068dSbostic  * code uses the prefix `__s' for stdio objects, typically followed
357bde068dSbostic  * by a three-character attempt at a mnemonic.
367bde068dSbostic  */
377bde068dSbostic 
388c1325ffSbostic /* stdio buffers */
398c1325ffSbostic struct __sbuf {
408c1325ffSbostic 	unsigned char *_base;
418c1325ffSbostic 	int	_size;
428c1325ffSbostic };
435fb3de76Stoy 
448c1325ffSbostic /*
458c1325ffSbostic  * stdio state variables.
468c1325ffSbostic  *
478c1325ffSbostic  * The following always hold:
488c1325ffSbostic  *
498c1325ffSbostic  *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
508c1325ffSbostic  *		_lbfsize is -_bf._size, else _lbfsize is 0
518c1325ffSbostic  *	if _flags&__SRD, _w is 0
528c1325ffSbostic  *	if _flags&__SWR, _r is 0
538c1325ffSbostic  *
548c1325ffSbostic  * This ensures that the getc and putc macros (or inline functions) never
558c1325ffSbostic  * try to write or read from a file that is in `read' or `write' mode.
568c1325ffSbostic  * (Moreover, they can, and do, automatically switch from read mode to
578c1325ffSbostic  * write mode, and back, on "r+" and "w+" files.)
588c1325ffSbostic  *
598c1325ffSbostic  * _lbfsize is used only to make the inline line-buffered output stream
608c1325ffSbostic  * code as compact as possible.
618c1325ffSbostic  *
628c1325ffSbostic  * _ub, _up, and _ur are used when ungetc() pushes back more characters
638c1325ffSbostic  * than fit in the current _bf, or when ungetc() pushes back a character
648c1325ffSbostic  * that does not match the previous one in _bf.  When this happens,
658c1325ffSbostic  * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
668c1325ffSbostic  * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
678c1325ffSbostic  */
688c1325ffSbostic typedef	struct __sFILE {
698c1325ffSbostic 	unsigned char *_p;	/* current position in (some) buffer */
708c1325ffSbostic 	int	_r;		/* read space left for getc() */
718c1325ffSbostic 	int	_w;		/* write space left for putc() */
728c1325ffSbostic 	short	_flags;		/* flags, below; this FILE is free if 0 */
738c1325ffSbostic 	short	_file;		/* fileno, if Unix descriptor, else -1 */
748c1325ffSbostic 	struct	__sbuf _bf;	/* the buffer (at least 1 byte, if !NULL) */
758c1325ffSbostic 	int	_lbfsize;	/* 0 or -_bf._size, for inline putc */
768c1325ffSbostic 
778c1325ffSbostic 	/* operations */
788c1325ffSbostic 	void	*_cookie;	/* cookie passed to io functions */
79e505d896Sdonn 	int	(*_close) __P((void *));
80e505d896Sdonn 	int	(*_read)  __P((void *, char *, int));
81e505d896Sdonn 	fpos_t	(*_seek)  __P((void *, fpos_t, int));
82e505d896Sdonn 	int	(*_write) __P((void *, const char *, int));
83cbae850eSkfall 
848c1325ffSbostic 	/* separate buffer for long sequences of ungetc() */
858c1325ffSbostic 	struct	__sbuf _ub;	/* ungetc buffer */
868c1325ffSbostic 	unsigned char *_up;	/* saved _p when _p is doing ungetc data */
878c1325ffSbostic 	int	_ur;		/* saved _r when _r is counting ungetc data */
888c1325ffSbostic 
898c1325ffSbostic 	/* tricks to meet minimum requirements even when malloc() fails */
908c1325ffSbostic 	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
918c1325ffSbostic 	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
928c1325ffSbostic 
938c1325ffSbostic 	/* separate buffer for fgetline() when line crosses buffer boundary */
948c1325ffSbostic 	struct	__sbuf _lb;	/* buffer for fgetline() */
958c1325ffSbostic 
968c1325ffSbostic 	/* Unix stdio files get aligned to block boundaries on fseek() */
978c1325ffSbostic 	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
988c1325ffSbostic 	int	_offset;	/* current lseek offset */
998c1325ffSbostic } FILE;
1008c1325ffSbostic 
1014b0a96f3Sdonn __BEGIN_DECLS
1028c1325ffSbostic extern FILE __sF[];
1034b0a96f3Sdonn __END_DECLS
1048c1325ffSbostic 
1058c1325ffSbostic #define	__SLBF	0x0001		/* line buffered */
1068c1325ffSbostic #define	__SNBF	0x0002		/* unbuffered */
1078c1325ffSbostic #define	__SRD	0x0004		/* OK to read */
1088c1325ffSbostic #define	__SWR	0x0008		/* OK to write */
1098c1325ffSbostic 	/* RD and WR are never simultaneously asserted */
1108c1325ffSbostic #define	__SRW	0x0010		/* open for reading & writing */
1118c1325ffSbostic #define	__SEOF	0x0020		/* found EOF */
1128c1325ffSbostic #define	__SERR	0x0040		/* found error */
1138c1325ffSbostic #define	__SMBF	0x0080		/* _buf is from malloc */
1148c1325ffSbostic #define	__SAPP	0x0100		/* fdopen()ed in append mode */
1158c1325ffSbostic #define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
1168c1325ffSbostic #define	__SOPT	0x0400		/* do fseek() optimisation */
1178c1325ffSbostic #define	__SNPT	0x0800		/* do not do fseek() optimisation */
1188c1325ffSbostic #define	__SOFF	0x1000		/* set iff _offset is in fact correct */
1198c1325ffSbostic #define	__SMOD	0x2000		/* true => fgetline modified _p text */
1208c1325ffSbostic 
1218c1325ffSbostic /*
1228c1325ffSbostic  * The following three definitions are for ANSI C, which took them
1238c1325ffSbostic  * from System V, which brilliantly took internal interface macros and
1248c1325ffSbostic  * made them official arguments to setvbuf(), without renaming them.
1258c1325ffSbostic  * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
1268c1325ffSbostic  *
1278c1325ffSbostic  * Although numbered as their counterparts above, the implementation
1288c1325ffSbostic  * does not rely on this.
1298c1325ffSbostic  */
1308c1325ffSbostic #define	_IOFBF	0		/* setvbuf should set fully buffered */
1318c1325ffSbostic #define	_IOLBF	1		/* setvbuf should set line buffered */
1328c1325ffSbostic #define	_IONBF	2		/* setvbuf should set unbuffered */
1338c1325ffSbostic 
1348c1325ffSbostic #define	BUFSIZ	1024		/* size of buffer used by setbuf */
1358c1325ffSbostic #define	EOF	(-1)
1368c1325ffSbostic 
1378c1325ffSbostic /*
1388c1325ffSbostic  * FOPEN_MAX is a minimum maximum, and should be the number of descriptors
1398c1325ffSbostic  * that the kernel can provide without allocation of a resource that can
1408c1325ffSbostic  * fail without the process sleeping.  Do not use this for anything.
1418c1325ffSbostic  */
1428c1325ffSbostic #define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
1438c1325ffSbostic #define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
1448c1325ffSbostic 
1458c1325ffSbostic /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
1468c1325ffSbostic #ifndef _ANSI_SOURCE
147e57e7607Sbostic #define	P_tmpdir	"/var/tmp/"
1488c1325ffSbostic #endif
1498c1325ffSbostic #define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
1508c1325ffSbostic #define	TMP_MAX		308915776
1518c1325ffSbostic 
1528c1325ffSbostic #ifndef SEEK_SET
1538c1325ffSbostic #define	SEEK_SET	0	/* set file offset to offset */
1548c1325ffSbostic #endif
1558c1325ffSbostic #ifndef SEEK_CUR
1568c1325ffSbostic #define	SEEK_CUR	1	/* set file offset to current plus offset */
1578c1325ffSbostic #endif
1588c1325ffSbostic #ifndef SEEK_END
1598c1325ffSbostic #define	SEEK_END	2	/* set file offset to EOF plus offset */
1608c1325ffSbostic #endif
1618c1325ffSbostic 
1628c1325ffSbostic #define	stdin	(&__sF[0])
1638c1325ffSbostic #define	stdout	(&__sF[1])
1648c1325ffSbostic #define	stderr	(&__sF[2])
1658c1325ffSbostic 
1668c1325ffSbostic /*
1678c1325ffSbostic  * Functions defined in ANSI C standard.
1688c1325ffSbostic  */
1697bde068dSbostic __BEGIN_DECLS
1707bde068dSbostic void	 clearerr __P((FILE *));
1717bde068dSbostic int	 fclose __P((FILE *));
1727bde068dSbostic int	 feof __P((FILE *));
1737bde068dSbostic int	 ferror __P((FILE *));
1747bde068dSbostic int	 fflush __P((FILE *));
1757bde068dSbostic int	 fgetc __P((FILE *));
1767bde068dSbostic int	 fgetpos __P((FILE *, fpos_t *));
1777bde068dSbostic char	*fgets __P((char *, size_t, FILE *));
178e505d896Sdonn FILE	*fopen __P((const char *, const char *));
1797bde068dSbostic int	 fprintf __P((FILE *, const char *, ...));
1807bde068dSbostic int	 fputc __P((int, FILE *));
1817bde068dSbostic int	 fputs __P((const char *, FILE *));
182e505d896Sdonn int	 fread __P((void *, size_t, size_t, FILE *));
183e505d896Sdonn FILE	*freopen __P((const char *, const char *, FILE *));
1847bde068dSbostic int	 fscanf __P((FILE *, const char *, ...));
1857bde068dSbostic int	 fseek __P((FILE *, long, int));
1867bde068dSbostic int	 fsetpos __P((FILE *, const fpos_t *));
1877bde068dSbostic long	 ftell __P((const FILE *));
188e505d896Sdonn int	 fwrite __P((const void *, size_t, size_t, FILE *));
1897bde068dSbostic int	 getc __P((FILE *));
1907bde068dSbostic int	 getchar __P((void));
1917bde068dSbostic char	*gets __P((char *));
1927bde068dSbostic void	 perror __P((const char *));
1937bde068dSbostic int	 printf __P((const char *, ...));
1947bde068dSbostic int	 putc __P((int, FILE *));
1957bde068dSbostic int	 putchar __P((int));
1967bde068dSbostic int	 puts __P((const char *));
1977bde068dSbostic int	 remove __P((const char *));
198e505d896Sdonn int	 rename  __P((const char *, const char *));
1997bde068dSbostic void	 rewind __P((FILE *));
2007bde068dSbostic int	 scanf __P((const char *, ...));
2017bde068dSbostic void	 setbuf __P((FILE *, char *));
2027bde068dSbostic int	 setvbuf __P((FILE *, char *, int, size_t));
2037bde068dSbostic int	 sprintf __P((char *, const char *, ...));
2047bde068dSbostic int	 sscanf __P((char *, const char *, ...));
2057bde068dSbostic FILE	*tmpfile __P((void));
2067bde068dSbostic char	*tmpnam __P((char *));
2077bde068dSbostic int	 ungetc __P((int, FILE *));
2087bde068dSbostic int	 vfprintf __P((FILE *, const char *, _VA_LIST_));
2097bde068dSbostic int	 vprintf __P((const char *, _VA_LIST_));
2107bde068dSbostic int	 vsprintf __P((char *, const char *, _VA_LIST_));
2117bde068dSbostic __END_DECLS
2128c1325ffSbostic 
2138c1325ffSbostic /*
2148c1325ffSbostic  * Functions defined in POSIX 1003.1.
2158c1325ffSbostic  */
2168c1325ffSbostic #ifndef _ANSI_SOURCE
2178c1325ffSbostic #define	L_cuserid	9	/* size for cuserid(); UT_NAMESIZE + 1 */
2188c1325ffSbostic #define	L_ctermid	1024	/* size for ctermid(); PATH_MAX */
2198c1325ffSbostic 
2207bde068dSbostic __BEGIN_DECLS
2210db5e65cSbostic char	*ctermid __P((char *));
2227bde068dSbostic FILE	*fdopen __P((int, const char *));
2237bde068dSbostic int	 fileno __P((FILE *));
2247bde068dSbostic __END_DECLS
225e505d896Sdonn #endif /* not ANSI */
2268c1325ffSbostic 
2278c1325ffSbostic /*
2288c1325ffSbostic  * Routines that are purely local.
2298c1325ffSbostic  */
230e505d896Sdonn #if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
2317bde068dSbostic __BEGIN_DECLS
2327bde068dSbostic char	*fgetline __P((FILE *, size_t *));
2337bde068dSbostic int	 fpurge __P((FILE *));
2347bde068dSbostic int	 getw __P((FILE *));
2357bde068dSbostic int	 pclose __P((FILE *));
236e505d896Sdonn FILE	*popen __P((const char *, const char *));
2377bde068dSbostic int	 putw __P((int, FILE *));
2387bde068dSbostic void	 setbuffer __P((FILE *, char *, int));
2397bde068dSbostic int	 setlinebuf __P((FILE *));
240e57e7607Sbostic char	*tempnam __P((const char *, const char *));
2417bde068dSbostic int	 snprintf __P((char *, size_t, const char *, ...));
2427bde068dSbostic int	 vsnprintf __P((char *, size_t, const char *, _VA_LIST_));
2437bde068dSbostic __END_DECLS
2448c1325ffSbostic 
2458c1325ffSbostic /*
246*433618cdSdonn  * This is a #define because the function is used internally and
247*433618cdSdonn  * (unlike vfscanf) the name __svfscanf is guaranteed not to collide
248*433618cdSdonn  * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined.
249*433618cdSdonn  */
250*433618cdSdonn #define	 vfscanf	__svfscanf
251*433618cdSdonn 
252*433618cdSdonn /*
2538c1325ffSbostic  * Stdio function-access interface.
2548c1325ffSbostic  */
2557bde068dSbostic __BEGIN_DECLS
256e505d896Sdonn FILE	*funopen __P((const void *,
257e505d896Sdonn 		int (*)(void *, char *, int),
258e505d896Sdonn 		int (*)(void *, const char *, int),
259e505d896Sdonn 		fpos_t (*)(void *, fpos_t, int),
260e505d896Sdonn 		int (*)(void *)));
2617bde068dSbostic __END_DECLS
2628c1325ffSbostic #define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
2638c1325ffSbostic #define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
2644b0a96f3Sdonn #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
2658c1325ffSbostic 
2668c1325ffSbostic /*
2678c1325ffSbostic  * Functions internal to the implementation.
2688c1325ffSbostic  */
2694b0a96f3Sdonn __BEGIN_DECLS
2707bde068dSbostic int	__srget __P((FILE *));
271*433618cdSdonn int	__svfscanf __P((FILE *, const char *, _VA_LIST_));
2727bde068dSbostic int	__swbuf __P((int, FILE *));
2734b0a96f3Sdonn __END_DECLS
2748c1325ffSbostic 
2758c1325ffSbostic /*
2768c1325ffSbostic  * The __sfoo macros are here so that we can
2778c1325ffSbostic  * define function versions in the C library.
2788c1325ffSbostic  */
2798c1325ffSbostic #define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
280*433618cdSdonn #if defined(__GNUC__) && defined(__STDC__)
281*433618cdSdonn static inline int __sputc(int _c, FILE *_p) {
2828c1325ffSbostic 	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
2838c1325ffSbostic 		return (*_p->_p++ = _c);
2848c1325ffSbostic 	else
2858c1325ffSbostic 		return (__swbuf(_c, _p));
2868c1325ffSbostic }
2878c1325ffSbostic #else
2888c1325ffSbostic /*
2898c1325ffSbostic  * This has been tuned to generate reasonable code on the vax using pcc.
2908c1325ffSbostic  */
2918c1325ffSbostic #define	__sputc(c, p) \
2928c1325ffSbostic 	(--(p)->_w < 0 ? \
2938c1325ffSbostic 		(p)->_w >= (p)->_lbfsize ? \
2948c1325ffSbostic 			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
2958c1325ffSbostic 				(int)*(p)->_p++ : \
2968c1325ffSbostic 				__swbuf('\n', p) : \
2978c1325ffSbostic 			__swbuf((int)(c), p) : \
2988c1325ffSbostic 		(*(p)->_p = (c), (int)*(p)->_p++))
2998c1325ffSbostic #endif
3008c1325ffSbostic 
3018c1325ffSbostic #define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
3028c1325ffSbostic #define	__sferror(p)	(((p)->_flags & __SERR) != 0)
3038c1325ffSbostic #define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
3048c1325ffSbostic #define	__sfileno(p)	((p)->_file)
3058c1325ffSbostic 
3068c1325ffSbostic #define	feof(p)		__sfeof(p)
3078c1325ffSbostic #define	ferror(p)	__sferror(p)
3088c1325ffSbostic #define	clearerr(p)	__sclearerr(p)
3098c1325ffSbostic 
3108c1325ffSbostic #ifndef _ANSI_SOURCE
3118c1325ffSbostic #define	fileno(p)	__sfileno(p)
3128c1325ffSbostic #endif
3138c1325ffSbostic 
3148c1325ffSbostic #ifndef lint
3158c1325ffSbostic #define	getc(fp)	__sgetc(fp)
3168c1325ffSbostic #define putc(x, fp)	__sputc(x, fp)
3178c1325ffSbostic #endif /* lint */
3188c1325ffSbostic 
3198c1325ffSbostic #define	getchar()	getc(stdin)
3208c1325ffSbostic #define	putchar(x)	putc(x, stdout)
3218c1325ffSbostic #endif /* _STDIO_H_ */
322