1/*	$OpenBSD: stdio.h,v 1.35 2006/01/13 18:10:09 miod Exp $	*/
2/*	$NetBSD: stdio.h,v 1.18 1996/04/25 18:29:21 jtc Exp $	*/
3
4/*-
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Chris Torek.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	@(#)stdio.h	5.17 (Berkeley) 6/3/91
36 */
37
38#ifndef	_STDIO_H_
39#define	_STDIO_H_
40
41#include <sys/cdefs.h>
42#include <sys/_types.h>
43
44/* va_list and size_t must be defined by stdio.h according to Posix */
45#define __need___va_list
46#include <stdarg.h>
47
48/* note that this forces stddef.h to *only* define size_t */
49#define __need_size_t
50#include <stddef.h>
51
52#include <stddef.h>
53
54#if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE
55#include <sys/types.h>	/* XXX should be removed */
56#endif
57
58#ifndef	_SIZE_T_DEFINED_
59#define	_SIZE_T_DEFINED_
60typedef	unsigned long    size_t;
61#endif
62
63#ifndef	_OFF_T_DEFINED_
64#define	_OFF_T_DEFINED_
65typedef	long    off_t;
66#endif
67
68#ifndef NULL
69#ifdef 	__GNUG__
70#define	NULL	__null
71#else
72#define	NULL	0L
73#endif
74#endif
75
76#define	_FSTDIO			/* Define for new stdio with functions. */
77
78typedef off_t fpos_t;		/* stdio file position type */
79
80/*
81 * NB: to fit things in six character monocase externals, the stdio
82 * code uses the prefix `__s' for stdio objects, typically followed
83 * by a three-character attempt at a mnemonic.
84 */
85
86/* stdio buffers */
87struct __sbuf {
88	unsigned char *_base;
89	int	_size;
90};
91
92/*
93 * stdio state variables.
94 *
95 * The following always hold:
96 *
97 *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
98 *		_lbfsize is -_bf._size, else _lbfsize is 0
99 *	if _flags&__SRD, _w is 0
100 *	if _flags&__SWR, _r is 0
101 *
102 * This ensures that the getc and putc macros (or inline functions) never
103 * try to write or read from a file that is in `read' or `write' mode.
104 * (Moreover, they can, and do, automatically switch from read mode to
105 * write mode, and back, on "r+" and "w+" files.)
106 *
107 * _lbfsize is used only to make the inline line-buffered output stream
108 * code as compact as possible.
109 *
110 * _ub, _up, and _ur are used when ungetc() pushes back more characters
111 * than fit in the current _bf, or when ungetc() pushes back a character
112 * that does not match the previous one in _bf.  When this happens,
113 * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
114 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
115 *
116 * NOTE: if you change this structure, you also need to update the
117 * std() initializer in findfp.c.
118 */
119typedef	struct __sFILE {
120	unsigned char *_p;	/* current position in (some) buffer */
121	int	_r;		/* read space left for getc() */
122	int	_w;		/* write space left for putc() */
123	short	_flags;		/* flags, below; this FILE is free if 0 */
124	short	_file;		/* fileno, if Unix descriptor, else -1 */
125	struct	__sbuf _bf;	/* the buffer (at least 1 byte, if !NULL) */
126	int	_lbfsize;	/* 0 or -_bf._size, for inline putc */
127
128	/* operations */
129	void	*_cookie;	/* cookie passed to io functions */
130	int	(*_close)(void *);
131	int	(*_read)(void *, char *, int);
132	fpos_t	(*_seek)(void *, fpos_t, int);
133	int	(*_write)(void *, const char *, int);
134
135	/* extension data, to avoid further ABI breakage */
136	struct	__sbuf _ext;
137	/* data for long sequences of ungetc() */
138	unsigned char *_up;	/* saved _p when _p is doing ungetc data */
139	int	_ur;		/* saved _r when _r is counting ungetc data */
140
141	/* tricks to meet minimum requirements even when malloc() fails */
142	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
143	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
144
145	/* separate buffer for fgetln() when line crosses buffer boundary */
146	struct	__sbuf _lb;	/* buffer for fgetln() */
147
148	/* Unix stdio files get aligned to block boundaries on fseek() */
149	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
150	fpos_t	_offset;	/* current lseek offset */
151} FILE;
152
153__BEGIN_DECLS
154extern FILE __sF[];
155__END_DECLS
156
157#define	__SLBF	0x0001		/* line buffered */
158#define	__SNBF	0x0002		/* unbuffered */
159#define	__SRD	0x0004		/* OK to read */
160#define	__SWR	0x0008		/* OK to write */
161	/* RD and WR are never simultaneously asserted */
162#define	__SRW	0x0010		/* open for reading & writing */
163#define	__SEOF	0x0020		/* found EOF */
164#define	__SERR	0x0040		/* found error */
165#define	__SMBF	0x0080		/* _buf is from malloc */
166#define	__SAPP	0x0100		/* fdopen()ed in append mode */
167#define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
168#define	__SOPT	0x0400		/* do fseek() optimisation */
169#define	__SNPT	0x0800		/* do not do fseek() optimisation */
170#define	__SOFF	0x1000		/* set iff _offset is in fact correct */
171#define	__SMOD	0x2000		/* true => fgetln modified _p text */
172#define	__SALC	0x4000		/* allocate string space dynamically */
173#define	__SIGN	0x8000		/* ignore this file in _fwalk */
174
175/*
176 * The following three definitions are for ANSI C, which took them
177 * from System V, which brilliantly took internal interface macros and
178 * made them official arguments to setvbuf(), without renaming them.
179 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
180 *
181 * Although numbered as their counterparts above, the implementation
182 * does not rely on this.
183 */
184#define	_IOFBF	0		/* setvbuf should set fully buffered */
185#define	_IOLBF	1		/* setvbuf should set line buffered */
186#define	_IONBF	2		/* setvbuf should set unbuffered */
187
188#define	BUFSIZ	1024		/* size of buffer used by setbuf */
189
190#define	EOF	(-1)
191
192/*
193 * FOPEN_MAX is a minimum maximum, and should be the number of descriptors
194 * that the kernel can provide without allocation of a resource that can
195 * fail without the process sleeping.  Do not use this for anything.
196 */
197#define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
198#define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
199
200/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
201#if __BSD_VISIBLE || __XPG_VISIBLE
202#define	P_tmpdir	"/tmp/"
203#endif
204#define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
205#define	TMP_MAX		308915776
206
207#ifndef SEEK_SET
208#define	SEEK_SET	0	/* set file offset to offset */
209#endif
210#ifndef SEEK_CUR
211#define	SEEK_CUR	1	/* set file offset to current plus offset */
212#endif
213#ifndef SEEK_END
214#define	SEEK_END	2	/* set file offset to EOF plus offset */
215#endif
216
217#define	stdin	(&__sF[0])
218#define	stdout	(&__sF[1])
219#define	stderr	(&__sF[2])
220
221/*
222 * Functions defined in ANSI C standard.
223 */
224__BEGIN_DECLS
225void	 clearerr(FILE *);
226int	 fclose(FILE *);
227int	 feof(FILE *);
228int	 ferror(FILE *);
229int	 fflush(FILE *);
230int	 fgetc(FILE *);
231int	 fgetpos(FILE *, fpos_t *);
232char	*fgets(char *, int, FILE *);
233FILE	*fopen(const char *, const char *);
234int	 fprintf(FILE *, const char *, ...)
235		__attribute__((__format__ (printf, 2, 3)))
236		__attribute__((__nonnull__ (2)));
237int	 fputc(int, FILE *);
238int	 fputs(const char *, FILE *);
239size_t	 fread(void *, size_t, size_t, FILE *);
240FILE	*freopen(const char *, const char *, FILE *);
241int	 fscanf(FILE *, const char *, ...)
242		__attribute__ ((__format__ (scanf, 2, 3)))
243		__attribute__ ((__nonnull__ (2)));
244int	 fseek(FILE *, long, int);
245int	 fseeko(FILE *, off_t, int);
246int	 fsetpos(FILE *, const fpos_t *);
247long	 ftell(FILE *);
248off_t	 ftello(FILE *);
249size_t	 fwrite(const void *, size_t, size_t, FILE *);
250int	 getc(FILE *);
251int	 getchar(void);
252char	*gets(char *);
253#if __BSD_VISIBLE && !defined(__SYS_ERRLIST)
254#define __SYS_ERRLIST
255
256extern int sys_nerr;			/* perror(3) external variables */
257extern char *sys_errlist[];
258#endif
259void	 perror(const char *);
260int	 printf(const char *, ...)
261		__attribute__((__format__ (printf, 1, 2)))
262		__attribute__((__nonnull__ (1)));
263int	 putc(int, FILE *);
264int	 putchar(int);
265int	 puts(const char *);
266int	 remove(const char *);
267int	 rename(const char *, const char *);
268void	 rewind(FILE *);
269int	 scanf(const char *, ...)
270		__attribute__ ((__format__ (scanf, 1, 2)))
271		__attribute__ ((__nonnull__ (1)));
272void	 setbuf(FILE *, char *);
273int	 setvbuf(FILE *, char *, int, size_t);
274int	 sprintf(char *, const char *, ...)
275		__attribute__((__format__ (printf, 2, 3)))
276		__attribute__((__nonnull__ (2)));
277int	 sscanf(const char *, const char *, ...)
278		__attribute__ ((__format__ (scanf, 2, 3)))
279		__attribute__ ((__nonnull__ (2)));
280FILE	*tmpfile(void);
281char	*tmpnam(char *);
282int	 ungetc(int, FILE *);
283int	 vfprintf(FILE *, const char *, __va_list)
284		__attribute__((__format__ (printf, 2, 0)))
285		__attribute__((__nonnull__ (2)));
286int	 vprintf(const char *, __va_list)
287		__attribute__((__format__ (printf, 1, 0)))
288		__attribute__((__nonnull__ (1)));
289int	 vsprintf(char *, const char *, __va_list)
290		__attribute__((__format__ (printf, 2, 0)))
291		__attribute__((__nonnull__ (2)));
292
293#if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE
294int	 snprintf(char *, size_t, const char *, ...)
295		__attribute__((__format__ (printf, 3, 4)))
296		__attribute__((__nonnull__ (3)));
297int	 vfscanf(FILE *, const char *, __va_list)
298		__attribute__((__format__ (scanf, 2, 0)))
299		__attribute__((__nonnull__ (2)));
300int	 vscanf(const char *, __va_list)
301		__attribute__((__format__ (scanf, 1, 0)))
302		__attribute__((__nonnull__ (1)));
303int	 vsnprintf(char *, size_t, const char *, __va_list)
304		__attribute__((__format__ (printf, 3, 0)))
305		__attribute__((__nonnull__ (3)));
306int	 vsscanf(const char *, const char *, __va_list)
307		__attribute__((__format__ (scanf, 2, 0)))
308		__attribute__((__nonnull__ (2)));
309#endif /* __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE */
310
311__END_DECLS
312
313
314/*
315 * Functions defined in POSIX 1003.1.
316 */
317#if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE
318#define	L_ctermid	1024	/* size for ctermid(); PATH_MAX */
319#define L_cuserid	9	/* size for cuserid(); UT_NAMESIZE + 1 */
320
321__BEGIN_DECLS
322#if 0 /* MISSING FROM BIONIC */
323char	*ctermid(char *);
324char	*cuserid(char *);
325#endif /* MISSING */
326FILE	*fdopen(int, const char *);
327int	 fileno(FILE *);
328
329#if (__POSIX_VISIBLE >= 199209)
330int	 pclose(FILE *);
331FILE	*popen(const char *, const char *);
332#endif
333
334#if __POSIX_VISIBLE >= 199506
335void	 flockfile(FILE *);
336int	 ftrylockfile(FILE *);
337void	 funlockfile(FILE *);
338
339/*
340 * These are normally used through macros as defined below, but POSIX
341 * requires functions as well.
342 */
343int	 getc_unlocked(FILE *);
344int	 getchar_unlocked(void);
345int	 putc_unlocked(int, FILE *);
346int	 putchar_unlocked(int);
347#endif /* __POSIX_VISIBLE >= 199506 */
348
349#if __XPG_VISIBLE
350char	*tempnam(const char *, const char *);
351#endif
352__END_DECLS
353
354#endif /* __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE */
355
356/*
357 * Routines that are purely local.
358 */
359#if __BSD_VISIBLE
360__BEGIN_DECLS
361int	 asprintf(char **, const char *, ...)
362		__attribute__((__format__ (printf, 2, 3)))
363		__attribute__((__nonnull__ (2)));
364char	*fgetln(FILE *, size_t *);
365int	 fpurge(FILE *);
366int	 getw(FILE *);
367int	 putw(int, FILE *);
368void	 setbuffer(FILE *, char *, int);
369int	 setlinebuf(FILE *);
370int	 vasprintf(char **, const char *, __va_list)
371		__attribute__((__format__ (printf, 2, 0)))
372		__attribute__((__nonnull__ (2)));
373__END_DECLS
374
375/*
376 * Stdio function-access interface.
377 */
378__BEGIN_DECLS
379FILE	*funopen(const void *,
380		int (*)(void *, char *, int),
381		int (*)(void *, const char *, int),
382		fpos_t (*)(void *, fpos_t, int),
383		int (*)(void *));
384__END_DECLS
385#define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
386#define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
387#endif /* __BSD_VISIBLE */
388
389/*
390 * Functions internal to the implementation.
391 */
392__BEGIN_DECLS
393int	__srget(FILE *);
394int	__swbuf(int, FILE *);
395__END_DECLS
396
397/*
398 * The __sfoo macros are here so that we can
399 * define function versions in the C library.
400 */
401#define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
402#if defined(__GNUC__)
403static __inline int __sputc(int _c, FILE *_p) {
404	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
405		return (*_p->_p++ = _c);
406	else
407		return (__swbuf(_c, _p));
408}
409#else
410/*
411 * This has been tuned to generate reasonable code on the vax using pcc.
412 */
413#define	__sputc(c, p) \
414	(--(p)->_w < 0 ? \
415		(p)->_w >= (p)->_lbfsize ? \
416			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
417				(int)*(p)->_p++ : \
418				__swbuf('\n', p) : \
419			__swbuf((int)(c), p) : \
420		(*(p)->_p = (c), (int)*(p)->_p++))
421#endif
422
423#define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
424#define	__sferror(p)	(((p)->_flags & __SERR) != 0)
425#define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
426#define	__sfileno(p)	((p)->_file)
427
428extern	int __isthreaded;
429
430#define	feof(p)		(!__isthreaded ? __sfeof(p) : (feof)(p))
431#define	ferror(p)	(!__isthreaded ? __sferror(p) : (ferror)(p))
432#define	clearerr(p)	(!__isthreaded ? __sclearerr(p) : (clearerr)(p))
433
434#if __POSIX_VISIBLE
435#define	fileno(p)	(!__isthreaded ? __sfileno(p) : (fileno)(p))
436#endif
437
438#define	getc(fp)	(!__isthreaded ? __sgetc(fp) : (getc)(fp))
439
440#if __BSD_VISIBLE
441/*
442 * The macro implementations of putc and putc_unlocked are not
443 * fully POSIX compliant; they do not set errno on failure
444 */
445#define putc(x, fp)	(!__isthreaded ? __sputc(x, fp) : (putc)(x, fp))
446#endif /* __BSD_VISIBLE */
447
448#ifndef lint
449#if __POSIX_VISIBLE >= 199506
450#define	getc_unlocked(fp)	__sgetc(fp)
451/*
452 * The macro implementations of putc and putc_unlocked are not
453 * fully POSIX compliant; they do not set errno on failure
454 */
455#if __BSD_VISIBLE
456#define putc_unlocked(x, fp)	__sputc(x, fp)
457#endif /* __BSD_VISIBLE */
458#endif /* __POSIX_VISIBLE >= 199506 */
459#endif /* lint */
460
461#define	getchar()	getc(stdin)
462#define	putchar(x)	putc(x, stdout)
463#define	getchar_unlocked()	getc_unlocked(stdin)
464#define	putchar_unlocked(c)	putc_unlocked(c, stdout)
465
466#ifdef _GNU_SOURCE
467/*
468 * glibc defines dprintf(int, const char*, ...), which is poorly named
469 * and likely to conflict with locally defined debugging printfs
470 * fdprintf is a better name, and some programs that use fdprintf use a
471 * #define fdprintf dprintf for compatibility
472 */
473__BEGIN_DECLS
474int fdprintf(int, const char*, ...)
475		__attribute__((__format__ (printf, 2, 3)))
476		__attribute__((__nonnull__ (2)));
477int vfdprintf(int, const char*, __va_list)
478		__attribute__((__format__ (printf, 2, 0)))
479		__attribute__((__nonnull__ (2)));
480__END_DECLS
481#endif /* _GNU_SOURCE */
482
483#if defined(__BIONIC_FORTIFY_INLINE)
484
485__BIONIC_FORTIFY_INLINE
486__attribute__((__format__ (printf, 3, 0)))
487__attribute__((__nonnull__ (3)))
488int vsnprintf(char *dest, size_t size, const char *format, __va_list ap)
489{
490    return __builtin___vsnprintf_chk(dest, size, 0,
491        __builtin_object_size(dest, 0), format, ap);
492}
493
494__BIONIC_FORTIFY_INLINE
495__attribute__((__format__ (printf, 2, 0)))
496__attribute__((__nonnull__ (2)))
497int vsprintf(char *dest, const char *format, __va_list ap)
498{
499    return __builtin___vsprintf_chk(dest, 0,
500        __builtin_object_size(dest, 0), format, ap);
501}
502
503__BIONIC_FORTIFY_INLINE
504__attribute__((__format__ (printf, 3, 4)))
505__attribute__((__nonnull__ (3)))
506int snprintf(char *str, size_t size, const char *format, ...)
507{
508    return __builtin___snprintf_chk(str, size, 0,
509        __builtin_object_size(str, 0), format, __builtin_va_arg_pack());
510}
511
512__BIONIC_FORTIFY_INLINE
513__attribute__((__format__ (printf, 2, 3)))
514__attribute__((__nonnull__ (2)))
515int sprintf(char *dest, const char *format, ...)
516{
517    return __builtin___sprintf_chk(dest, 0,
518        __builtin_object_size(dest, 0), format, __builtin_va_arg_pack());
519}
520
521extern char *__fgets_real(char *, int, FILE *)
522    __asm__(__USER_LABEL_PREFIX__ "fgets");
523extern void __fgets_too_big_error()
524    __attribute__((__error__("fgets called with size bigger than buffer")));
525extern void __fgets_too_small_error()
526    __attribute__((__error__("fgets called with size less than zero")));
527extern char *__fgets_chk(char *, int, FILE *, size_t);
528
529__BIONIC_FORTIFY_INLINE
530char *fgets(char *dest, int size, FILE *stream)
531{
532    size_t bos = __builtin_object_size(dest, 0);
533
534    // Compiler can prove, at compile time, that the passed in size
535    // is always negative. Force a compiler error.
536    if (__builtin_constant_p(size) && (size < 0)) {
537        __fgets_too_small_error();
538    }
539
540    // Compiler doesn't know destination size. Don't call __fgets_chk
541    if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
542        return __fgets_real(dest, size, stream);
543    }
544
545    // Compiler can prove, at compile time, that the passed in size
546    // is always <= the actual object size. Don't call __fgets_chk
547    if (__builtin_constant_p(size) && (size <= bos)) {
548        return __fgets_real(dest, size, stream);
549    }
550
551    // Compiler can prove, at compile time, that the passed in size
552    // is always > the actual object size. Force a compiler error.
553    if (__builtin_constant_p(size) && (size > bos)) {
554        __fgets_too_big_error();
555    }
556
557    return __fgets_chk(dest, size, stream, bos);
558}
559
560#endif /* defined(__BIONIC_FORTIFY_INLINE) */
561
562#endif /* _STDIO_H_ */
563