stdio.h revision 72472
1191668Sjamie/*-
2191668Sjamie * Copyright (c) 1990, 1993
3234712Sjamie *	The Regents of the University of California.  All rights reserved.
4191668Sjamie *
5191668Sjamie * This code is derived from software contributed to Berkeley by
6191668Sjamie * Chris Torek.
7191668Sjamie *
8191668Sjamie * Redistribution and use in source and binary forms, with or without
9191668Sjamie * modification, are permitted provided that the following conditions
10191668Sjamie * are met:
11191668Sjamie * 1. Redistributions of source code must retain the above copyright
12191668Sjamie *    notice, this list of conditions and the following disclaimer.
13191668Sjamie * 2. Redistributions in binary form must reproduce the above copyright
14191668Sjamie *    notice, this list of conditions and the following disclaimer in the
15191668Sjamie *    documentation and/or other materials provided with the distribution.
16191668Sjamie * 3. All advertising materials mentioning features or use of this software
17191668Sjamie *    must display the following acknowledgement:
18191668Sjamie *	This product includes software developed by the University of
19191668Sjamie *	California, Berkeley and its contributors.
20191668Sjamie * 4. Neither the name of the University nor the names of its contributors
21191668Sjamie *    may be used to endorse or promote products derived from this software
22191668Sjamie *    without specific prior written permission.
23191668Sjamie *
24191668Sjamie * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25191668Sjamie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2646432Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2746432Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28117280Scharnier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29117280Scharnier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30117280Scharnier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31234712Sjamie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32234712Sjamie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33185435Sbz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34158428Smatteo * SUCH DAMAGE.
3578723Sdd *
36192896Sjamie *	@(#)stdio.h	8.5 (Berkeley) 4/29/95
3746155Sphk * $FreeBSD: head/include/stdio.h 72472 2001-02-14 05:00:20Z peter $
3846155Sphk */
3978723Sdd
40129848Smaxim#ifndef	_STDIO_H_
41234712Sjamie#define	_STDIO_H_
4278723Sdd
4378723Sdd#include <sys/cdefs.h>
4478723Sdd#include <machine/ansi.h>
4578723Sdd
4678723Sdd#ifdef	_BSD_SIZE_T_
47234712Sjamietypedef	_BSD_SIZE_T_	size_t;
48192896Sjamie#undef	_BSD_SIZE_T_
49234712Sjamie#endif
50185435Sbz
51234712Sjamie#ifndef NULL
52234712Sjamie#define	NULL	0
53234712Sjamie#endif
54234712Sjamie
55234712Sjamietypedef	_BSD_OFF_T_	fpos_t;
56234712Sjamie
57234712Sjamie#define	_FSTDIO			/* Define for new stdio with functions. */
58236198Sjamie
59234712Sjamie/*
60234712Sjamie * NB: to fit things in six character monocase externals, the stdio
61234712Sjamie * code uses the prefix `__s' for stdio objects, typically followed
62234712Sjamie * by a three-character attempt at a mnemonic.
63234712Sjamie */
64234712Sjamie
65234712Sjamie/* stdio buffers */
66234712Sjamiestruct __sbuf {
67234712Sjamie	unsigned char *_base;
68234712Sjamie	int	_size;
69234712Sjamie};
70234712Sjamie
71234712Sjamiestruct __file_lock;
72192896Sjamie
73192896Sjamie/*
74185435Sbz * stdio state variables.
75234712Sjamie *
76234712Sjamie * The following always hold:
77234712Sjamie *
78234712Sjamie *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
79234712Sjamie *		_lbfsize is -_bf._size, else _lbfsize is 0
80234712Sjamie *	if _flags&__SRD, _w is 0
81234712Sjamie *	if _flags&__SWR, _r is 0
82193929Sjamie *
83193929Sjamie * This ensures that the getc and putc macros (or inline functions) never
84234712Sjamie * try to write or read from a file that is in `read' or `write' mode.
85234988Sjamie * (Moreover, they can, and do, automatically switch from read mode to
86234712Sjamie * write mode, and back, on "r+" and "w+" files.)
87234712Sjamie *
88234712Sjamie * _lbfsize is used only to make the inline line-buffered output stream
89234712Sjamie * code as compact as possible.
90234712Sjamie *
91234712Sjamie * _ub, _up, and _ur are used when ungetc() pushes back more characters
92234712Sjamie * than fit in the current _bf, or when ungetc() pushes back a character
93234712Sjamie * that does not match the previous one in _bf.  When this happens,
94234712Sjamie * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
95256387Shrs * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
96278484Sjamie *
97234712Sjamie * NB: see WARNING above before changing the layout of this structure!
98234712Sjamie */
99234712Sjamietypedef	struct __sFILE {
100234712Sjamie	unsigned char *_p;	/* current position in (some) buffer */
101234712Sjamie	int	_r;		/* read space left for getc() */
102234712Sjamie	int	_w;		/* write space left for putc() */
103234988Sjamie	short	_flags;		/* flags, below; this FILE is free if 0 */
104234712Sjamie	short	_file;		/* fileno, if Unix descriptor, else -1 */
105192896Sjamie	struct	__sbuf _bf;	/* the buffer (at least 1 byte, if !NULL) */
106234712Sjamie	int	_lbfsize;	/* 0 or -_bf._size, for inline putc */
107234988Sjamie
108234712Sjamie	/* operations */
109234712Sjamie	void	*_cookie;	/* cookie passed to io functions */
110234712Sjamie	int	(*_close) __P((void *));
111234712Sjamie	int	(*_read)  __P((void *, char *, int));
112234712Sjamie	fpos_t	(*_seek)  __P((void *, fpos_t, int));
113278484Sjamie	int	(*_write) __P((void *, const char *, int));
114256387Shrs
115234712Sjamie	/* separate buffer for long sequences of ungetc() */
116234712Sjamie	struct	__sbuf _ub;	/* ungetc buffer */
117234712Sjamie	unsigned char *_up;	/* saved _p when _p is doing ungetc data */
118234712Sjamie	int	_ur;		/* saved _r when _r is counting ungetc data */
119234712Sjamie
120234712Sjamie	/* tricks to meet minimum requirements even when malloc() fails */
121234712Sjamie	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
122234712Sjamie	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
123234712Sjamie
124234988Sjamie	/* separate buffer for fgetln() when line crosses buffer boundary */
125234712Sjamie	struct	__sbuf _lb;	/* buffer for fgetln() */
126129848Smaxim
12746155Sphk	/* Unix stdio files get aligned to block boundaries on fseek() */
12846155Sphk	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
12946155Sphk	fpos_t	_offset;	/* current lseek offset (see WARNING) */
130234712Sjamie	struct __file_lock *_lock;	/* used for MT-safety */
131234712Sjamie} FILE;
132234712Sjamie
133234712Sjamie__BEGIN_DECLS
134193929Sjamieextern FILE __stdin;
135234712Sjamieextern FILE __stdout;
136234712Sjamieextern FILE __stderr;
137236198Sjamie__END_DECLS
138194869Sjamie
139234712Sjamie#define	__SLBF	0x0001		/* line buffered */
140234712Sjamie#define	__SNBF	0x0002		/* unbuffered */
141234712Sjamie#define	__SRD	0x0004		/* OK to read */
142234712Sjamie#define	__SWR	0x0008		/* OK to write */
143234712Sjamie	/* RD and WR are never simultaneously asserted */
144234712Sjamie#define	__SRW	0x0010		/* open for reading & writing */
14546155Sphk#define	__SEOF	0x0020		/* found EOF */
146234712Sjamie#define	__SERR	0x0040		/* found error */
147236198Sjamie#define	__SMBF	0x0080		/* _buf is from malloc */
148234712Sjamie#define	__SAPP	0x0100		/* fdopen()ed in append mode */
149234712Sjamie#define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
150234712Sjamie#define	__SOPT	0x0400		/* do fseek() optimization */
151112705Smaxim#define	__SNPT	0x0800		/* do not do fseek() optimization */
152237697Smaxim#define	__SOFF	0x1000		/* set iff _offset is in fact correct */
153112705Smaxim#define	__SMOD	0x2000		/* true => fgetln modified _p text */
154234712Sjamie#define	__SALC	0x4000		/* allocate string space dynamically */
155234712Sjamie#define	__SIGN	0x8000		/* ignore this file in _fwalk */
156234712Sjamie
157192896Sjamie/*
158234712Sjamie * The following three definitions are for ANSI C, which took them
159192896Sjamie * from System V, which brilliantly took internal interface macros and
160234712Sjamie * made them official arguments to setvbuf(), without renaming them.
161234712Sjamie * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
162234712Sjamie *
163185435Sbz * Although numbered as their counterparts above, the implementation
164234712Sjamie * does not rely on this.
165234712Sjamie */
166234712Sjamie#define	_IOFBF	0		/* setvbuf should set fully buffered */
167234712Sjamie#define	_IOLBF	1		/* setvbuf should set line buffered */
168185435Sbz#define	_IONBF	2		/* setvbuf should set unbuffered */
169113277Smike
170113277Smike#define	BUFSIZ	1024		/* size of buffer used by setbuf */
171234712Sjamie#define	EOF	(-1)
172113277Smike
173153056Sphilip/*
174153056Sphilip * FOPEN_MAX is a minimum maximum, and is the number of streams that
175153056Sphilip * stdio can provide without attempting to allocate further resources
176234712Sjamie * (which could fail).  Do not use this for anything.
177234712Sjamie */
178234712Sjamie				/* must be == _POSIX_STREAM_MAX <limits.h> */
179234712Sjamie#define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
180234712Sjamie#define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
181234712Sjamie
182234712Sjamie/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
183185435Sbz#ifndef _ANSI_SOURCE
184234712Sjamie#define	P_tmpdir	"/var/tmp/"
185234712Sjamie#endif
186185435Sbz#define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
187234712Sjamie#define	TMP_MAX		308915776
188234712Sjamie
189234712Sjamie#ifndef SEEK_SET
190234712Sjamie#define	SEEK_SET	0	/* set file offset to offset */
191234712Sjamie#endif
192234712Sjamie#ifndef SEEK_CUR
193234712Sjamie#define	SEEK_CUR	1	/* set file offset to current plus offset */
194234712Sjamie#endif
195234712Sjamie#ifndef SEEK_END
196234712Sjamie#define	SEEK_END	2	/* set file offset to EOF plus offset */
197234712Sjamie#endif
198234712Sjamie
199234712Sjamie#define	stdin	(&__stdin)
200234712Sjamie#define	stdout	(&__stdout)
201234712Sjamie#define	stderr	(&__stderr)
202158428Smatteo
203234712Sjamie/*
204234712Sjamie * Functions defined in ANSI C standard.
205158428Smatteo */
206112705Smaxim__BEGIN_DECLS
207234712Sjamievoid	 clearerr __P((FILE *));
208234712Sjamieint	 fclose __P((FILE *));
209234712Sjamieint	 feof __P((FILE *));
210112705Smaximint	 ferror __P((FILE *));
211129848Smaximint	 fflush __P((FILE *));
212234712Sjamieint	 fgetc __P((FILE *));
213234712Sjamieint	 fgetpos __P((FILE *, fpos_t *));
214234712Sjamiechar	*fgets __P((char *, int, FILE *));
215234712SjamieFILE	*fopen __P((const char *, const char *));
216129848Smaximint	 fprintf __P((FILE *, const char *, ...));
217234712Sjamieint	 fputc __P((int, FILE *));
218234712Sjamieint	 fputs __P((const char *, FILE *));
219133743Smaximsize_t	 fread __P((void *, size_t, size_t, FILE *));
220112705SmaximFILE	*freopen __P((const char *, const char *, FILE *));
221112705Smaximint	 fscanf __P((FILE *, const char *, ...));
222112705Smaximint	 fseek __P((FILE *, long, int));
223113277Smikeint	 fsetpos __P((FILE *, const fpos_t *));
224112705Smaximlong	 ftell __P((FILE *));
225112705Smaximsize_t	 fwrite __P((const void *, size_t, size_t, FILE *));
226234712Sjamieint	 getc __P((FILE *));
227234712Sjamieint	 getchar __P((void));
228234712Sjamiechar	*gets __P((char *));
229234712Sjamie#if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
230234712Sjamieextern __const int sys_nerr;		/* perror(3) external variables */
231234712Sjamieextern __const char *__const sys_errlist[];
232192896Sjamie#endif
233234712Sjamievoid	 perror __P((const char *));
234234712Sjamieint	 printf __P((const char *, ...));
235234712Sjamieint	 putc __P((int, FILE *));
236234712Sjamieint	 putchar __P((int));
237234712Sjamieint	 puts __P((const char *));
238234712Sjamieint	 remove __P((const char *));
239234712Sjamieint	 rename  __P((const char *, const char *));
240234712Sjamievoid	 rewind __P((FILE *));
241234712Sjamieint	 scanf __P((const char *, ...));
242234712Sjamievoid	 setbuf __P((FILE *, char *));
243234712Sjamieint	 setvbuf __P((FILE *, char *, int, size_t));
244234712Sjamieint	 sprintf __P((char *, const char *, ...));
245234712Sjamieint	 sscanf __P((const char *, const char *, ...));
246234712SjamieFILE	*tmpfile __P((void));
247234712Sjamiechar	*tmpnam __P((char *));
248234712Sjamieint	 ungetc __P((int, FILE *));
249234712Sjamieint	 vfprintf __P((FILE *, const char *, _BSD_VA_LIST_));
250234712Sjamieint	 vprintf __P((const char *, _BSD_VA_LIST_));
251234712Sjamieint	 vsprintf __P((char *, const char *, _BSD_VA_LIST_));
252222465Sbz__END_DECLS
253234712Sjamie
254234712Sjamie/*
255192896Sjamie * Functions defined in POSIX 1003.1.
256192896Sjamie */
257192896Sjamie#ifndef _ANSI_SOURCE
258192896Sjamie/* size for cuserid(3); UT_NAMESIZE + 1, see <utmp.h> */
259234712Sjamie#define	L_cuserid	17
260234712Sjamie
261234712Sjamie#define	L_ctermid	1024	/* size for ctermid(3); PATH_MAX */
262193929Sjamie
263193929Sjamie__BEGIN_DECLS
264193929Sjamiechar	*ctermid __P((char *));
265193929SjamieFILE	*fdopen __P((int, const char *));
266193929Sjamieint	 fileno __P((FILE *));
267193929Sjamieint	 ftrylockfile __P((FILE *));
268234712Sjamievoid	 flockfile __P((FILE *));
269193929Sjamievoid	 funlockfile __P((FILE *));
270234712Sjamie__END_DECLS
271234712Sjamie#endif /* not ANSI */
272234712Sjamie
273234712Sjamie/*
274234712Sjamie * Portability hacks.  See <sys/types.h>.
275193929Sjamie */
276193929Sjamie#if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
277193929Sjamie__BEGIN_DECLS
278193929Sjamie#ifndef _FTRUNCATE_DECLARED
279193929Sjamie#define	_FTRUNCATE_DECLARED
280193929Sjamieint	 ftruncate __P((int, _BSD_OFF_T_));
281234712Sjamie#endif
282234712Sjamie#ifndef _LSEEK_DECLARED
283193929Sjamie#define	_LSEEK_DECLARED
284193929Sjamie_BSD_OFF_T_ lseek __P((int, _BSD_OFF_T_, int));
285234712Sjamie#endif
286234712Sjamie#ifndef _MMAP_DECLARED
287234712Sjamie#define	_MMAP_DECLARED
288234712Sjamievoid	*mmap __P((void *, size_t, int, int, int, _BSD_OFF_T_));
289234712Sjamie#endif
290234712Sjamie#ifndef _TRUNCATE_DECLARED
291234712Sjamie#define	_TRUNCATE_DECLARED
292234712Sjamieint	 truncate __P((const char *, _BSD_OFF_T_));
293234712Sjamie#endif
294234712Sjamie__END_DECLS
295234712Sjamie#endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
296234712Sjamie
297234712Sjamie/*
298234712Sjamie * Routines that are purely local.
299234712Sjamie */
300234712Sjamie#if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
301234712Sjamie__BEGIN_DECLS
302234712Sjamieint	 asprintf __P((char **, const char *, ...)) __printflike(2, 3);
303234712Sjamiechar	*ctermid_r __P((char *));
304234712Sjamiechar	*fgetln __P((FILE *, size_t *));
305234712Sjamieint	 fpurge __P((FILE *));
306234712Sjamieint	 fseeko __P((FILE *, _BSD_OFF_T_, int));
307234712Sjamie_BSD_OFF_T_ ftello __P((FILE *));
308234712Sjamieint	 getw __P((FILE *));
309234712Sjamieint	 pclose __P((FILE *));
310234712SjamieFILE	*popen __P((const char *, const char *));
311234712Sjamieint	 putw __P((int, FILE *));
312239602Sjamievoid	 setbuffer __P((FILE *, char *, int));
313239602Sjamieint	 setlinebuf __P((FILE *));
314239602Sjamiechar	*tempnam __P((const char *, const char *));
315239602Sjamieint	 snprintf __P((char *, size_t, const char *, ...)) __printflike(3, 4);
316239602Sjamieint	 vasprintf __P((char **, const char *, _BSD_VA_LIST_))
317239602Sjamie	    __printflike(2, 0);
318239602Sjamieint	 vsnprintf __P((char *, size_t, const char *, _BSD_VA_LIST_))
319239602Sjamie	    __printflike(3, 0);
320239602Sjamieint	 vscanf __P((const char *, _BSD_VA_LIST_)) __scanflike(1, 0);
321239602Sjamieint	 vsscanf __P((const char *, const char *, _BSD_VA_LIST_))
322239602Sjamie	    __scanflike(2, 0);
323239602Sjamie__END_DECLS
324239602Sjamie
325239602Sjamie/*
326239602Sjamie * This is a #define because the function is used internally and
327239602Sjamie * (unlike vfscanf) the name __vfscanf is guaranteed not to collide
328239602Sjamie * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined.
329239602Sjamie */
330239602Sjamie#define	 vfscanf	__vfscanf
331239602Sjamie
332239602Sjamie/*
333239602Sjamie * Stdio function-access interface.
334239602Sjamie */
335239602Sjamie__BEGIN_DECLS
336239602SjamieFILE	*funopen __P((const void *,
337239602Sjamie		int (*)(void *, char *, int),
338234712Sjamie		int (*)(void *, const char *, int),
339234712Sjamie		fpos_t (*)(void *, fpos_t, int),
340234712Sjamie		int (*)(void *)));
341234712Sjamie__END_DECLS
342234712Sjamie#define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
343234712Sjamie#define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
344185435Sbz#endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
345185435Sbz
346234712Sjamie/*
347234712Sjamie * Functions internal to the implementation.
348234712Sjamie */
349234712Sjamie__BEGIN_DECLS
350234712Sjamieint	__srget __P((FILE *));
351234712Sjamieint	__vfscanf __P((FILE *, const char *, _BSD_VA_LIST_));
352234712Sjamieint	__svfscanf __P((FILE *, const char *, _BSD_VA_LIST_));
353234712Sjamieint	__swbuf __P((int, FILE *));
354234712Sjamie__END_DECLS
355234712Sjamie
356153056Sphilip/*
357234712Sjamie * The __sfoo macros are here so that we can
358234712Sjamie * define function versions in the C library.
359234712Sjamie */
360234712Sjamie#define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
361234712Sjamie#if defined(__GNUC__) && defined(__STDC__)
362234712Sjamiestatic __inline int __sputc(int _c, FILE *_p) {
363234712Sjamie	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
364113804Smike		return (*_p->_p++ = _c);
365234712Sjamie	else
366234712Sjamie		return (__swbuf(_c, _p));
367234712Sjamie}
368234712Sjamie#else
369234712Sjamie/*
370234712Sjamie * This has been tuned to generate reasonable code on the vax using pcc.
371234712Sjamie */
372234712Sjamie#define	__sputc(c, p) \
373234712Sjamie	(--(p)->_w < 0 ? \
374234712Sjamie		(p)->_w >= (p)->_lbfsize ? \
375234712Sjamie			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
376234712Sjamie				(int)*(p)->_p++ : \
377234712Sjamie				__swbuf('\n', p) : \
378234712Sjamie			__swbuf((int)(c), p) : \
379234712Sjamie		(*(p)->_p = (c), (int)*(p)->_p++))
380234712Sjamie#endif
381234712Sjamie
382234712Sjamie#define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
383234712Sjamie#define	__sferror(p)	(((p)->_flags & __SERR) != 0)
384234712Sjamie#define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
385234712Sjamie#define	__sfileno(p)	((p)->_file)
386234712Sjamie
387234712Sjamie/*
388234712Sjamie * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
389234712Sjamie * B.8.2.7 for the rationale behind the *_unlocked() macros.
390234712Sjamie */
391234712Sjamie#define	feof_unlocked(p)	__sfeof(p)
392234712Sjamie#define	ferror_unlocked(p)	__sferror(p)
393234712Sjamie#define	clearerr_unlocked(p)	__sclearerr(p)
394234712Sjamie
395234712Sjamie#ifndef _ANSI_SOURCE
396234712Sjamie#define	fileno_unlocked(p)	__sfileno(p)
397234712Sjamie#endif
398234712Sjamie
399234712Sjamie#define	getc_unlocked(fp)	__sgetc(fp)
400234712Sjamie#define	putc_unlocked(x, fp)	__sputc(x, fp)
401234712Sjamie
402234712Sjamie#define	getchar_unlocked()	getc_unlocked(stdin)
403234712Sjamie#define	putchar_unlocked(x)	putc_unlocked(x, stdout)
404234712Sjamie
405234712Sjamie#endif /* !_STDIO_H_ */
406234712Sjamie