1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * @(#)stdio.h 5.3 (Berkeley) 3/15/86
18 */
19
20 /*
21 * NB: to fit things in six character monocase externals, the
22 * stdio code uses the prefix `__s' for stdio objects, typically
23 * followed by a three-character attempt at a mnemonic.
24 */
25
26 #ifndef _STDIO_H_
27 #define _STDIO_H_
28
29 #include "_ansi.h"
30
31 #define _FSTDIO /* ``function stdio'' */
32
33 #define __need_size_t
34 #include <stddef.h>
35
36 #define __need___va_list
37 #include <stdarg.h>
38
39 /*
40 * <sys/reent.h> defines __FILE, _fpos_t.
41 * They must be defined there because struct _reent needs them (and we don't
42 * want reent.h to include this file.
43 */
44
45 #include <sys/reent.h>
46 #include <sys/types.h>
47
48 _BEGIN_STD_C
49
50 typedef __FILE FILE;
51
52 #ifdef __CYGWIN__
53 #ifdef __CYGWIN_USE_BIG_TYPES__
54 typedef _fpos64_t fpos_t;
55 #else
56 typedef _fpos_t fpos_t;
57 #endif
58 #else
59 typedef _fpos_t fpos_t;
60 #ifdef __LARGE64_FILES
61 typedef _fpos64_t fpos64_t;
62 #endif
63 #endif /* !__CYGWIN__ */
64
65 #include <sys/stdio.h>
66
67 #define __SLBF 0x0001 /* line buffered */
68 #define __SNBF 0x0002 /* unbuffered */
69 #define __SRD 0x0004 /* OK to read */
70 #define __SWR 0x0008 /* OK to write */
71 /* RD and WR are never simultaneously asserted */
72 #define __SRW 0x0010 /* open for reading & writing */
73 #define __SEOF 0x0020 /* found EOF */
74 #define __SERR 0x0040 /* found error */
75 #define __SMBF 0x0080 /* _buf is from malloc */
76 #define __SAPP 0x0100 /* fdopen()ed in append mode - so must write to end */
77 #define __SSTR 0x0200 /* this is an sprintf/snprintf string */
78 #define __SOPT 0x0400 /* do fseek() optimisation */
79 #define __SNPT 0x0800 /* do not do fseek() optimisation */
80 #define __SOFF 0x1000 /* set iff _offset is in fact correct */
81 #define __SMOD 0x2000 /* true => fgetline modified _p text */
82 #if defined(__CYGWIN__)
83 # define __SCLE 0x4000 /* convert line endings CR/LF <-> NL */
84 #endif
85 #define __SL64 0x8000 /* is 64-bit offset large file */
86
87 /*
88 * The following three definitions are for ANSI C, which took them
89 * from System V, which stupidly took internal interface macros and
90 * made them official arguments to setvbuf(), without renaming them.
91 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
92 *
93 * Although these happen to match their counterparts above, the
94 * implementation does not rely on that (so these could be renumbered).
95 */
96 #define _IOFBF 0 /* setvbuf should set fully buffered */
97 #define _IOLBF 1 /* setvbuf should set line buffered */
98 #define _IONBF 2 /* setvbuf should set unbuffered */
99
100 #ifndef NULL
101 #define NULL 0
102 #endif
103
104 #define EOF (-1)
105
106 #ifdef __BUFSIZ__
107 #define BUFSIZ __BUFSIZ__
108 #else
109 #define BUFSIZ 1024
110 #endif
111
112 #ifdef __FOPEN_MAX__
113 #define FOPEN_MAX __FOPEN_MAX__
114 #else
115 #define FOPEN_MAX 20
116 #endif
117
118 #ifdef __FILENAME_MAX__
119 #define FILENAME_MAX __FILENAME_MAX__
120 #else
121 #define FILENAME_MAX 1024
122 #endif
123
124 #ifdef __L_tmpnam__
125 #define L_tmpnam __L_tmpnam__
126 #else
127 #define L_tmpnam FILENAME_MAX
128 #endif
129
130 #ifndef __STRICT_ANSI__
131 #define P_tmpdir "/tmp"
132 #endif
133
134 #ifndef SEEK_SET
135 #define SEEK_SET 0 /* set file offset to offset */
136 #endif
137 #ifndef SEEK_CUR
138 #define SEEK_CUR 1 /* set file offset to current plus offset */
139 #endif
140 #ifndef SEEK_END
141 #define SEEK_END 2 /* set file offset to EOF plus offset */
142 #endif
143
144 #define TMP_MAX 26
145
146 #if defined(AEONSIM) || defined(XGEN)
147
148 FILE *__stdin();
149 FILE *__stdout();
150 FILE *__stderr();
151 #define stdin __stdin()
152 #define stdout __stdout()
153 #define stderr __stderr()
154
155 int __print_mvreg_V4HI(const char *head, int mvreg);
156 int __print_mvreg_V8QI(const char *head, int mvreg);
157 int __break(int v);
158
159
160 // Validation
161 void __md5_init(int h);
162 void __md5_append(int h, void *p, int size);
163 void __md5_finish(unsigned char digest[16], int h);
164
165 int __in_icache(void *p, size_t nbytes);
166 void __warn_hit_icache(void *p, size_t nbytes);
167 void __warn_miss_icache(void *p, size_t nbytes);
168
169 int __in_dcache(void *p, size_t nbytes);
170 void __warn_hit_dcache(void *p, size_t nbytes);
171 void __warn_miss_dcache(void *p, size_t nbytes);
172
173 #else
174
175 #ifndef _REENT_ONLY
176 #define stdin (_REENT->_stdin)
177 #define stdout (_REENT->_stdout)
178 #define stderr (_REENT->_stderr)
179 #else /* _REENT_ONLY */
180 #define stdin (_impure_ptr->_stdin)
181 #define stdout (_impure_ptr->_stdout)
182 #define stderr (_impure_ptr->_stderr)
183 #endif /* _REENT_ONLY */
184
185 #endif /* AEONSIM || XGEN */
186
187 #define _stdin_r(x) ((x)->_stdin)
188 #define _stdout_r(x) ((x)->_stdout)
189 #define _stderr_r(x) ((x)->_stderr)
190
191 /*
192 * Functions defined in ANSI C standard.
193 */
194
195 #ifdef __GNUC__
196 #define __VALIST __gnuc_va_list
197 #else
198 #define __VALIST char*
199 #endif
200
201 FILE * _EXFUN(tmpfile, (void));
202 char * _EXFUN(tmpnam, (char *));
203 int _EXFUN(fclose, (FILE *));
204 int _EXFUN(fflush, (FILE *));
205 FILE * _EXFUN(freopen, (const char *, const char *, FILE *));
206 void _EXFUN(setbuf, (FILE *, char *));
207 int _EXFUN(setvbuf, (FILE *, char *, int, size_t));
208 int _EXFUN(fprintf, (FILE *, const char *, ...));
209 int _EXFUN(fscanf, (FILE *, const char *, ...));
210 int _EXFUN(printf, (const char *, ...));
211 int _EXFUN(scanf, (const char *, ...));
212 int _EXFUN(sscanf, (const char *, const char *, ...));
213 int _EXFUN(vfprintf, (FILE *, const char *, __VALIST));
214 int _EXFUN(vprintf, (const char *, __VALIST));
215 int _EXFUN(vsprintf, (char *, const char *, __VALIST));
216 int _EXFUN(fgetc, (FILE *));
217 char * _EXFUN(fgets, (char *, int, FILE *));
218 int _EXFUN(fputc, (int, FILE *));
219 int _EXFUN(fputs, (const char *, FILE *));
220 int _EXFUN(getc, (FILE *));
221 int _EXFUN(getchar, (void));
222 char * _EXFUN(gets, (char *));
223 int _EXFUN(putc, (int, FILE *));
224 int _EXFUN(putchar, (int));
225 int _EXFUN(puts, (const char *));
226 int _EXFUN(ungetc, (int, FILE *));
227 size_t _EXFUN(fread, (_PTR, size_t _size, size_t _n, FILE *));
228 size_t _EXFUN(fwrite, (const _PTR , size_t _size, size_t _n, FILE *));
229 #ifdef _COMPILING_NEWLIB
230 int _EXFUN(fgetpos, (FILE *, _fpos_t *));
231 #else
232 int _EXFUN(fgetpos, (FILE *, fpos_t *));
233 #endif
234 int _EXFUN(fseek, (FILE *, long, int));
235 #ifdef _COMPILING_NEWLIB
236 int _EXFUN(fsetpos, (FILE *, const _fpos_t *));
237 #else
238 int _EXFUN(fsetpos, (FILE *, const fpos_t *));
239 #endif
240 long _EXFUN(ftell, ( FILE *));
241 void _EXFUN(rewind, (FILE *));
242 void _EXFUN(clearerr, (FILE *));
243 int _EXFUN(feof, (FILE *));
244 int _EXFUN(ferror, (FILE *));
245 void _EXFUN(perror, (const char *));
246 #ifndef _REENT_ONLY
247 FILE * _EXFUN(fopen, (const char *_name, const char *_type));
248 int _EXFUN(sprintf, (char *, const char *, ...));
249 int _EXFUN(remove, (const char *));
250 int _EXFUN(rename, (const char *, const char *));
251 #endif
252 #ifndef __STRICT_ANSI__
253 #ifdef _COMPILING_NEWLIB
254 int _EXFUN(fseeko, (FILE *, _off_t, int));
255 _off_t _EXFUN(ftello, ( FILE *));
256 #else
257 int _EXFUN(fseeko, (FILE *, off_t, int));
258 off_t _EXFUN(ftello, ( FILE *));
259 #endif
260 #ifndef _REENT_ONLY
261 int _EXFUN(asiprintf, (char **, const char *, ...));
262 int _EXFUN(asprintf, (char **, const char *, ...));
263 int _EXFUN(dprintf, (int, const char *, ...));
264 int _EXFUN(fcloseall, (_VOID));
265 int _EXFUN(fiprintf, (FILE *, const char *, ...));
266 int _EXFUN(iprintf, (const char *, ...));
267 int _EXFUN(siprintf, (char *, const char *, ...));
268 int _EXFUN(snprintf, (char *, size_t, const char *, ...));
269 int _EXFUN(sniprintf, (char *, size_t, const char *, ...));
270 char * _EXFUN(tempnam, (const char *, const char *));
271 int _EXFUN(vasiprintf, (char **, const char *, __VALIST));
272 int _EXFUN(vasprintf, (char **, const char *, __VALIST));
273 int _EXFUN(vdprintf, (int, const char *, __VALIST));
274 int _EXFUN(vsniprintf, (char *, size_t, const char *, __VALIST));
275 int _EXFUN(vsnprintf, (char *, size_t, const char *, __VALIST));
276 int _EXFUN(vfiprintf, (FILE *, const char *, __VALIST));
277 int _EXFUN(vfiscanf, (FILE *, const char *, __VALIST));
278 int _EXFUN(vfscanf, (FILE *, const char *, __VALIST));
279 int _EXFUN(viprintf, (const char *, __VALIST));
280 int _EXFUN(viscanf, (const char *, __VALIST));
281 int _EXFUN(vscanf, (const char *, __VALIST));
282 int _EXFUN(vsiscanf, (const char *, const char *, __VALIST));
283 int _EXFUN(vsscanf, (const char *, const char *, __VALIST));
284 #endif
285 #endif
286
287 /*
288 * Routines in POSIX 1003.1.
289 */
290
291 #ifndef __STRICT_ANSI__
292 #ifndef _REENT_ONLY
293 FILE * _EXFUN(fdopen, (int, const char *));
294 #endif
295 int _EXFUN(fileno, (FILE *));
296 int _EXFUN(getw, (FILE *));
297 int _EXFUN(pclose, (FILE *));
298 FILE * _EXFUN(popen, (const char *, const char *));
299 int _EXFUN(putw, (int, FILE *));
300 void _EXFUN(setbuffer, (FILE *, char *, int));
301 int _EXFUN(setlinebuf, (FILE *));
302 int _EXFUN(getc_unlocked, (FILE *));
303 int _EXFUN(getchar_unlocked, (void));
304 void _EXFUN(flockfile, (FILE *));
305 int _EXFUN(ftrylockfile, (FILE *));
306 void _EXFUN(funlockfile, (FILE *));
307 int _EXFUN(putc_unlocked, (int, FILE *));
308 int _EXFUN(putchar_unlocked, (int));
309 #endif
310
311 /*
312 * Recursive versions of the above.
313 */
314
315 int _EXFUN(_asiprintf_r, (struct _reent *, char **, const char *, ...));
316 int _EXFUN(_asprintf_r, (struct _reent *, char **, const char *, ...));
317 int _EXFUN(_dprintf_r, (struct _reent *, int, const char *, ...));
318 int _EXFUN(_fcloseall_r, (struct _reent *));
319 FILE * _EXFUN(_fdopen_r, (struct _reent *, int, const char *));
320 FILE * _EXFUN(_fopen_r, (struct _reent *, const char *, const char *));
321 int _EXFUN(_fclose_r, (struct _reent *, FILE *));
322 int _EXFUN(_fiscanf_r, (struct _reent *, FILE *, const char *, ...));
323 int _EXFUN(_fscanf_r, (struct _reent *, FILE *, const char *, ...));
324 int _EXFUN(_fseek_r, (struct _reent *, FILE *, long, int));
325 long _EXFUN(_ftell_r, (struct _reent *, FILE *));
326 int _EXFUN(_getchar_r, (struct _reent *));
327 char * _EXFUN(_gets_r, (struct _reent *, char *));
328 int _EXFUN(_iprintf_r, (struct _reent *, const char *, ...));
329 int _EXFUN(_iscanf_r, (struct _reent *, const char *, ...));
330 int _EXFUN(_mkstemp_r, (struct _reent *, char *));
331 char * _EXFUN(_mktemp_r, (struct _reent *, char *));
332 void _EXFUN(_perror_r, (struct _reent *, const char *));
333 int _EXFUN(_printf_r, (struct _reent *, const char *, ...));
334 int _EXFUN(_putchar_r, (struct _reent *, int));
335 int _EXFUN(_puts_r, (struct _reent *, const char *));
336 int _EXFUN(_remove_r, (struct _reent *, const char *));
337 int _EXFUN(_rename_r, (struct _reent *,
338 const char *_old, const char *_new));
339 int _EXFUN(_scanf_r, (struct _reent *, const char *, ...));
340 int _EXFUN(_siprintf_r, (struct _reent *, char *, const char *, ...));
341 int _EXFUN(_siscanf_r, (struct _reent *, const char *, const char *, ...));
342 int _EXFUN(_sniprintf_r, (struct _reent *, char *, size_t, const char *, ...));
343 int _EXFUN(_snprintf_r, (struct _reent *, char *, size_t, const char *, ...));
344 int _EXFUN(_sprintf_r, (struct _reent *, char *, const char *, ...));
345 int _EXFUN(_sscanf_r, (struct _reent *, const char *, const char *, ...));
346 char * _EXFUN(_tempnam_r, (struct _reent *, const char *, const char *));
347 FILE * _EXFUN(_tmpfile_r, (struct _reent *));
348 char * _EXFUN(_tmpnam_r, (struct _reent *, char *));
349 int _EXFUN(_ungetc_r, (struct _reent *, int, FILE *));
350 int _EXFUN(_vasiprintf_r, (struct _reent *, char **, const char *, __VALIST));
351 int _EXFUN(_vasprintf_r, (struct _reent *, char **, const char *, __VALIST));
352 int _EXFUN(_vdprintf_r, (struct _reent *, int, const char *, __VALIST));
353 int _EXFUN(_vfiprintf_r, (struct _reent *, FILE *, const char *, __VALIST));
354 int _EXFUN(_vfprintf_r, (struct _reent *, FILE *, const char *, __VALIST));
355 int _EXFUN(_viprintf_r, (struct _reent *, const char *, __VALIST));
356 int _EXFUN(_vprintf_r, (struct _reent *, const char *, __VALIST));
357 int _EXFUN(_vsiprintf_r, (struct _reent *, char *, const char *, __VALIST));
358 int _EXFUN(_vsprintf_r, (struct _reent *, char *, const char *, __VALIST));
359 int _EXFUN(_vsniprintf_r, (struct _reent *, char *, size_t, const char *, __VALIST));
360 int _EXFUN(_vsnprintf_r, (struct _reent *, char *, size_t, const char *, __VALIST));
361 int _EXFUN(_vfiscanf_r, (struct _reent *, FILE *, const char *, __VALIST));
362 int _EXFUN(_vfscanf_r, (struct _reent *, FILE *, const char *, __VALIST));
363 int _EXFUN(_viscanf_r, (struct _reent *, const char *, __VALIST));
364 int _EXFUN(_vscanf_r, (struct _reent *, const char *, __VALIST));
365 int _EXFUN(_vsscanf_r, (struct _reent *, const char *, const char *, __VALIST));
366 int _EXFUN(_vsiscanf_r, (struct _reent *, const char *, const char *, __VALIST));
367
368 ssize_t _EXFUN(__getdelim, (char **, size_t *, int, FILE *));
369 ssize_t _EXFUN(__getline, (char **, size_t *, FILE *));
370
371 #ifdef __LARGE64_FILES
372 #if !defined(__CYGWIN__) || defined(_COMPILING_NEWLIB)
373 FILE * _EXFUN(fdopen64, (int, const char *));
374 FILE * _EXFUN(fopen64, (const char *, const char *));
375 _off64_t _EXFUN(ftello64, (FILE *));
376 _off64_t _EXFUN(fseeko64, (FILE *, _off64_t, int));
377 int _EXFUN(fgetpos64, (FILE *, _fpos64_t *));
378 int _EXFUN(fsetpos64, (FILE *, const _fpos64_t *));
379 FILE * _EXFUN(tmpfile64, (void));
380
381 FILE * _EXFUN(_fdopen64_r, (struct _reent *, int, const char *));
382 FILE * _EXFUN(_fopen64_r, (struct _reent *,const char *, const char *));
383 _off64_t _EXFUN(_ftello64_r, (struct _reent *, FILE *));
384 _off64_t _EXFUN(_fseeko64_r, (struct _reent *, FILE *, _off64_t, int));
385 int _EXFUN(_fgetpos64_r, (struct _reent *, FILE *, _fpos64_t *));
386 int _EXFUN(_fsetpos64_r, (struct _reent *, FILE *, const _fpos64_t *));
387 FILE * _EXFUN(_tmpfile64_r, (struct _reent *));
388 #endif /* !__CYGWIN__ */
389 #endif /* __LARGE64_FILES */
390
391 /*
392 * Routines internal to the implementation.
393 */
394
395 int _EXFUN(__srget, (FILE *));
396 int _EXFUN(__swbuf, (int, FILE *));
397
398 /*
399 * Stdio function-access interface.
400 */
401
402 #ifndef __STRICT_ANSI__
403 FILE *_EXFUN(funopen,(const _PTR _cookie,
404 int (*readfn)(_PTR _cookie, char *_buf, int _n),
405 int (*writefn)(_PTR _cookie, const char *_buf, int _n),
406 fpos_t (*seekfn)(_PTR _cookie, fpos_t _off, int _whence),
407 int (*closefn)(_PTR _cookie)));
408
409 #define fropen(cookie, fn) funopen(cookie, fn, (int (*)())0, (fpos_t (*)())0, (int (*)())0)
410 #define fwopen(cookie, fn) funopen(cookie, (int (*)())0, fn, (fpos_t (*)())0, (int (*)())0)
411 #endif
412
413 /*
414 * The __sfoo macros are here so that we can
415 * define function versions in the C library.
416 */
417 #define __sgetc_raw(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
418
419 #ifdef __SCLE
__sgetc(FILE * __p)420 static __inline__ int __sgetc(FILE *__p)
421 {
422 int __c = __sgetc_raw(__p);
423 if ((__p->_flags & __SCLE) && (__c == '\r'))
424 {
425 int __c2 = __sgetc_raw(__p);
426 if (__c2 == '\n')
427 __c = __c2;
428 else
429 ungetc(__c2, __p);
430 }
431 return __c;
432 }
433 #else
434 #define __sgetc(p) __sgetc_raw(p)
435 #endif
436
437 #ifdef _never /* __GNUC__ */
438 /* If this inline is actually used, then systems using coff debugging
439 info get hopelessly confused. 21sept93 rich@cygnus.com. */
__sputc(int _c,FILE * _p)440 static __inline int __sputc(int _c, FILE *_p) {
441 if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
442 return (*_p->_p++ = _c);
443 else
444 return (__swbuf(_c, _p));
445 }
446 #else
447 /*
448 * This has been tuned to generate reasonable code on the vax using pcc
449 */
450 #define __sputc_raw(c, p) \
451 (--(p)->_w < 0 ? \
452 (p)->_w >= (p)->_lbfsize ? \
453 (*(p)->_p = (c)), *(p)->_p != '\n' ? \
454 (int)*(p)->_p++ : \
455 __swbuf('\n', p) : \
456 __swbuf((int)(c), p) : \
457 (*(p)->_p = (c), (int)*(p)->_p++))
458 #ifdef __SCLE
459 #define __sputc(c, p) \
460 ((((p)->_flags & __SCLE) && ((c) == '\n')) \
461 ? __sputc_raw('\r', (p)) : 0 , \
462 __sputc_raw((c), (p)))
463 #else
464 #define __sputc(c, p) __sputc_raw(c, p)
465 #endif
466 #endif
467
468 #define __sfeof(p) (((p)->_flags & __SEOF) != 0)
469 #define __sferror(p) (((p)->_flags & __SERR) != 0)
470 #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
471 #define __sfileno(p) ((p)->_file)
472
473 #if !defined(AEONSIM) && !defined(XGEN)
474 #define feof(p) __sfeof(p)
475 #define ferror(p) __sferror(p)
476 #define clearerr(p) __sclearerr(p)
477 #endif /* !AEONSIM && !XGEN */
478
479 #if 0 /*ndef __STRICT_ANSI__ - FIXME: must initialize stdio first, use fn */
480 #define fileno(p) __sfileno(p)
481 #endif
482
483 #if !defined(__CYGWIN__) && !defined(AEONSIM) && !defined(XGEN)
484 #ifndef lint
485 #define getc(fp) __sgetc(fp)
486 #define putc(x, fp) __sputc(x, fp)
487 #endif /* lint */
488 #endif /* !__CYGWIN__ && !AEONSIM && !XGEN */
489
490 #if !defined(AEONSIM) && !defined(XGEN)
491 #define getchar() getc(stdin)
492 #define putchar(x) putc(x, stdout)
493 #endif /* !AEONSIM && !XGEN */
494
495 #ifndef __STRICT_ANSI__
496 /* fast always-buffered version, true iff error */
497 #define fast_putc(x,p) (--(p)->_w < 0 ? \
498 __swbuf((int)(x), p) == EOF : (*(p)->_p = (x), (p)->_p++, 0))
499
500 #define L_cuserid 9 /* posix says it goes in stdio.h :( */
501 #ifdef __CYGWIN__
502 #define L_ctermid 16
503 #endif
504 #endif
505
506 _END_STD_C
507
508 #endif /* _STDIO_H_ */
509