xref: /original-bsd/include/stdio.h (revision ca98dac2)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Torek.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)stdio.h	5.23 (Berkeley) 10/09/92
11  */
12 
13 #ifndef	_STDIO_H_
14 #define	_STDIO_H_
15 
16 #if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
17 #include <sys/types.h>
18 #endif
19 
20 #include <sys/cdefs.h>
21 
22 #include <machine/ansi.h>
23 #ifdef	_BSD_SIZE_T_
24 typedef	_BSD_SIZE_T_	size_t;
25 #undef	_BSD_SIZE_T_
26 #endif
27 
28 #ifndef NULL
29 #define	NULL	0
30 #endif
31 
32 /*
33  * This is fairly grotesque, but pure ANSI code must not inspect the
34  * innards of an fpos_t anyway.  The library internally uses off_t,
35  * which we assume is exactly as big as eight chars.
36  */
37 #if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
38 typedef off_t fpos_t;
39 #else
40 typedef struct __sfpos {
41 	char	_pos[8];
42 } fpos_t;
43 #endif
44 
45 #define	_FSTDIO			/* Define for new stdio with functions. */
46 
47 /*
48  * NB: to fit things in six character monocase externals, the stdio
49  * code uses the prefix `__s' for stdio objects, typically followed
50  * by a three-character attempt at a mnemonic.
51  */
52 
53 /* stdio buffers */
54 struct __sbuf {
55 	unsigned char *_base;
56 	int	_size;
57 };
58 
59 /*
60  * stdio state variables.
61  *
62  * The following always hold:
63  *
64  *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
65  *		_lbfsize is -_bf._size, else _lbfsize is 0
66  *	if _flags&__SRD, _w is 0
67  *	if _flags&__SWR, _r is 0
68  *
69  * This ensures that the getc and putc macros (or inline functions) never
70  * try to write or read from a file that is in `read' or `write' mode.
71  * (Moreover, they can, and do, automatically switch from read mode to
72  * write mode, and back, on "r+" and "w+" files.)
73  *
74  * _lbfsize is used only to make the inline line-buffered output stream
75  * code as compact as possible.
76  *
77  * _ub, _up, and _ur are used when ungetc() pushes back more characters
78  * than fit in the current _bf, or when ungetc() pushes back a character
79  * that does not match the previous one in _bf.  When this happens,
80  * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
81  * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
82  */
83 typedef	struct __sFILE {
84 	unsigned char *_p;	/* current position in (some) buffer */
85 	int	_r;		/* read space left for getc() */
86 	int	_w;		/* write space left for putc() */
87 	short	_flags;		/* flags, below; this FILE is free if 0 */
88 	short	_file;		/* fileno, if Unix descriptor, else -1 */
89 	struct	__sbuf _bf;	/* the buffer (at least 1 byte, if !NULL) */
90 	int	_lbfsize;	/* 0 or -_bf._size, for inline putc */
91 
92 	/* operations */
93 	void	*_cookie;	/* cookie passed to io functions */
94 	int	(*_close) __P((void *));
95 	int	(*_read)  __P((void *, char *, int));
96 	fpos_t	(*_seek)  __P((void *, fpos_t, int));
97 	int	(*_write) __P((void *, const char *, int));
98 
99 	/* separate buffer for long sequences of ungetc() */
100 	struct	__sbuf _ub;	/* ungetc buffer */
101 	unsigned char *_up;	/* saved _p when _p is doing ungetc data */
102 	int	_ur;		/* saved _r when _r is counting ungetc data */
103 
104 	/* tricks to meet minimum requirements even when malloc() fails */
105 	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
106 	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
107 
108 	/* separate buffer for fgetline() when line crosses buffer boundary */
109 	struct	__sbuf _lb;	/* buffer for fgetline() */
110 
111 	/* Unix stdio files get aligned to block boundaries on fseek() */
112 	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
113 	int	_offset;	/* current lseek offset */
114 } FILE;
115 
116 __BEGIN_DECLS
117 extern FILE __sF[];
118 __END_DECLS
119 
120 #define	__SLBF	0x0001		/* line buffered */
121 #define	__SNBF	0x0002		/* unbuffered */
122 #define	__SRD	0x0004		/* OK to read */
123 #define	__SWR	0x0008		/* OK to write */
124 	/* RD and WR are never simultaneously asserted */
125 #define	__SRW	0x0010		/* open for reading & writing */
126 #define	__SEOF	0x0020		/* found EOF */
127 #define	__SERR	0x0040		/* found error */
128 #define	__SMBF	0x0080		/* _buf is from malloc */
129 #define	__SAPP	0x0100		/* fdopen()ed in append mode */
130 #define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
131 #define	__SOPT	0x0400		/* do fseek() optimisation */
132 #define	__SNPT	0x0800		/* do not do fseek() optimisation */
133 #define	__SOFF	0x1000		/* set iff _offset is in fact correct */
134 #define	__SMOD	0x2000		/* true => fgetline modified _p text */
135 
136 /*
137  * The following three definitions are for ANSI C, which took them
138  * from System V, which brilliantly took internal interface macros and
139  * made them official arguments to setvbuf(), without renaming them.
140  * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
141  *
142  * Although numbered as their counterparts above, the implementation
143  * does not rely on this.
144  */
145 #define	_IOFBF	0		/* setvbuf should set fully buffered */
146 #define	_IOLBF	1		/* setvbuf should set line buffered */
147 #define	_IONBF	2		/* setvbuf should set unbuffered */
148 
149 #define	BUFSIZ	1024		/* size of buffer used by setbuf */
150 #define	EOF	(-1)
151 
152 /*
153  * FOPEN_MAX is a minimum maximum, and should be the number of descriptors
154  * that the kernel can provide without allocation of a resource that can
155  * fail without the process sleeping.  Do not use this for anything.
156  */
157 #define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
158 #define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
159 
160 /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
161 #ifndef _ANSI_SOURCE
162 #define	P_tmpdir	"/var/tmp/"
163 #endif
164 #define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
165 #define	TMP_MAX		308915776
166 
167 #ifndef SEEK_SET
168 #define	SEEK_SET	0	/* set file offset to offset */
169 #endif
170 #ifndef SEEK_CUR
171 #define	SEEK_CUR	1	/* set file offset to current plus offset */
172 #endif
173 #ifndef SEEK_END
174 #define	SEEK_END	2	/* set file offset to EOF plus offset */
175 #endif
176 
177 #define	stdin	(&__sF[0])
178 #define	stdout	(&__sF[1])
179 #define	stderr	(&__sF[2])
180 
181 /*
182  * Functions defined in ANSI C standard.
183  */
184 __BEGIN_DECLS
185 void	 clearerr __P((FILE *));
186 int	 fclose __P((FILE *));
187 int	 feof __P((FILE *));
188 int	 ferror __P((FILE *));
189 int	 fflush __P((FILE *));
190 int	 fgetc __P((FILE *));
191 int	 fgetpos __P((FILE *, fpos_t *));
192 char	*fgets __P((char *, size_t, FILE *));
193 FILE	*fopen __P((const char *, const char *));
194 int	 fprintf __P((FILE *, const char *, ...));
195 int	 fputc __P((int, FILE *));
196 int	 fputs __P((const char *, FILE *));
197 size_t	 fread __P((void *, size_t, size_t, FILE *));
198 FILE	*freopen __P((const char *, const char *, FILE *));
199 int	 fscanf __P((FILE *, const char *, ...));
200 int	 fseek __P((FILE *, long, int));
201 int	 fsetpos __P((FILE *, const fpos_t *));
202 long	 ftell __P((const FILE *));
203 size_t	 fwrite __P((const void *, size_t, size_t, FILE *));
204 int	 getc __P((FILE *));
205 int	 getchar __P((void));
206 char	*gets __P((char *));
207 #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
208 extern int sys_nerr;			/* perror(3) external variables */
209 extern char *sys_errlist[];
210 #endif
211 void	 perror __P((const char *));
212 int	 printf __P((const char *, ...));
213 int	 putc __P((int, FILE *));
214 int	 putchar __P((int));
215 int	 puts __P((const char *));
216 int	 remove __P((const char *));
217 int	 rename  __P((const char *, const char *));
218 void	 rewind __P((FILE *));
219 int	 scanf __P((const char *, ...));
220 void	 setbuf __P((FILE *, char *));
221 int	 setvbuf __P((FILE *, char *, int, size_t));
222 int	 sprintf __P((char *, const char *, ...));
223 int	 sscanf __P((const char *, const char *, ...));
224 FILE	*tmpfile __P((void));
225 char	*tmpnam __P((char *));
226 int	 ungetc __P((int, FILE *));
227 int	 vfprintf __P((FILE *, const char *, _BSD_VA_LIST_));
228 int	 vprintf __P((const char *, _BSD_VA_LIST_));
229 int	 vsprintf __P((char *, const char *, _BSD_VA_LIST_));
230 __END_DECLS
231 
232 /*
233  * Functions defined in POSIX 1003.1.
234  */
235 #ifndef _ANSI_SOURCE
236 #define	L_cuserid	9	/* size for cuserid(); UT_NAMESIZE + 1 */
237 #define	L_ctermid	1024	/* size for ctermid(); PATH_MAX */
238 
239 __BEGIN_DECLS
240 char	*ctermid __P((char *));
241 FILE	*fdopen __P((int, const char *));
242 int	 fileno __P((FILE *));
243 __END_DECLS
244 #endif /* not ANSI */
245 
246 /*
247  * Routines that are purely local.
248  */
249 #if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
250 __BEGIN_DECLS
251 char	*fgetline __P((FILE *, size_t *));
252 int	 fpurge __P((FILE *));
253 int	 getw __P((FILE *));
254 int	 pclose __P((FILE *));
255 FILE	*popen __P((const char *, const char *));
256 int	 putw __P((int, FILE *));
257 void	 setbuffer __P((FILE *, char *, int));
258 int	 setlinebuf __P((FILE *));
259 char	*tempnam __P((const char *, const char *));
260 int	 snprintf __P((char *, size_t, const char *, ...));
261 int	 vsnprintf __P((char *, size_t, const char *, _BSD_VA_LIST_));
262 int	 vscanf __P((const char *, _BSD_VA_LIST_));
263 int	 vsscanf __P((const char *, const char *, _BSD_VA_LIST_));
264 __END_DECLS
265 
266 /*
267  * This is a #define because the function is used internally and
268  * (unlike vfscanf) the name __svfscanf is guaranteed not to collide
269  * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined.
270  */
271 #define	 vfscanf	__svfscanf
272 
273 /*
274  * Stdio function-access interface.
275  */
276 __BEGIN_DECLS
277 FILE	*funopen __P((const void *,
278 		int (*)(void *, char *, int),
279 		int (*)(void *, const char *, int),
280 		fpos_t (*)(void *, fpos_t, int),
281 		int (*)(void *)));
282 __END_DECLS
283 #define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
284 #define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
285 #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
286 
287 /*
288  * Functions internal to the implementation.
289  */
290 __BEGIN_DECLS
291 int	__srget __P((FILE *));
292 int	__svfscanf __P((FILE *, const char *, _BSD_VA_LIST_));
293 int	__swbuf __P((int, FILE *));
294 __END_DECLS
295 
296 /*
297  * The __sfoo macros are here so that we can
298  * define function versions in the C library.
299  */
300 #define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
301 #if defined(__GNUC__) && defined(__STDC__)
302 static __inline int __sputc(int _c, FILE *_p) {
303 	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
304 		return (*_p->_p++ = _c);
305 	else
306 		return (__swbuf(_c, _p));
307 }
308 #else
309 /*
310  * This has been tuned to generate reasonable code on the vax using pcc.
311  */
312 #define	__sputc(c, p) \
313 	(--(p)->_w < 0 ? \
314 		(p)->_w >= (p)->_lbfsize ? \
315 			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
316 				(int)*(p)->_p++ : \
317 				__swbuf('\n', p) : \
318 			__swbuf((int)(c), p) : \
319 		(*(p)->_p = (c), (int)*(p)->_p++))
320 #endif
321 
322 #define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
323 #define	__sferror(p)	(((p)->_flags & __SERR) != 0)
324 #define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
325 #define	__sfileno(p)	((p)->_file)
326 
327 #define	feof(p)		__sfeof(p)
328 #define	ferror(p)	__sferror(p)
329 #define	clearerr(p)	__sclearerr(p)
330 
331 #ifndef _ANSI_SOURCE
332 #define	fileno(p)	__sfileno(p)
333 #endif
334 
335 #ifndef lint
336 #define	getc(fp)	__sgetc(fp)
337 #define putc(x, fp)	__sputc(x, fp)
338 #endif /* lint */
339 
340 #define	getchar()	getc(stdin)
341 #define	putchar(x)	putc(x, stdout)
342 #endif /* _STDIO_H_ */
343