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