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