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