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  * and/or 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 <sys/cdefs.h>
30 
31 #define	_FSTDIO			/* ``function stdio'' */
32 
33 #define __need_size_t
34 #define __need_NULL
35 #include <stddef.h>
36 
37 #include <stdarg.h>
38 #include <sys/_types.h>
39 
40 #ifndef __machine_flock_t_defined
41 #include <sys/lock.h>
42 typedef _LOCK_RECURSIVE_T _flock_t;
43 #endif
44 
45 _BEGIN_STD_C
46 
47 #ifndef ___FILE_DECLARED
48 typedef struct __file __FILE;
49 # define ___FILE_DECLARED
50 #endif
51 
52 #ifndef _FILE_DECLARED
53 typedef __FILE FILE;
54 #define _FILE_DECLARED
55 #endif
56 
57 /*
58  * Stdio buffers.
59  */
60 
61 struct __sbuf {
62 	unsigned char *_base;
63 	int	_size;
64 };
65 
66 /*
67  * Stdio state variables.
68  *
69  * The following always hold:
70  *
71  *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
72  *		_lbfsize is -_bf._size, else _lbfsize is 0
73  *	if _flags&__SRD, _w is 0
74  *	if _flags&__SWR, _r is 0
75  *
76  * This ensures that the getc and putc macros (or inline functions) never
77  * try to write or read from a file that is in `read' or `write' mode.
78  * (Moreover, they can, and do, automatically switch from read mode to
79  * write mode, and back, on "r+" and "w+" files.)
80  *
81  * _lbfsize is used only to make the inline line-buffered output stream
82  * code as compact as possible.
83  *
84  * _ub, _up, and _ur are used when ungetc() pushes back more characters
85  * than fit in the current _bf, or when ungetc() pushes back a character
86  * that does not match the previous one in _bf.  When this happens,
87  * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
88  * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
89  */
90 
91 #define _REENT_SMALL_CHECK_INIT(ptr) /* nothing */
92 
93 #ifdef __CUSTOM_FILE_IO__
94 
95 /* Get custom _FILE definition.  */
96 #include <sys/custom_file.h>
97 
98 #else /* !__CUSTOM_FILE_IO__ */
99 #ifndef __LARGE64_FILES
100 struct __file {
101   unsigned char *_p;	/* current position in (some) buffer */
102   int	_r;		/* read space left for getc() */
103   int	_w;		/* write space left for putc() */
104   short	_flags;		/* flags, below; this FILE is free if 0 */
105   short	_file;		/* fileno, if Unix descriptor, else -1 */
106   struct __sbuf _bf;	/* the buffer (at least 1 byte, if !NULL) */
107   int	_lbfsize;	/* 0 or -_bf._size, for inline putc */
108 
109   /* operations */
110   void *	_cookie;	/* cookie passed to io functions */
111 
112   _ssize_t (*_read) (void *,
113 					   char *, size_t);
114   _ssize_t (*_write) (void *,
115 					    const char *,
116 					    size_t);
117   _fpos_t (*_seek) (void *, _fpos_t, int);
118   int (*_close) (void *);
119 
120   /* separate buffer for long sequences of ungetc() */
121   struct __sbuf _ub;	/* ungetc buffer */
122   unsigned char *_up;	/* saved _p when _p is doing ungetc data */
123   int	_ur;		/* saved _r when _r is counting ungetc data */
124 
125   /* tricks to meet minimum requirements even when malloc() fails */
126   unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
127   unsigned char _nbuf[1];	/* guarantee a getc() buffer */
128 
129   /* separate buffer for fgetline() when line crosses buffer boundary */
130   struct __sbuf _lb;	/* buffer for fgetline() */
131 
132   /* Unix stdio files get aligned to block boundaries on fseek() */
133   int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
134   _off_t _offset;	/* current lseek offset */
135 
136 #ifndef __SINGLE_THREAD__
137   _flock_t _lock;	/* for thread-safety locking */
138 #endif
139   _mbstate_t _mbstate;	/* for wide char stdio functions. */
140   int   _flags2;        /* for future use */
141 };
142 #else
143 struct __file {
144   unsigned char *_p;	/* current position in (some) buffer */
145   int	_r;		/* read space left for getc() */
146   int	_w;		/* write space left for putc() */
147   short	_flags;		/* flags, below; this FILE is free if 0 */
148   short	_file;		/* fileno, if Unix descriptor, else -1 */
149   struct __sbuf _bf;	/* the buffer (at least 1 byte, if !NULL) */
150   int	_lbfsize;	/* 0 or -_bf._size, for inline putc */
151 
152   /* operations */
153   void *	_cookie;	/* cookie passed to io functions */
154 
155   _ssize_t (*_read) (void *,
156 					   char *, size_t);
157   _ssize_t (*_write) (void *,
158 					    const char *,
159 					    size_t);
160   _fpos_t (*_seek) (void *, _fpos_t, int);
161   int (*_close) (void *);
162 
163   /* separate buffer for long sequences of ungetc() */
164   struct __sbuf _ub;	/* ungetc buffer */
165   unsigned char *_up;	/* saved _p when _p is doing ungetc data */
166   int	_ur;		/* saved _r when _r is counting ungetc data */
167 
168   /* tricks to meet minimum requirements even when malloc() fails */
169   unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
170   unsigned char _nbuf[1];	/* guarantee a getc() buffer */
171 
172   /* separate buffer for fgetline() when line crosses buffer boundary */
173   struct __sbuf _lb;	/* buffer for fgetline() */
174 
175   /* Unix stdio files get aligned to block boundaries on fseek() */
176   int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
177   int   _flags2;        /* for future use */
178 
179   _off64_t _offset;     /* current lseek offset */
180   _fpos64_t (*_seek64) (void *, _fpos64_t, int);
181 
182 #ifndef __SINGLE_THREAD__
183   _flock_t _lock;	/* for thread-safety locking */
184 #endif
185   _mbstate_t _mbstate;	/* for wide char stdio functions. */
186 };
187 #endif /* __LARGE64_FILES */
188 #endif /* !__CUSTOM_FILE_IO__ */
189 
190 extern __FILE __sf[3];
191 
192 extern NEWLIB_THREAD_LOCAL __FILE *_tls_stdin;
193 #define _REENT_STDIN(_ptr) (_tls_stdin)
194 extern NEWLIB_THREAD_LOCAL __FILE *_tls_stdout;
195 #define _REENT_STDOUT(_ptr) (_tls_stdout)
196 extern NEWLIB_THREAD_LOCAL __FILE *_tls_stderr;
197 #define _REENT_STDERR(_ptr) (_tls_stderr)
198 
199 extern void (*__stdio_exit_handler) (void);
200 
201 struct _glue
202 {
203   struct _glue *_next;
204   int _niobs;
205   __FILE *_iobs;
206 };
207 
208 extern struct _glue __sglue;
209 
210 extern int _fwalk_sglue (int (*)(__FILE *), struct _glue *);
211 
212 typedef _fpos_t fpos_t;
213 #ifdef __LARGE64_FILES
214 typedef _fpos64_t fpos64_t;
215 #endif
216 
217 #if __POSIX_VISIBLE
218 
219 #ifndef _OFF_T_DECLARED
220 typedef __off_t off_t;
221 #define	_OFF_T_DECLARED
222 #endif
223 
224 #ifndef _SSIZE_T_DECLARED
225 typedef _ssize_t ssize_t;
226 #define	_SSIZE_T_DECLARED
227 #endif
228 
229 #endif
230 
231 #include <sys/stdio.h>
232 
233 #define	__SLBF	0x0001		/* line buffered */
234 #define	__SNBF	0x0002		/* unbuffered */
235 #define	__SRD	0x0004		/* OK to read */
236 #define	__SWR	0x0008		/* OK to write */
237 	/* RD and WR are never simultaneously asserted */
238 #define	__SRW	0x0010		/* open for reading & writing */
239 #define	__SEOF	0x0020		/* found EOF */
240 #define	__SERR	0x0040		/* found error */
241 #define	__SMBF	0x0080		/* _buf is from malloc */
242 #define	__SAPP	0x0100		/* fdopen()ed in append mode - so must  write to end */
243 #define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
244 #define	__SOPT	0x0400		/* do fseek() optimisation */
245 #define	__SNPT	0x0800		/* do not do fseek() optimisation */
246 #define	__SOFF	0x1000		/* set iff _offset is in fact correct */
247 #define	__SORD	0x2000		/* true => stream orientation (byte/wide) decided */
248 #define	__SL64	0x8000		/* is 64-bit offset large file */
249 
250 /* _flags2 flags */
251 #define	__SNLK  0x0001		/* stdio functions do not lock streams themselves */
252 #define	__SWID	0x2000		/* true => stream orientation wide, false => byte, only valid if __SORD in _flags is true */
253 
254 /*
255  * The following three definitions are for ANSI C, which took them
256  * from System V, which stupidly took internal interface macros and
257  * made them official arguments to setvbuf(), without renaming them.
258  * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
259  *
260  * Although these happen to match their counterparts above, the
261  * implementation does not rely on that (so these could be renumbered).
262  */
263 #define	_IOFBF	0		/* setvbuf should set fully buffered */
264 #define	_IOLBF	1		/* setvbuf should set line buffered */
265 #define	_IONBF	2		/* setvbuf should set unbuffered */
266 
267 #define	EOF	(-1)
268 
269 #ifdef __BUFSIZ__
270 #define	BUFSIZ		__BUFSIZ__
271 #else
272 #define	BUFSIZ		1024
273 #endif
274 
275 #ifdef __FOPEN_MAX__
276 #define FOPEN_MAX	__FOPEN_MAX__
277 #else
278 #define	FOPEN_MAX	20
279 #endif
280 
281 #ifdef __FILENAME_MAX__
282 #define FILENAME_MAX    __FILENAME_MAX__
283 #else
284 #define	FILENAME_MAX	1024
285 #endif
286 
287 #ifdef __L_tmpnam__
288 #define L_tmpnam	__L_tmpnam__
289 #else
290 #define	L_tmpnam	FILENAME_MAX
291 #endif
292 
293 #if __BSD_VISIBLE || __XSI_VISIBLE
294 #define P_tmpdir        "/tmp"
295 #endif
296 
297 #ifndef SEEK_SET
298 #define	SEEK_SET	0	/* set file offset to offset */
299 #endif
300 #ifndef SEEK_CUR
301 #define	SEEK_CUR	1	/* set file offset to current plus offset */
302 #endif
303 #ifndef SEEK_END
304 #define	SEEK_END	2	/* set file offset to EOF plus offset */
305 #endif
306 
307 #define	TMP_MAX		26
308 
309 #define	stdin	_REENT_STDIN(_REENT)
310 #define	stdout	_REENT_STDOUT(_REENT)
311 #define	stderr	_REENT_STDERR(_REENT)
312 
313 #define _stdin_r(x)	_REENT_STDIN(x)
314 #define _stdout_r(x)	_REENT_STDOUT(x)
315 #define _stderr_r(x)	_REENT_STDERR(x)
316 
317 /*
318  * Functions defined in ANSI C standard.
319  */
320 
321 #ifndef __VALIST
322 #ifdef __GNUC__
323 #define __VALIST __gnuc_va_list
324 #else
325 #define __VALIST char*
326 #endif
327 #endif
328 
329 #if __POSIX_VISIBLE
330 char *	ctermid (char *);
331 #endif
332 #if __GNU_VISIBLE || (__XSI_VISIBLE && __XSI_VISIBLE < 600)
333 char *	cuserid (char *);
334 #endif
335 FILE *	tmpfile (void);
336 char *	tmpnam (char *);
337 #if __BSD_VISIBLE || __XSI_VISIBLE || __POSIX_VISIBLE >= 200112
338 void	free (void *) _NOTHROW;
339 char *	tempnam (const char *, const char *) __malloc_like __result_use_check;
340 #endif
341 int	fclose (FILE *);
342 int	fflush (FILE *);
343 FILE *	freopen (const char *__restrict, const char *__restrict, FILE *__restrict);
344 void	setbuf (FILE *__restrict, char *__restrict);
345 int	setvbuf (FILE *__restrict, char *__restrict, int, size_t);
346 int	fprintf (FILE *__restrict, const char *__restrict, ...)
347                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
348 int	fscanf (FILE *__restrict, const char *__restrict, ...)
349                _ATTRIBUTE ((__format__ (__scanf__, 2, 3)));
350 int	printf (const char *__restrict, ...)
351                _ATTRIBUTE ((__format__ (__printf__, 1, 2)));
352 int	scanf (const char *__restrict, ...)
353                _ATTRIBUTE ((__format__ (__scanf__, 1, 2)));
354 int	sscanf (const char *__restrict, const char *__restrict, ...)
355                _ATTRIBUTE ((__format__ (__scanf__, 2, 3)));
356 int	vfprintf (FILE *__restrict, const char *__restrict, va_list)
357                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
358 int	vprintf (const char *, va_list)
359                _ATTRIBUTE ((__format__ (__printf__, 1, 0)));
360 int	vsprintf (char *__restrict, const char *__restrict, va_list)
361                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
362 int	fgetc (FILE *);
363 char *  fgets (char *__restrict, int, FILE *__restrict);
364 int	fputc (int, FILE *);
365 int	fputs (const char *__restrict, FILE *__restrict);
366 int	getc (FILE *);
367 int	getchar (void);
368 char *  gets (char *);
369 int	putc (int, FILE *);
370 int	putchar (int);
371 int	puts (const char *);
372 int	ungetc (int, FILE *);
373 size_t	fread (void *__restrict, size_t _size, size_t _n, FILE *__restrict);
374 size_t	fwrite (const void *__restrict , size_t _size, size_t _n, FILE *);
375 #ifdef _LIBC
376 int	fgetpos (FILE *, _fpos_t *);
377 #else
378 int	fgetpos (FILE *__restrict, fpos_t *__restrict);
379 #endif
380 int	fseek (FILE *, long, int);
381 #ifdef _LIBC
382 int	fsetpos (FILE *, const _fpos_t *);
383 #else
384 int	fsetpos (FILE *, const fpos_t *);
385 #endif
386 long	ftell ( FILE *);
387 void	rewind (FILE *);
388 void	clearerr (FILE *);
389 int	feof (FILE *);
390 int	ferror (FILE *);
391 void    perror (const char *);
392 #ifndef _REENT_ONLY
393 FILE *	fopen (const char *__restrict _name, const char *__restrict _type);
394 int	sprintf (char *__restrict, const char *__restrict, ...)
395                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
396 int	remove (const char *);
397 int	rename (const char *, const char *);
398 #ifdef _LIBC
399 int	_rename (const char *, const char *);
400 #endif
401 #endif
402 #if __LARGEFILE_VISIBLE || __POSIX_VISIBLE >= 200112
403 #ifdef _LIBC
404 int	fseeko (FILE *, _off_t, int);
405 _off_t	ftello (FILE *);
406 #else
407 int	fseeko (FILE *, off_t, int);
408 off_t	ftello (FILE *);
409 #endif
410 #endif
411 #if __GNU_VISIBLE
412 int	fcloseall (void);
413 #endif
414 #ifndef _REENT_ONLY
415 #if __ISO_C_VISIBLE >= 1999
416 int	snprintf (char *__restrict, size_t, const char *__restrict, ...)
417                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
418 int	vsnprintf (char *__restrict, size_t, const char *__restrict, va_list)
419                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
420 int	vfscanf (FILE *__restrict, const char *__restrict, va_list)
421                _ATTRIBUTE ((__format__ (__scanf__, 2, 0)));
422 int	vscanf (const char *, va_list)
423                _ATTRIBUTE ((__format__ (__scanf__, 1, 0)));
424 int	vsscanf (const char *__restrict, const char *__restrict, va_list)
425                _ATTRIBUTE ((__format__ (__scanf__, 2, 0)));
426 #endif
427 #if __GNU_VISIBLE
428 int	asprintf (char **__restrict, const char *__restrict, ...)
429                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
430 int	vasprintf (char **, const char *, va_list)
431                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
432 #endif
433 #if __MISC_VISIBLE /* Newlib-specific */
434 int	asiprintf (char **, const char *, ...)
435                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
436 char *	asniprintf (char *, size_t *, const char *, ...)
437                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
438 char *	asnprintf (char *__restrict, size_t *__restrict, const char *__restrict, ...)
439                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
440 #ifndef diprintf
441 int	diprintf (int, const char *, ...)
442                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
443 #endif
444 int	fiprintf (FILE *, const char *, ...)
445                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
446 #define __i_fprintf fiprintf
447 int	fiscanf (FILE *, const char *, ...)
448                _ATTRIBUTE ((__format__ (__scanf__, 2, 3)));
449 int	iprintf (const char *, ...)
450                _ATTRIBUTE ((__format__ (__printf__, 1, 2)));
451 int	iscanf (const char *, ...)
452                _ATTRIBUTE ((__format__ (__scanf__, 1, 2)));
453 int	siprintf (char *, const char *, ...)
454                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
455 #define __i_sprintf siprintf
456 int	siscanf (const char *, const char *, ...)
457                _ATTRIBUTE ((__format__ (__scanf__, 2, 3)));
458 int	sniprintf (char *, size_t, const char *, ...)
459                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
460 #define  __i_snprintf sniprintf
461 int	vasiprintf (char **, const char *, va_list)
462                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
463 char *	vasniprintf (char *, size_t *, const char *, va_list)
464                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
465 char *	vasnprintf (char *, size_t *, const char *, va_list)
466                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
467 int	vdiprintf (int, const char *, va_list)
468                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
469 int	vfiprintf (FILE *, const char *, va_list)
470                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
471 int	vfiscanf (FILE *, const char *, va_list)
472                _ATTRIBUTE ((__format__ (__scanf__, 2, 0)));
473 int	viprintf (const char *, va_list)
474                _ATTRIBUTE ((__format__ (__printf__, 1, 0)));
475 int	viscanf (const char *, va_list)
476                _ATTRIBUTE ((__format__ (__scanf__, 1, 0)));
477 int	vsiprintf (char *, const char *, va_list)
478                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
479 int	vsiscanf (const char *, const char *, va_list)
480                _ATTRIBUTE ((__format__ (__scanf__, 2, 0)));
481 int	vsniprintf (char *, size_t, const char *, va_list)
482                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
483 #endif /* __MISC_VISIBLE */
484 #endif /* !_REENT_ONLY */
485 
486 /*
487  * Routines in POSIX 1003.1:2001.
488  */
489 
490 #if __POSIX_VISIBLE
491 #ifndef _REENT_ONLY
492 FILE *	fdopen (int, const char *);
493 #endif
494 int	fileno (FILE *);
495 #endif
496 #if __MISC_VISIBLE || __POSIX_VISIBLE >= 199209
497 int	pclose (FILE *);
498 FILE *  popen (const char *, const char *);
499 #endif
500 
501 #if __BSD_VISIBLE
502 void    setbuffer (FILE *, char *, int);
503 int	setlinebuf (FILE *);
504 #endif
505 
506 #if __MISC_VISIBLE || (__XSI_VISIBLE && __POSIX_VISIBLE < 200112)
507 int	getw (FILE *);
508 int	putw (int, FILE *);
509 #endif
510 #if __MISC_VISIBLE || __POSIX_VISIBLE
511 int	getc_unlocked (FILE *);
512 int	getchar_unlocked (void);
513 void	flockfile (FILE *);
514 int	ftrylockfile (FILE *);
515 void	funlockfile (FILE *);
516 int	putc_unlocked (int, FILE *);
517 int	putchar_unlocked (int);
518 #endif
519 
520 /*
521  * Routines in POSIX 1003.1:200x.
522  */
523 
524 #if __POSIX_VISIBLE >= 200809
525 # ifndef _REENT_ONLY
526 #  ifndef dprintf
527 int	dprintf (int, const char *__restrict, ...)
528                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
529 #  endif
530 FILE *	fmemopen (void *__restrict, size_t, const char *__restrict);
531 /* getdelim - see __getdelim for now */
532 /* getline - see __getline for now */
533 FILE *	open_memstream (char **, size_t *);
534 int	vdprintf (int, const char *__restrict, va_list)
535                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
536 # endif
537 #endif
538 #if __ATFILE_VISIBLE
539 int	renameat (int, const char *, int, const char *);
540 #endif
541 
542 /* Other extensions.  */
543 
544 #if __BSD_VISIBLE
545 int	fpurge (FILE *);
546 #endif
547 
548 #if __MISC_VISIBLE
549 _ssize_t __getdelim (char **, size_t *, int, FILE *);
550 _ssize_t __getline (char **, size_t *, FILE *);
551 void	clearerr_unlocked (FILE *);
552 int	feof_unlocked (FILE *);
553 int	ferror_unlocked (FILE *);
554 int	fileno_unlocked (FILE *);
555 int	fflush_unlocked (FILE *);
556 int	fgetc_unlocked (FILE *);
557 int	fputc_unlocked (int, FILE *);
558 size_t	fread_unlocked (void *__restrict, size_t _size, size_t _n, FILE *__restrict);
559 size_t	fwrite_unlocked (const void *__restrict , size_t _size, size_t _n, FILE *);
560 #endif
561 
562 #if __GNU_VISIBLE
563 char *  fgets_unlocked (char *__restrict, int, FILE *__restrict);
564 int	fputs_unlocked (const char *__restrict, FILE *__restrict);
565 #endif
566 
567 #ifdef __LARGE64_FILES
568 #if __LARGEFILE64_VISIBLE
569 FILE *	fdopen64 (int, const char *);
570 FILE *  fopen64 (const char *, const char *);
571 FILE *  freopen64 (const char *, const char *, FILE *);
572 _off64_t ftello64 (FILE *);
573 _off64_t fseeko64 (FILE *, _off64_t, int);
574 int     fgetpos64 (FILE *, _fpos64_t *);
575 int     fsetpos64 (FILE *, const _fpos64_t *);
576 FILE *  tmpfile64 (void);
577 
578 #endif /* __LARGEFILE64_VISIBLE */
579 #endif /* __LARGE64_FILES */
580 
581 /*
582  * Routines internal to the implementation.
583  */
584 
585 int	_srget ( FILE *);
586 int	_swbuf ( int, FILE *);
587 
588 /*
589  * Stdio function-access interface.
590  */
591 
592 #if __BSD_VISIBLE
593 # ifdef __LARGE64_FILES
594 FILE	*funopen (const void *__cookie,
595 		int (*__readfn)(void *__c, char *__buf,
596 				size_t __n),
597 		int (*__writefn)(void *__c, const char *__buf,
598 				 size_t __n),
599 		_fpos64_t (*__seekfn)(void *__c, _fpos64_t __off, int __whence),
600 		int (*__closefn)(void *__c));
601 FILE	*funopen ( const void *__cookie,
602 		int (*__readfn)(void *__c, char *__buf,
603 				size_t __n),
604 		int (*__writefn)(void *__c, const char *__buf,
605 				 size_t __n),
606 		_fpos64_t (*__seekfn)(void *__c, _fpos64_t __off, int __whence),
607 		int (*__closefn)(void *__c));
608 # else
609 FILE	*funopen (const void *__cookie,
610 		int (*__readfn)(void *__cookie, char *__buf,
611 				size_t __n),
612 		int (*__writefn)(void *__cookie, const char *__buf,
613 				 size_t __n),
614 		fpos_t (*__seekfn)(void *__cookie, fpos_t __off, int __whence),
615 		int (*__closefn)(void *__cookie));
616 FILE	*funopen ( const void *__cookie,
617 		int (*__readfn)(void *__cookie, char *__buf,
618 				size_t __n),
619 		int (*__writefn)(void *__cookie, const char *__buf,
620 				 size_t __n),
621 		fpos_t (*__seekfn)(void *__cookie, fpos_t __off, int __whence),
622 		int (*__closefn)(void *__cookie));
623 # endif /* !__LARGE64_FILES */
624 
625 # define	fropen(__cookie, __fn) funopen(__cookie, __fn, NULL, NULL, NULL)
626 # define	fwopen(__cookie, __fn) funopen(__cookie, NULL, __fn, NULL, NULL)
627 #endif /* __BSD_VISIBLE */
628 
629 #if __GNU_VISIBLE
630 typedef ssize_t cookie_read_function_t(void *__cookie, char *__buf, size_t __n);
631 typedef ssize_t cookie_write_function_t(void *__cookie, const char *__buf,
632 					size_t __n);
633 # ifdef __LARGE64_FILES
634 typedef int cookie_seek_function_t(void *__cookie, _off64_t *__off,
635 				   int __whence);
636 # else
637 typedef int cookie_seek_function_t(void *__cookie, off_t *__off, int __whence);
638 # endif /* !__LARGE64_FILES */
639 typedef int cookie_close_function_t(void *__cookie);
640 typedef struct
641 {
642   /* These four struct member names are dictated by Linux; hopefully,
643      they don't conflict with any macros.  */
644   cookie_read_function_t  *read;
645   cookie_write_function_t *write;
646   cookie_seek_function_t  *seek;
647   cookie_close_function_t *close;
648 } cookie_io_functions_t;
649 FILE *fopencookie (void *__cookie,
650 		const char *__mode, cookie_io_functions_t __functions);
651 FILE *fopencookie ( void *__cookie,
652 		const char *__mode, cookie_io_functions_t __functions);
653 #endif /* __GNU_VISIBLE */
654 
655 #ifndef __CUSTOM_FILE_IO__
656 /*
657  * The __sfoo macros are here so that we can
658  * define function versions in the C library.
659  */
660 #define       _sgetc_raw( __f) (--(__f)->_r < 0 ? _srget( __f) : (int)(*(__f)->_p++))
661 
662 #ifdef __SCLE
663 /*  For a platform with CR/LF, additional logic is required by
664   __sgetc_r which would otherwise simply be a macro; therefore we
665   use an inlined function.  The function is only meant to be inlined
666   in place as used and the function body should never be emitted.
667 
668   There are two possible means to this end when compiling with GCC,
669   one when compiling with a standard C99 compiler, and for other
670   compilers we're just stuck.  At the moment, this issue only
671   affects the Cygwin target, so we'll most likely be using GCC. */
672 
673 _ELIDABLE_INLINE int _sgetc( FILE *__p);
674 
_sgetc(FILE * __p)675 _ELIDABLE_INLINE int _sgetc( FILE *__p)
676   {
677     int __c = _sgetc_raw( __p);
678     if ((__p->_flags & __SCLE) && (__c == '\r'))
679       {
680       int __c2 = _sgetc_raw( __p);
681       if (__c2 == '\n')
682         __c = __c2;
683       else
684         ungetc(__c2, __p);
685       }
686     return __c;
687   }
688 #else
689 #define _sgetc( __p) _sgetc_raw( __p)
690 #endif
691 
692 #ifdef __GNUC__
_sputc(int _c,FILE * _p)693 _ELIDABLE_INLINE int _sputc( int _c, FILE *_p) {
694 #ifdef __SCLE
695 	if ((_p->_flags & __SCLE) && _c == '\n')
696 	  _sputc ( '\r', _p);
697 #endif
698 	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
699                 return (*_p->_p++ = (unsigned char) _c);
700 	else
701 		return (_swbuf( _c, _p));
702 }
703 #else
704 /*
705  * This has been tuned to generate reasonable code on the vax using pcc
706  */
707 #define       _sputc_raw( __c, __p) \
708 	(--(__p)->_w < 0 ? \
709 		(__p)->_w >= (__p)->_lbfsize ? \
710 			(*(__p)->_p = (__c)), *(__p)->_p != '\n' ? \
711 				(int)*(__p)->_p++ : \
712 				_swbuf( '\n', __p) : \
713 			_swbuf( (int)(__c), __p) : \
714 		(*(__p)->_p = (__c), (int)*(__p)->_p++))
715 #ifdef __SCLE
716 #define _sputc( __c, __p) \
717         ((((__p)->_flags & __SCLE) && ((__c) == '\n')) \
718           ? _sputc_raw( '\r', (__p)) : 0 , \
719         _sputc_raw( (__c), (__p)))
720 #else
721 #define _sputc( __c, __p) _sputc_raw( __c, __p)
722 #endif
723 #endif
724 
725 #define	__sfeof(p)	((int)(((p)->_flags & __SEOF) != 0))
726 #define	__sferror(p)	((int)(((p)->_flags & __SERR) != 0))
727 #define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
728 #define	__sfileno(p)	((p)->_file)
729 
730 #ifndef __cplusplus
731 #ifndef _REENT_SMALL
732 #define	feof(p)		__sfeof(p)
733 #define	ferror(p)	__sferror(p)
734 #define	clearerr(p)	__sclearerr(p)
735 
736 #if __MISC_VISIBLE
737 #define	feof_unlocked(p)	__sfeof(p)
738 #define	ferror_unlocked(p)	__sferror(p)
739 #define	clearerr_unlocked(p)	__sclearerr(p)
740 #endif /* __MISC_VISIBLE */
741 #endif /* _REENT_SMALL */
742 
743 #if 0 /* __POSIX_VISIBLE - FIXME: must initialize stdio first, use fn */
744 #define	fileno(p)	__sfileno(p)
745 #endif
746 
747 static __inline int
_getchar_unlocked(void)748 _getchar_unlocked(void)
749 {
750     return (_sgetc(stdin));
751 }
752 
753 static __inline int
_putchar_unlocked(int _c)754 _putchar_unlocked(int _c)
755 {
756 	return (_sputc( _c, stdout));
757 }
758 
759 #ifdef __SINGLE_THREAD__
760 #define	getc(_p)	_sgetc( _p)
761 #define	putc(_c, _p)	_sputc( _c, _p)
762 #define	getchar()	_getchar_unlocked()
763 #define	putchar(_c)	_putchar_unlocked(_c)
764 #endif /* __SINGLE_THREAD__ */
765 
766 #if __MISC_VISIBLE || __POSIX_VISIBLE
767 #define	getchar_unlocked()	_getchar_unlocked()
768 #define	putchar_unlocked(_c)	_putchar_unlocked(_c)
769 #endif
770 #endif /* __cplusplus */
771 
772 #if __MISC_VISIBLE
773 /* fast always-buffered version, true iff error */
774 #define	fast_putc(x,p) (--(p)->_w < 0 ? \
775 	_swbuf( (int)(x), p) == EOF : (*(p)->_p = (x), (p)->_p++, 0))
776 #endif
777 
778 #if __GNU_VISIBLE || (__XSI_VISIBLE && __XSI_VISIBLE < 600)
779 #define	L_cuserid	9		/* posix says it goes in stdio.h :( */
780 #endif
781 #if __POSIX_VISIBLE
782 #define L_ctermid       16
783 #endif
784 
785 #else /* __CUSTOM_FILE_IO__ */
786 
787 #define	getchar()	getc(stdin)
788 #define	putchar(x)	putc(x, stdout)
789 
790 #if __MISC_VISIBLE || __POSIX_VISIBLE
791 #define	getchar_unlocked()	getc_unlocked(stdin)
792 #define	putchar_unlocked(x)	putc_unlocked(x, stdout)
793 #endif
794 
795 #endif /* !__CUSTOM_FILE_IO__ */
796 
797 #if __STDC_WANT_LIB_EXT1__ == 1
798 #include <sys/_types.h>
799 
800 #ifndef _ERRNO_T_DEFINED
801 typedef __errno_t errno_t;
802 #define _ERRNO_T_DEFINED
803 #endif
804 
805 #ifndef _RSIZE_T_DEFINED
806 typedef __rsize_t rsize_t;
807 #define _RSIZE_T_DEFINED
808 #endif
809 #endif
810 
811 _END_STD_C
812 
813 #if __SSP_FORTIFY_LEVEL > 0
814 #include <ssp/stdio.h>
815 #endif
816 
817 #endif /* _STDIO_H_ */
818