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 #ifndef _REENT_ONLY
147 #define	stdin	(_REENT->_stdin)
148 #define	stdout	(_REENT->_stdout)
149 #define	stderr	(_REENT->_stderr)
150 #else /* _REENT_ONLY */
151 #define	stdin	(_impure_ptr->_stdin)
152 #define	stdout	(_impure_ptr->_stdout)
153 #define	stderr	(_impure_ptr->_stderr)
154 #endif /* _REENT_ONLY */
155 
156 #define _stdin_r(x)	((x)->_stdin)
157 #define _stdout_r(x)	((x)->_stdout)
158 #define _stderr_r(x)	((x)->_stderr)
159 
160 /*
161  * Functions defined in ANSI C standard.
162  */
163 
164 #ifdef __GNUC__
165 #define __VALIST __gnuc_va_list
166 #else
167 #define __VALIST char*
168 #endif
169 
170 FILE *	_EXFUN(tmpfile, (void));
171 char *	_EXFUN(tmpnam, (char *));
172 int	_EXFUN(fclose, (FILE *));
173 int	_EXFUN(fflush, (FILE *));
174 FILE *	_EXFUN(freopen, (const char *, const char *, FILE *));
175 void	_EXFUN(setbuf, (FILE *, char *));
176 int	_EXFUN(setvbuf, (FILE *, char *, int, size_t));
177 int	_EXFUN(fprintf, (FILE *, const char *, ...));
178 int	_EXFUN(fscanf, (FILE *, const char *, ...));
179 int	_EXFUN(printf, (const char *, ...));
180 int	_EXFUN(scanf, (const char *, ...));
181 int	_EXFUN(sscanf, (const char *, const char *, ...));
182 int	_EXFUN(vfprintf, (FILE *, const char *, __VALIST));
183 int	_EXFUN(vprintf, (const char *, __VALIST));
184 int	_EXFUN(vsprintf, (char *, const char *, __VALIST));
185 int	_EXFUN(fgetc, (FILE *));
186 char *  _EXFUN(fgets, (char *, int, FILE *));
187 int	_EXFUN(fputc, (int, FILE *));
188 int	_EXFUN(fputs, (const char *, FILE *));
189 int	_EXFUN(getc, (FILE *));
190 int	_EXFUN(getchar, (void));
191 char *  _EXFUN(gets, (char *));
192 int	_EXFUN(putc, (int, FILE *));
193 int	_EXFUN(putchar, (int));
194 int	_EXFUN(puts, (const char *));
195 int	_EXFUN(ungetc, (int, FILE *));
196 size_t	_EXFUN(fread, (_PTR, size_t _size, size_t _n, FILE *));
197 size_t	_EXFUN(fwrite, (const _PTR , size_t _size, size_t _n, FILE *));
198 #ifdef _COMPILING_NEWLIB
199 int	_EXFUN(fgetpos, (FILE *, _fpos_t *));
200 #else
201 int	_EXFUN(fgetpos, (FILE *, fpos_t *));
202 #endif
203 int	_EXFUN(fseek, (FILE *, long, int));
204 #ifdef _COMPILING_NEWLIB
205 int	_EXFUN(fsetpos, (FILE *, const _fpos_t *));
206 #else
207 int	_EXFUN(fsetpos, (FILE *, const fpos_t *));
208 #endif
209 long	_EXFUN(ftell, ( FILE *));
210 void	_EXFUN(rewind, (FILE *));
211 void	_EXFUN(clearerr, (FILE *));
212 int	_EXFUN(feof, (FILE *));
213 int	_EXFUN(ferror, (FILE *));
214 void    _EXFUN(perror, (const char *));
215 #ifndef _REENT_ONLY
216 FILE *	_EXFUN(fopen, (const char *_name, const char *_type));
217 int	_EXFUN(sprintf, (char *, const char *, ...));
218 int	_EXFUN(remove, (const char *));
219 int	_EXFUN(rename, (const char *, const char *));
220 #endif
221 #ifndef __STRICT_ANSI__
222 int	_EXFUN(asprintf, (char **, const char *, ...));
223 #ifdef _COMPILING_NEWLIB
224 int	_EXFUN(fseeko, (FILE *, _off_t, int));
225 _off_t	_EXFUN(ftello, ( FILE *));
226 #else
227 int	_EXFUN(fseeko, (FILE *, off_t, int));
228 off_t	_EXFUN(ftello, ( FILE *));
229 #endif
230 int	_EXFUN(vfiprintf, (FILE *, const char *, __VALIST));
231 int	_EXFUN(iprintf, (const char *, ...));
232 int	_EXFUN(fiprintf, (FILE *, const char *, ...));
233 int	_EXFUN(siprintf, (char *, const char *, ...));
234 char *	_EXFUN(tempnam, (const char *, const char *));
235 int	_EXFUN(vasprintf, (char **, const char *, __VALIST));
236 int	_EXFUN(vsnprintf, (char *, size_t, const char *, __VALIST));
237 int	_EXFUN(vfscanf, (FILE *, const char *, __VALIST));
238 int	_EXFUN(vscanf, (const char *, __VALIST));
239 int	_EXFUN(vsscanf, (const char *, const char *, __VALIST));
240 #ifndef _REENT_ONLY
241 int	_EXFUN(fcloseall, (_VOID));
242 int	_EXFUN(snprintf, (char *, size_t, const char *, ...));
243 #endif
244 #endif
245 
246 /*
247  * Routines in POSIX 1003.1.
248  */
249 
250 #ifndef __STRICT_ANSI__
251 #ifndef _REENT_ONLY
252 FILE *	_EXFUN(fdopen, (int, const char *));
253 #endif
254 int	_EXFUN(fileno, (FILE *));
255 int	_EXFUN(getw, (FILE *));
256 int	_EXFUN(pclose, (FILE *));
257 FILE *  _EXFUN(popen, (const char *, const char *));
258 int	_EXFUN(putw, (int, FILE *));
259 void    _EXFUN(setbuffer, (FILE *, char *, int));
260 int	_EXFUN(setlinebuf, (FILE *));
261 int	_EXFUN(getc_unlocked, (FILE *));
262 int	_EXFUN(getchar_unlocked, (void));
263 void	_EXFUN(flockfile, (FILE *));
264 int	_EXFUN(ftrylockfile, (FILE *));
265 void	_EXFUN(funlockfile, (FILE *));
266 int	_EXFUN(putc_unlocked, (int, FILE *));
267 int	_EXFUN(putchar_unlocked, (int));
268 #endif
269 
270 /*
271  * Recursive versions of the above.
272  */
273 
274 int	_EXFUN(_asprintf_r, (struct _reent *, char **, const char *, ...));
275 int	_EXFUN(_fcloseall_r, (struct _reent *));
276 FILE *	_EXFUN(_fdopen_r, (struct _reent *, int, const char *));
277 FILE *	_EXFUN(_fopen_r, (struct _reent *, const char *, const char *));
278 int	_EXFUN(_fscanf_r, (struct _reent *, FILE *, const char *, ...));
279 int	_EXFUN(_getchar_r, (struct _reent *));
280 char *	_EXFUN(_gets_r, (struct _reent *, char *));
281 int	_EXFUN(_iprintf_r, (struct _reent *, const char *, ...));
282 int	_EXFUN(_mkstemp_r, (struct _reent *, char *));
283 char *	_EXFUN(_mktemp_r, (struct _reent *, char *));
284 void	_EXFUN(_perror_r, (struct _reent *, const char *));
285 int	_EXFUN(_printf_r, (struct _reent *, const char *, ...));
286 int	_EXFUN(_putchar_r, (struct _reent *, int));
287 int	_EXFUN(_puts_r, (struct _reent *, const char *));
288 int	_EXFUN(_remove_r, (struct _reent *, const char *));
289 int	_EXFUN(_rename_r, (struct _reent *,
290 			   const char *_old, const char *_new));
291 int	_EXFUN(_scanf_r, (struct _reent *, const char *, ...));
292 int	_EXFUN(_sprintf_r, (struct _reent *, char *, const char *, ...));
293 int	_EXFUN(_snprintf_r, (struct _reent *, char *, size_t, const char *, ...));
294 int	_EXFUN(_sscanf_r, (struct _reent *, const char *, const char *, ...));
295 char *	_EXFUN(_tempnam_r, (struct _reent *, const char *, const char *));
296 FILE *	_EXFUN(_tmpfile_r, (struct _reent *));
297 char *	_EXFUN(_tmpnam_r, (struct _reent *, char *));
298 int	_EXFUN(_vasprintf_r, (struct _reent *, char **, const char *, __VALIST));
299 int	_EXFUN(_vfprintf_r, (struct _reent *, FILE *, const char *, __VALIST));
300 int	_EXFUN(_vprintf_r, (struct _reent *, const char *, __VALIST));
301 int	_EXFUN(_vsprintf_r, (struct _reent *, char *, const char *, __VALIST));
302 int	_EXFUN(_vsnprintf_r, (struct _reent *, char *, size_t, const char *, __VALIST));
303 int	_EXFUN(_vfscanf_r, (struct _reent *, FILE *, const char *, __VALIST));
304 int	_EXFUN(_vscanf_r, (struct _reent *, const char *, __VALIST));
305 int	_EXFUN(_vsscanf_r, (struct _reent *, const char *, const char *, __VALIST));
306 
307 ssize_t _EXFUN(__getdelim, (char **, size_t *, int, FILE *));
308 ssize_t _EXFUN(__getline, (char **, size_t *, FILE *));
309 
310 #ifdef __LARGE64_FILES
311 #if !defined(__CYGWIN__) || defined(_COMPILING_NEWLIB)
312 FILE *	_EXFUN(fdopen64, (int, const char *));
313 FILE *  _EXFUN(fopen64, (const char *, const char *));
314 _off64_t _EXFUN(ftello64, (FILE *));
315 _off64_t _EXFUN(fseeko64, (FILE *, _off64_t, int));
316 int     _EXFUN(fgetpos64, (FILE *, _fpos64_t *));
317 int     _EXFUN(fsetpos64, (FILE *, const _fpos64_t *));
318 FILE *  _EXFUN(tmpfile64, (void));
319 
320 FILE *	_EXFUN(_fdopen64_r, (struct _reent *, int, const char *));
321 FILE *  _EXFUN(_fopen64_r, (struct _reent *,const char *, const char *));
322 _off64_t _EXFUN(_ftello64_r, (struct _reent *, FILE *));
323 _off64_t _EXFUN(_fseeko64_r, (struct _reent *, FILE *, _off64_t, int));
324 int     _EXFUN(_fgetpos64_r, (struct _reent *, FILE *, _fpos64_t *));
325 int     _EXFUN(_fsetpos64_r, (struct _reent *, FILE *, const _fpos64_t *));
326 FILE *  _EXFUN(_tmpfile64_r, (struct _reent *));
327 #endif /* !__CYGWIN__ */
328 #endif /* __LARGE64_FILES */
329 
330 /*
331  * Routines internal to the implementation.
332  */
333 
334 int	_EXFUN(__srget, (FILE *));
335 int	_EXFUN(__swbuf, (int, FILE *));
336 
337 /*
338  * Stdio function-access interface.
339  */
340 
341 #ifndef __STRICT_ANSI__
342 FILE	*_EXFUN(funopen,(const _PTR _cookie,
343 		int (*readfn)(_PTR _cookie, char *_buf, int _n),
344 		int (*writefn)(_PTR _cookie, const char *_buf, int _n),
345 		fpos_t (*seekfn)(_PTR _cookie, fpos_t _off, int _whence),
346 		int (*closefn)(_PTR _cookie)));
347 
348 #define	fropen(cookie, fn) funopen(cookie, fn, (int (*)())0, (fpos_t (*)())0, (int (*)())0)
349 #define	fwopen(cookie, fn) funopen(cookie, (int (*)())0, fn, (fpos_t (*)())0, (int (*)())0)
350 #endif
351 
352 /*
353  * The __sfoo macros are here so that we can
354  * define function versions in the C library.
355  */
356 #define       __sgetc_raw(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
357 
358 #ifdef __SCLE
__sgetc(FILE * __p)359 static __inline__ int __sgetc(FILE *__p)
360   {
361     int __c = __sgetc_raw(__p);
362     if ((__p->_flags & __SCLE) && (__c == '\r'))
363       {
364       int __c2 = __sgetc_raw(__p);
365       if (__c2 == '\n')
366         __c = __c2;
367       else
368         ungetc(__c2, __p);
369       }
370     return __c;
371   }
372 #else
373 #define __sgetc(p) __sgetc_raw(p)
374 #endif
375 
376 #ifdef _never /* __GNUC__ */
377 /* If this inline is actually used, then systems using coff debugging
378    info get hopelessly confused.  21sept93 rich@cygnus.com.  */
__sputc(int _c,FILE * _p)379 static __inline int __sputc(int _c, FILE *_p) {
380 	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
381 		return (*_p->_p++ = _c);
382 	else
383 		return (__swbuf(_c, _p));
384 }
385 #else
386 /*
387  * This has been tuned to generate reasonable code on the vax using pcc
388  */
389 #define       __sputc_raw(c, p) \
390 	(--(p)->_w < 0 ? \
391 		(p)->_w >= (p)->_lbfsize ? \
392 			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
393 				(int)*(p)->_p++ : \
394 				__swbuf('\n', p) : \
395 			__swbuf((int)(c), p) : \
396 		(*(p)->_p = (c), (int)*(p)->_p++))
397 #ifdef __SCLE
398 #define __sputc(c, p) \
399         ((((p)->_flags & __SCLE) && ((c) == '\n')) \
400           ? __sputc_raw('\r', (p)) : 0 , \
401         __sputc_raw((c), (p)))
402 #else
403 #define __sputc(c, p) __sputc_raw(c, p)
404 #endif
405 #endif
406 
407 #define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
408 #define	__sferror(p)	(((p)->_flags & __SERR) != 0)
409 #define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
410 #define	__sfileno(p)	((p)->_file)
411 
412 #define	feof(p)		__sfeof(p)
413 #define	ferror(p)	__sferror(p)
414 #define	clearerr(p)	__sclearerr(p)
415 
416 #if 0 /*ndef __STRICT_ANSI__ - FIXME: must initialize stdio first, use fn */
417 #define	fileno(p)	__sfileno(p)
418 #endif
419 
420 #ifndef __CYGWIN__
421 #ifndef lint
422 #define	getc(fp)	__sgetc(fp)
423 #define putc(x, fp)	__sputc(x, fp)
424 #endif /* lint */
425 #endif /* __CYGWIN__ */
426 
427 #define	getchar()	getc(stdin)
428 #define	putchar(x)	putc(x, stdout)
429 
430 #ifndef __STRICT_ANSI__
431 /* fast always-buffered version, true iff error */
432 #define	fast_putc(x,p) (--(p)->_w < 0 ? \
433 	__swbuf((int)(x), p) == EOF : (*(p)->_p = (x), (p)->_p++, 0))
434 
435 #define	L_cuserid	9		/* posix says it goes in stdio.h :( */
436 #ifdef __CYGWIN__
437 #define L_ctermid       16
438 #endif
439 #endif
440 
441 _END_STD_C
442 
443 #endif /* _STDIO_H_ */
444