xref: /dragonfly/include/stdio.h (revision 851dc90d)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Torek.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)stdio.h	8.5 (Berkeley) 4/29/95
37  * $FreeBSD: src/include/stdio.h,v 1.24.2.5 2002/11/09 08:07:20 imp Exp $
38  * $DragonFly: src/include/stdio.h,v 1.3 2003/11/09 02:22:28 dillon Exp $
39  */
40 
41 #ifndef	_STDIO_H_
42 #define	_STDIO_H_
43 
44 #include <sys/cdefs.h>
45 #ifndef _SYS_STDINT_H_
46 #include <sys/stdint.h>
47 #endif
48 #ifndef _MACHINE_STDARG_H_
49 #include <machine/stdarg.h>
50 #endif
51 
52 #ifndef _SIZE_T_DECLARED_
53 #define _SIZE_T_DECLARED_
54 typedef __size_t	size_t;
55 #endif
56 
57 #ifndef NULL
58 #define	NULL	0
59 #endif
60 
61 typedef	__off_t	fpos_t;
62 
63 #define	_FSTDIO			/* Define for new stdio with functions. */
64 
65 /*
66  * NB: to fit things in six character monocase externals, the stdio
67  * code uses the prefix `__s' for stdio objects, typically followed
68  * by a three-character attempt at a mnemonic.
69  */
70 
71 /* stdio buffers */
72 struct __sbuf {
73 	unsigned char *_base;
74 	int	_size;
75 };
76 
77 /*
78  * stdio state variables.
79  *
80  * The following always hold:
81  *
82  *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
83  *		_lbfsize is -_bf._size, else _lbfsize is 0
84  *	if _flags&__SRD, _w is 0
85  *	if _flags&__SWR, _r is 0
86  *
87  * This ensures that the getc and putc macros (or inline functions) never
88  * try to write or read from a file that is in `read' or `write' mode.
89  * (Moreover, they can, and do, automatically switch from read mode to
90  * write mode, and back, on "r+" and "w+" files.)
91  *
92  * _lbfsize is used only to make the inline line-buffered output stream
93  * code as compact as possible.
94  *
95  * _ub, _up, and _ur are used when ungetc() pushes back more characters
96  * than fit in the current _bf, or when ungetc() pushes back a character
97  * that does not match the previous one in _bf.  When this happens,
98  * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
99  * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
100  *
101  * NB: see WARNING above before changing the layout of this structure!
102  */
103 typedef	struct __sFILE {
104 	unsigned char *_p;	/* current position in (some) buffer */
105 	int	_r;		/* read space left for getc() */
106 	int	_w;		/* write space left for putc() */
107 	short	_flags;		/* flags, below; this FILE is free if 0 */
108 	short	_file;		/* fileno, if Unix descriptor, else -1 */
109 	struct	__sbuf _bf;	/* the buffer (at least 1 byte, if !NULL) */
110 	int	_lbfsize;	/* 0 or -_bf._size, for inline putc */
111 
112 	/* operations */
113 	void	*_cookie;	/* cookie passed to io functions */
114 	int	(*_close) __P((void *));
115 	int	(*_read)  __P((void *, char *, int));
116 	fpos_t	(*_seek)  __P((void *, fpos_t, int));
117 	int	(*_write) __P((void *, const char *, int));
118 
119 	/* separate buffer for long sequences of ungetc() */
120 	struct	__sbuf _ub;	/* ungetc buffer */
121 	unsigned char *_up;	/* saved _p when _p is doing ungetc data */
122 	int	_ur;		/* saved _r when _r is counting ungetc data */
123 
124 	/* tricks to meet minimum requirements even when malloc() fails */
125 	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
126 	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
127 
128 	/* separate buffer for fgetln() when line crosses buffer boundary */
129 	struct	__sbuf _lb;	/* buffer for fgetln() */
130 
131 	/* Unix stdio files get aligned to block boundaries on fseek() */
132 	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
133 	fpos_t	_offset;	/* current lseek offset (see WARNING) */
134 } FILE;
135 
136 __BEGIN_DECLS
137 extern FILE *__stdinp, *__stdoutp, *__stderrp;
138 __END_DECLS
139 
140 #define	__SLBF	0x0001		/* line buffered */
141 #define	__SNBF	0x0002		/* unbuffered */
142 #define	__SRD	0x0004		/* OK to read */
143 #define	__SWR	0x0008		/* OK to write */
144 	/* RD and WR are never simultaneously asserted */
145 #define	__SRW	0x0010		/* open for reading & writing */
146 #define	__SEOF	0x0020		/* found EOF */
147 #define	__SERR	0x0040		/* found error */
148 #define	__SMBF	0x0080		/* _buf is from malloc */
149 #define	__SAPP	0x0100		/* fdopen()ed in append mode */
150 #define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
151 #define	__SOPT	0x0400		/* do fseek() optimization */
152 #define	__SNPT	0x0800		/* do not do fseek() optimization */
153 #define	__SOFF	0x1000		/* set iff _offset is in fact correct */
154 #define	__SMOD	0x2000		/* true => fgetln modified _p text */
155 #define	__SALC	0x4000		/* allocate string space dynamically */
156 
157 /*
158  * The following three definitions are for ANSI C, which took them
159  * from System V, which brilliantly took internal interface macros and
160  * made them official arguments to setvbuf(), without renaming them.
161  * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
162  *
163  * Although numbered as their counterparts above, the implementation
164  * does not rely on this.
165  */
166 #define	_IOFBF	0		/* setvbuf should set fully buffered */
167 #define	_IOLBF	1		/* setvbuf should set line buffered */
168 #define	_IONBF	2		/* setvbuf should set unbuffered */
169 
170 #define	BUFSIZ	1024		/* size of buffer used by setbuf */
171 #define	EOF	(-1)
172 
173 /*
174  * FOPEN_MAX is a minimum maximum, and is the number of streams that
175  * stdio can provide without attempting to allocate further resources
176  * (which could fail).  Do not use this for anything.
177  */
178 				/* must be == _POSIX_STREAM_MAX <limits.h> */
179 #define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
180 #define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
181 
182 /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
183 #ifndef _ANSI_SOURCE
184 #define	P_tmpdir	"/var/tmp/"
185 #endif
186 #define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
187 #define	TMP_MAX		308915776
188 
189 #ifndef SEEK_SET
190 #define	SEEK_SET	0	/* set file offset to offset */
191 #endif
192 #ifndef SEEK_CUR
193 #define	SEEK_CUR	1	/* set file offset to current plus offset */
194 #endif
195 #ifndef SEEK_END
196 #define	SEEK_END	2	/* set file offset to EOF plus offset */
197 #endif
198 
199 #define	stdin	(__stdinp)
200 #define	stdout	(__stdoutp)
201 #define	stderr	(__stderrp)
202 
203 /*
204  * Functions defined in ANSI C standard.
205  */
206 __BEGIN_DECLS
207 void	 clearerr __P((FILE *));
208 int	 fclose __P((FILE *));
209 int	 feof __P((FILE *));
210 int	 ferror __P((FILE *));
211 int	 fflush __P((FILE *));
212 int	 fgetc __P((FILE *));
213 int	 fgetpos __P((FILE *, fpos_t *));
214 char	*fgets __P((char *, int, FILE *));
215 FILE	*fopen __P((const char *, const char *));
216 int	 fprintf __P((FILE *, const char *, ...));
217 int	 fputc __P((int, FILE *));
218 int	 fputs __P((const char *, FILE *));
219 size_t fread __P((void *, size_t, size_t, FILE *));
220 FILE	*freopen __P((const char *, const char *, FILE *));
221 int	 fscanf __P((FILE *, const char *, ...));
222 int	 fseek __P((FILE *, long, int));
223 int	 fsetpos __P((FILE *, const fpos_t *));
224 long	 ftell __P((FILE *));
225 size_t fwrite __P((const void *, size_t, size_t, FILE *));
226 int	 getc __P((FILE *));
227 int	 getchar __P((void));
228 char	*gets __P((char *));
229 #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
230 extern __const int sys_nerr;		/* perror(3) external variables */
231 extern __const char *__const sys_errlist[];
232 #endif
233 void	 perror __P((const char *));
234 int	 printf __P((const char *, ...));
235 int	 putc __P((int, FILE *));
236 int	 putchar __P((int));
237 int	 puts __P((const char *));
238 int	 remove __P((const char *));
239 int	 rename  __P((const char *, const char *));
240 void	 rewind __P((FILE *));
241 int	 scanf __P((const char *, ...));
242 void	 setbuf __P((FILE *, char *));
243 int	 setvbuf __P((FILE *, char *, int, size_t));
244 int	 sprintf __P((char *, const char *, ...));
245 int	 sscanf __P((const char *, const char *, ...));
246 FILE	*tmpfile __P((void));
247 char	*tmpnam __P((char *));
248 int	 ungetc __P((int, FILE *));
249 int	 vfprintf __P((FILE *, const char *, __va_list));
250 int	 vprintf __P((const char *, __va_list));
251 int	 vsprintf __P((char *, const char *, __va_list));
252 __END_DECLS
253 
254 /*
255  * Functions defined in POSIX 1003.1.
256  */
257 #ifndef _ANSI_SOURCE
258 /* size for cuserid(3); UT_NAMESIZE + 1, see <utmp.h> */
259 #define	L_cuserid	17
260 
261 #define	L_ctermid	1024	/* size for ctermid(3); PATH_MAX */
262 
263 __BEGIN_DECLS
264 char	*ctermid __P((char *));
265 FILE	*fdopen __P((int, const char *));
266 int	 fileno __P((FILE *));
267 int	 ftrylockfile __P((FILE *));
268 void	 flockfile __P((FILE *));
269 void	 funlockfile __P((FILE *));
270 __END_DECLS
271 #endif /* not ANSI */
272 
273 /*
274  * Portability hacks.  See <sys/types.h>.
275  */
276 #if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
277 __BEGIN_DECLS
278 #ifndef _FTRUNCATE_DECLARED
279 #define	_FTRUNCATE_DECLARED
280 int	 ftruncate __P((int, __off_t));
281 #endif
282 #ifndef _LSEEK_DECLARED
283 #define	_LSEEK_DECLARED
284 __off_t lseek __P((int, __off_t, int));
285 #endif
286 #ifndef _MMAP_DECLARED
287 #define	_MMAP_DECLARED
288 void	*mmap __P((void *, size_t, int, int, int, __off_t));
289 #endif
290 #ifndef _TRUNCATE_DECLARED
291 #define	_TRUNCATE_DECLARED
292 int	 truncate __P((const char *, __off_t));
293 #endif
294 __END_DECLS
295 #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
296 
297 /*
298  * Routines that are purely local.
299  */
300 #if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
301 __BEGIN_DECLS
302 int	 asprintf __P((char **, const char *, ...)) __printflike(2, 3);
303 char	*ctermid_r __P((char *));
304 char	*fgetln __P((FILE *, size_t *));
305 #if __GNUC__ == 2 && __GNUC_MINOR__ >= 7 || __GNUC__ >= 3
306 #define	__ATTR_FORMAT_ARG	__attribute__((__format_arg__(2)))
307 #else
308 #define	__ATTR_FORMAT_ARG
309 #endif
310 __const char *fmtcheck __P((const char *, const char *)) __ATTR_FORMAT_ARG;
311 int	 fpurge __P((FILE *));
312 int	 fseeko __P((FILE *, __off_t, int));
313 __off_t ftello __P((FILE *));
314 int	 getw __P((FILE *));
315 int	 pclose __P((FILE *));
316 FILE	*popen __P((const char *, const char *));
317 int	 putw __P((int, FILE *));
318 void	 setbuffer __P((FILE *, char *, int));
319 int	 setlinebuf __P((FILE *));
320 char	*tempnam __P((const char *, const char *));
321 int	 snprintf __P((char *, size_t, const char *, ...)) __printflike(3, 4);
322 int	 vasprintf __P((char **, const char *, __va_list))
323 	    __printflike(2, 0);
324 int	 vsnprintf __P((char *, size_t, const char *, __va_list))
325 	    __printflike(3, 0);
326 int	 vscanf __P((const char *, __va_list)) __scanflike(1, 0);
327 int	 vsscanf __P((const char *, const char *, __va_list))
328 	    __scanflike(2, 0);
329 __END_DECLS
330 
331 /*
332  * This is a #define because the function is used internally and
333  * (unlike vfscanf) the name __svfscanf is guaranteed not to collide
334  * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined.
335  */
336 #define	 vfscanf	__svfscanf
337 
338 /*
339  * Stdio function-access interface.
340  */
341 __BEGIN_DECLS
342 FILE	*funopen __P((const void *,
343 		int (*)(void *, char *, int),
344 		int (*)(void *, const char *, int),
345 		fpos_t (*)(void *, fpos_t, int),
346 		int (*)(void *)));
347 __END_DECLS
348 #define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
349 #define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
350 #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
351 
352 /*
353  * Functions internal to the implementation.
354  */
355 __BEGIN_DECLS
356 int	__srget __P((FILE *));
357 int	__svfscanf __P((FILE *, const char *, __va_list));
358 int	__swbuf __P((int, FILE *));
359 __END_DECLS
360 
361 /*
362  * The __sfoo macros are here so that we can
363  * define function versions in the C library.
364  */
365 #define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
366 #if defined(__GNUC__) && defined(__STDC__)
367 static __inline int __sputc(int _c, FILE *_p) {
368 	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
369 		return (*_p->_p++ = _c);
370 	else
371 		return (__swbuf(_c, _p));
372 }
373 #else
374 /*
375  * This has been tuned to generate reasonable code on the vax using pcc.
376  */
377 #define	__sputc(c, p) \
378 	(--(p)->_w < 0 ? \
379 		(p)->_w >= (p)->_lbfsize ? \
380 			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
381 				(int)*(p)->_p++ : \
382 				__swbuf('\n', p) : \
383 			__swbuf((int)(c), p) : \
384 		(*(p)->_p = (c), (int)*(p)->_p++))
385 #endif
386 
387 #define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
388 #define	__sferror(p)	(((p)->_flags & __SERR) != 0)
389 #define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
390 #define	__sfileno(p)	((p)->_file)
391 
392 /*
393  * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
394  * B.8.2.7 for the rationale behind the *_unlocked() macros.
395  */
396 #define	feof_unlocked(p)	__sfeof(p)
397 #define	ferror_unlocked(p)	__sferror(p)
398 #define	clearerr_unlocked(p)	__sclearerr(p)
399 
400 #ifndef _ANSI_SOURCE
401 #define	fileno_unlocked(p)	__sfileno(p)
402 #endif
403 
404 #ifndef  _THREAD_SAFE
405 #define	feof(p)		feof_unlocked(p)
406 #define	ferror(p)	ferror_unlocked(p)
407 #define	clearerr(p)	clearerr_unlocked(p)
408 
409 #ifndef _ANSI_SOURCE
410 #define	fileno(p)	fileno_unlocked(p)
411 #endif
412 #endif
413 
414 #ifndef lint
415 #define	getc_unlocked(fp)	__sgetc(fp)
416 #define putc_unlocked(x, fp)	__sputc(x, fp)
417 #ifdef	_THREAD_SAFE
418 void	_flockfile_debug __P((FILE *, char *, int));
419 #ifdef	_FLOCK_DEBUG
420 #define _FLOCKFILE(x)	_flockfile_debug(x, __FILE__, __LINE__)
421 #else
422 #define _FLOCKFILE(x)	flockfile(x)
423 #endif
424 extern int __isthreaded;
425 static __inline int			\
426 __getc_locked(FILE *_fp)		\
427 {					\
428 	int _ret;			\
429 	if (__isthreaded)		\
430 		_FLOCKFILE(_fp);	\
431 	_ret = getc_unlocked(_fp);	\
432 	if (__isthreaded)		\
433 		funlockfile(_fp);	\
434 	return (_ret);			\
435 }
436 static __inline int			\
437 __putc_locked(int _x, FILE *_fp)	\
438 {					\
439 	int _ret;			\
440 	if (__isthreaded)		\
441 		_FLOCKFILE(_fp);	\
442 	_ret = putc_unlocked(_x, _fp);	\
443 	if (__isthreaded)		\
444 		funlockfile(_fp);	\
445 	return (_ret);			\
446 }
447 #define	getc(fp)	__getc_locked(fp)
448 #define	putc(x, fp)	__putc_locked(x, fp)
449 #else
450 #define	getc(fp)	getc_unlocked(fp)
451 #define putc(x, fp)	putc_unlocked(x, fp)
452 #endif
453 #endif /* lint */
454 
455 #define	getchar()		getc(stdin)
456 #define	getchar_unlocked()	getc_unlocked(stdin)
457 #define	putchar(x)		putc(x, stdout)
458 #define	putchar_unlocked(x)	putc_unlocked(x, stdout)
459 
460 #endif /* !_STDIO_H_ */
461