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 #define __need_NULL
35 #include <sys/cdefs.h>
36 #include <stddef.h>
37 
38 /* typedef only __gnuc_va_list, used throughout the header */
39 #define __need___va_list
40 #include <stdarg.h>
41 
42 /* typedef va_list only when required */
43 #if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
44 #ifdef __GNUC__
45 #ifndef _VA_LIST_DEFINED
46 typedef __gnuc_va_list va_list;
47 #define _VA_LIST_DEFINED
48 #endif
49 #else /* !__GNUC__ */
50 #include <stdarg.h>
51 #endif
52 #endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
53 
54 /*
55  * <sys/reent.h> defines __FILE, _fpos_t.
56  * They must be defined there because struct _reent needs them (and we don't
57  * want reent.h to include this file.
58  */
59 
60 #include <sys/reent.h>
61 #include <sys/types.h>
62 
63 _BEGIN_STD_C
64 
65 #if !defined(__FILE_defined)
66 typedef __FILE FILE;
67 # define __FILE_defined
68 #endif
69 
70 #ifdef __CYGWIN__
71 typedef _fpos64_t fpos_t;
72 #else
73 typedef _fpos_t fpos_t;
74 #ifdef __LARGE64_FILES
75 typedef _fpos64_t fpos64_t;
76 #endif
77 #endif /* !__CYGWIN__ */
78 
79 #include <sys/stdio.h>
80 
81 #define	__SLBF	0x0001		/* line buffered */
82 #define	__SNBF	0x0002		/* unbuffered */
83 #define	__SRD	0x0004		/* OK to read */
84 #define	__SWR	0x0008		/* OK to write */
85 	/* RD and WR are never simultaneously asserted */
86 #define	__SRW	0x0010		/* open for reading & writing */
87 #define	__SEOF	0x0020		/* found EOF */
88 #define	__SERR	0x0040		/* found error */
89 #define	__SMBF	0x0080		/* _buf is from malloc */
90 #define	__SAPP	0x0100		/* fdopen()ed in append mode - so must  write to end */
91 #define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
92 #define	__SOPT	0x0400		/* do fseek() optimisation */
93 #define	__SNPT	0x0800		/* do not do fseek() optimisation */
94 #define	__SOFF	0x1000		/* set iff _offset is in fact correct */
95 #define	__SORD	0x2000		/* true => stream orientation (byte/wide) decided */
96 #if defined(__CYGWIN__)
97 #  define __SCLE  0x4000        /* convert line endings CR/LF <-> NL */
98 #endif
99 #define	__SL64	0x8000		/* is 64-bit offset large file */
100 
101 /* _flags2 flags */
102 #define	__SNLK  0x0001		/* stdio functions do not lock streams themselves */
103 #define	__SWID	0x2000		/* true => stream orientation wide, false => byte, only valid if __SORD in _flags is true */
104 
105 /*
106  * The following three definitions are for ANSI C, which took them
107  * from System V, which stupidly took internal interface macros and
108  * made them official arguments to setvbuf(), without renaming them.
109  * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
110  *
111  * Although these happen to match their counterparts above, the
112  * implementation does not rely on that (so these could be renumbered).
113  */
114 #define	_IOFBF	0		/* setvbuf should set fully buffered */
115 #define	_IOLBF	1		/* setvbuf should set line buffered */
116 #define	_IONBF	2		/* setvbuf should set unbuffered */
117 
118 #define	EOF	(-1)
119 
120 #ifdef __BUFSIZ__
121 #define	BUFSIZ		__BUFSIZ__
122 #else
123 #define	BUFSIZ		1024
124 #endif
125 
126 #ifdef __FOPEN_MAX__
127 #define FOPEN_MAX	__FOPEN_MAX__
128 #else
129 #define	FOPEN_MAX	20
130 #endif
131 
132 #ifdef __FILENAME_MAX__
133 #define FILENAME_MAX    __FILENAME_MAX__
134 #else
135 #define	FILENAME_MAX	1024
136 #endif
137 
138 #ifdef __L_tmpnam__
139 #define L_tmpnam	__L_tmpnam__
140 #else
141 #define	L_tmpnam	FILENAME_MAX
142 #endif
143 
144 #if __BSD_VISIBLE || __XSI_VISIBLE
145 #define P_tmpdir        "/tmp"
146 #endif
147 
148 #ifndef SEEK_SET
149 #define	SEEK_SET	0	/* set file offset to offset */
150 #endif
151 #ifndef SEEK_CUR
152 #define	SEEK_CUR	1	/* set file offset to current plus offset */
153 #endif
154 #ifndef SEEK_END
155 #define	SEEK_END	2	/* set file offset to EOF plus offset */
156 #endif
157 
158 #define	TMP_MAX		26
159 
160 #define	stdin	(_REENT->_stdin)
161 #define	stdout	(_REENT->_stdout)
162 #define	stderr	(_REENT->_stderr)
163 
164 #define _stdin_r(x)	((x)->_stdin)
165 #define _stdout_r(x)	((x)->_stdout)
166 #define _stderr_r(x)	((x)->_stderr)
167 
168 /*
169  * Functions defined in ANSI C standard.
170  */
171 
172 #ifndef __VALIST
173 #ifdef __GNUC__
174 #define __VALIST __gnuc_va_list
175 #else
176 #define __VALIST char*
177 #endif
178 #endif
179 
180 #if __POSIX_VISIBLE
181 char *	ctermid (char *);
182 #endif
183 #if __GNU_VISIBLE || (__XSI_VISIBLE && __XSI_VISIBLE < 600)
184 char *	cuserid (char *);
185 #endif
186 FILE *	tmpfile (void);
187 char *	tmpnam (char *);
188 #if __BSD_VISIBLE || __XSI_VISIBLE || __POSIX_VISIBLE >= 200112
189 char *	tempnam (const char *, const char *) __malloc_like __result_use_check;
190 #endif
191 int	fclose (FILE *);
192 int	fflush (FILE *);
193 FILE *	freopen (const char *__restrict, const char *__restrict, FILE *__restrict);
194 void	setbuf (FILE *__restrict, char *__restrict);
195 int	setvbuf (FILE *__restrict, char *__restrict, int, size_t);
196 int	fprintf (FILE *__restrict, const char *__restrict, ...)
197                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
198 int	fscanf (FILE *__restrict, const char *__restrict, ...)
199                _ATTRIBUTE ((__format__ (__scanf__, 2, 3)));
200 int	printf (const char *__restrict, ...)
201                _ATTRIBUTE ((__format__ (__printf__, 1, 2)));
202 int	scanf (const char *__restrict, ...)
203                _ATTRIBUTE ((__format__ (__scanf__, 1, 2)));
204 int	sscanf (const char *__restrict, const char *__restrict, ...)
205                _ATTRIBUTE ((__format__ (__scanf__, 2, 3)));
206 int	vfprintf (FILE *__restrict, const char *__restrict, __VALIST)
207                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
208 int	vprintf (const char *, __VALIST)
209                _ATTRIBUTE ((__format__ (__printf__, 1, 0)));
210 int	vsprintf (char *__restrict, const char *__restrict, __VALIST)
211                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
212 int	fgetc (FILE *);
213 char *  fgets (char *__restrict, int, FILE *__restrict);
214 int	fputc (int, FILE *);
215 int	fputs (const char *__restrict, FILE *__restrict);
216 int	getc (FILE *);
217 int	getchar (void);
218 char *  gets (char *);
219 int	putc (int, FILE *);
220 int	putchar (int);
221 int	puts (const char *);
222 int	ungetc (int, FILE *);
223 size_t	fread (void *__restrict, size_t _size, size_t _n, FILE *__restrict);
224 size_t	fwrite (const void *__restrict , size_t _size, size_t _n, FILE *);
225 #ifdef _COMPILING_NEWLIB
226 int	fgetpos (FILE *, _fpos_t *);
227 #else
228 int	fgetpos (FILE *__restrict, fpos_t *__restrict);
229 #endif
230 int	fseek (FILE *, long, int);
231 #ifdef _COMPILING_NEWLIB
232 int	fsetpos (FILE *, const _fpos_t *);
233 #else
234 int	fsetpos (FILE *, const fpos_t *);
235 #endif
236 long	ftell ( FILE *);
237 void	rewind (FILE *);
238 void	clearerr (FILE *);
239 int	feof (FILE *);
240 int	ferror (FILE *);
241 void    perror (const char *);
242 #ifndef _REENT_ONLY
243 FILE *	fopen (const char *__restrict _name, const char *__restrict _type);
244 int	sprintf (char *__restrict, const char *__restrict, ...)
245                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
246 int	remove (const char *);
247 int	rename (const char *, const char *);
248 #ifdef _COMPILING_NEWLIB
249 int	_rename (const char *, const char *);
250 #endif
251 #endif
252 #if __LARGEFILE_VISIBLE || __POSIX_VISIBLE >= 200112
253 #ifdef _COMPILING_NEWLIB
254 int	fseeko (FILE *, _off_t, int);
255 _off_t	ftello (FILE *);
256 #else
257 int	fseeko (FILE *, off_t, int);
258 off_t	ftello (FILE *);
259 #endif
260 #endif
261 #if __GNU_VISIBLE
262 int	fcloseall (void);
263 #endif
264 #ifndef _REENT_ONLY
265 #if __ISO_C_VISIBLE >= 1999
266 int	snprintf (char *__restrict, size_t, const char *__restrict, ...)
267                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
268 int	vsnprintf (char *__restrict, size_t, const char *__restrict, __VALIST)
269                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
270 int	vfscanf (FILE *__restrict, const char *__restrict, __VALIST)
271                _ATTRIBUTE ((__format__ (__scanf__, 2, 0)));
272 int	vscanf (const char *, __VALIST)
273                _ATTRIBUTE ((__format__ (__scanf__, 1, 0)));
274 int	vsscanf (const char *__restrict, const char *__restrict, __VALIST)
275                _ATTRIBUTE ((__format__ (__scanf__, 2, 0)));
276 #endif
277 #if __GNU_VISIBLE
278 int	asprintf (char **__restrict, const char *__restrict, ...)
279                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
280 int	vasprintf (char **, const char *, __VALIST)
281                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
282 #endif
283 #if __MISC_VISIBLE /* Newlib-specific */
284 int	asiprintf (char **, const char *, ...)
285                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
286 char *	asniprintf (char *, size_t *, const char *, ...)
287                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
288 char *	asnprintf (char *__restrict, size_t *__restrict, const char *__restrict, ...)
289                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
290 #ifndef diprintf
291 int	diprintf (int, const char *, ...)
292                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
293 #endif
294 int	fiprintf (FILE *, const char *, ...)
295                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
296 #define __i_fprintf fiprintf
297 int	fiscanf (FILE *, const char *, ...)
298                _ATTRIBUTE ((__format__ (__scanf__, 2, 3)));
299 int	iprintf (const char *, ...)
300                _ATTRIBUTE ((__format__ (__printf__, 1, 2)));
301 int	iscanf (const char *, ...)
302                _ATTRIBUTE ((__format__ (__scanf__, 1, 2)));
303 int	siprintf (char *, const char *, ...)
304                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
305 #define __i_sprintf siprintf
306 int	siscanf (const char *, const char *, ...)
307                _ATTRIBUTE ((__format__ (__scanf__, 2, 3)));
308 int	sniprintf (char *, size_t, const char *, ...)
309                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
310 #define  __i_snprintf sniprintf
311 int	vasiprintf (char **, const char *, __VALIST)
312                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
313 char *	vasniprintf (char *, size_t *, const char *, __VALIST)
314                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
315 char *	vasnprintf (char *, size_t *, const char *, __VALIST)
316                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
317 int	vdiprintf (int, const char *, __VALIST)
318                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
319 int	vfiprintf (FILE *, const char *, __VALIST)
320                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
321 int	vfiscanf (FILE *, const char *, __VALIST)
322                _ATTRIBUTE ((__format__ (__scanf__, 2, 0)));
323 int	viprintf (const char *, __VALIST)
324                _ATTRIBUTE ((__format__ (__printf__, 1, 0)));
325 int	viscanf (const char *, __VALIST)
326                _ATTRIBUTE ((__format__ (__scanf__, 1, 0)));
327 int	vsiprintf (char *, const char *, __VALIST)
328                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
329 int	vsiscanf (const char *, const char *, __VALIST)
330                _ATTRIBUTE ((__format__ (__scanf__, 2, 0)));
331 int	vsniprintf (char *, size_t, const char *, __VALIST)
332                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
333 #endif /* __MISC_VISIBLE */
334 #endif /* !_REENT_ONLY */
335 
336 /*
337  * Routines in POSIX 1003.1:2001.
338  */
339 
340 #if __POSIX_VISIBLE
341 #ifndef _REENT_ONLY
342 FILE *	fdopen (int, const char *);
343 #endif
344 int	fileno (FILE *);
345 #endif
346 #if __MISC_VISIBLE || __POSIX_VISIBLE >= 199209
347 int	pclose (FILE *);
348 FILE *  popen (const char *, const char *);
349 #endif
350 
351 #if __BSD_VISIBLE
352 void    setbuffer (FILE *, char *, int);
353 int	setlinebuf (FILE *);
354 #endif
355 
356 #if __MISC_VISIBLE || (__XSI_VISIBLE && __POSIX_VISIBLE < 200112)
357 int	getw (FILE *);
358 int	putw (int, FILE *);
359 #endif
360 #if __MISC_VISIBLE || __POSIX_VISIBLE
361 int	getc_unlocked (FILE *);
362 int	getchar_unlocked (void);
363 void	flockfile (FILE *);
364 int	ftrylockfile (FILE *);
365 void	funlockfile (FILE *);
366 int	putc_unlocked (int, FILE *);
367 int	putchar_unlocked (int);
368 #endif
369 
370 /*
371  * Routines in POSIX 1003.1:200x.
372  */
373 
374 #if __POSIX_VISIBLE >= 200809
375 # ifndef _REENT_ONLY
376 #  ifndef dprintf
377 int	dprintf (int, const char *__restrict, ...)
378                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
379 #  endif
380 FILE *	fmemopen (void *__restrict, size_t, const char *__restrict);
381 /* getdelim - see __getdelim for now */
382 /* getline - see __getline for now */
383 FILE *	open_memstream (char **, size_t *);
384 int	vdprintf (int, const char *__restrict, __VALIST)
385                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
386 # endif
387 #endif
388 #if __ATFILE_VISIBLE
389 int	renameat (int, const char *, int, const char *);
390 # ifdef __CYGWIN__
391 int	renameat2 (int, const char *, int, const char *, unsigned int);
392 # endif
393 #endif
394 
395 /*
396  * Recursive versions of the above.
397  */
398 
399 int	_asiprintf_r (struct _reent *, char **, const char *, ...)
400                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
401 char *	_asniprintf_r (struct _reent *, char *, size_t *, const char *, ...)
402                _ATTRIBUTE ((__format__ (__printf__, 4, 5)));
403 char *	_asnprintf_r (struct _reent *, char *__restrict, size_t *__restrict, const char *__restrict, ...)
404                _ATTRIBUTE ((__format__ (__printf__, 4, 5)));
405 int	_asprintf_r (struct _reent *, char **__restrict, const char *__restrict, ...)
406                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
407 int	_diprintf_r (struct _reent *, int, const char *, ...)
408                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
409 int	_dprintf_r (struct _reent *, int, const char *__restrict, ...)
410                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
411 int	_fclose_r (struct _reent *, FILE *);
412 int	_fcloseall_r (struct _reent *);
413 FILE *	_fdopen_r (struct _reent *, int, const char *);
414 int	_fflush_r (struct _reent *, FILE *);
415 int	_fgetc_r (struct _reent *, FILE *);
416 int	_fgetc_unlocked_r (struct _reent *, FILE *);
417 char *  _fgets_r (struct _reent *, char *__restrict, int, FILE *__restrict);
418 char *  _fgets_unlocked_r (struct _reent *, char *__restrict, int, FILE *__restrict);
419 #ifdef _COMPILING_NEWLIB
420 int	_fgetpos_r (struct _reent *, FILE *__restrict, _fpos_t *__restrict);
421 int	_fsetpos_r (struct _reent *, FILE *, const _fpos_t *);
422 #else
423 int	_fgetpos_r (struct _reent *, FILE *, fpos_t *);
424 int	_fsetpos_r (struct _reent *, FILE *, const fpos_t *);
425 #endif
426 int	_fiprintf_r (struct _reent *, FILE *, const char *, ...)
427                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
428 int	_fiscanf_r (struct _reent *, FILE *, const char *, ...)
429                _ATTRIBUTE ((__format__ (__scanf__, 3, 4)));
430 FILE *	_fmemopen_r (struct _reent *, void *__restrict, size_t, const char *__restrict);
431 FILE *	_fopen_r (struct _reent *, const char *__restrict, const char *__restrict);
432 FILE *	_freopen_r (struct _reent *, const char *__restrict, const char *__restrict, FILE *__restrict);
433 int	_fprintf_r (struct _reent *, FILE *__restrict, const char *__restrict, ...)
434                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
435 int	_fpurge_r (struct _reent *, FILE *);
436 int	_fputc_r (struct _reent *, int, FILE *);
437 int	_fputc_unlocked_r (struct _reent *, int, FILE *);
438 int	_fputs_r (struct _reent *, const char *__restrict, FILE *__restrict);
439 int	_fputs_unlocked_r (struct _reent *, const char *__restrict, FILE *__restrict);
440 size_t	_fread_r (struct _reent *, void *__restrict, size_t _size, size_t _n, FILE *__restrict);
441 size_t	_fread_unlocked_r (struct _reent *, void *__restrict, size_t _size, size_t _n, FILE *__restrict);
442 int	_fscanf_r (struct _reent *, FILE *__restrict, const char *__restrict, ...)
443                _ATTRIBUTE ((__format__ (__scanf__, 3, 4)));
444 int	_fseek_r (struct _reent *, FILE *, long, int);
445 int	_fseeko_r (struct _reent *, FILE *, _off_t, int);
446 long	_ftell_r (struct _reent *, FILE *);
447 _off_t	_ftello_r (struct _reent *, FILE *);
448 void	_rewind_r (struct _reent *, FILE *);
449 size_t	_fwrite_r (struct _reent *, const void *__restrict, size_t _size, size_t _n, FILE *__restrict);
450 size_t	_fwrite_unlocked_r (struct _reent *, const void *__restrict, size_t _size, size_t _n, FILE *__restrict);
451 int	_getc_r (struct _reent *, FILE *);
452 int	_getc_unlocked_r (struct _reent *, FILE *);
453 int	_getchar_r (struct _reent *);
454 int	_getchar_unlocked_r (struct _reent *);
455 char *	_gets_r (struct _reent *, char *);
456 int	_iprintf_r (struct _reent *, const char *, ...)
457                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
458 int	_iscanf_r (struct _reent *, const char *, ...)
459                _ATTRIBUTE ((__format__ (__scanf__, 2, 3)));
460 FILE *	_open_memstream_r (struct _reent *, char **, size_t *);
461 void	_perror_r (struct _reent *, const char *);
462 int	_printf_r (struct _reent *, const char *__restrict, ...)
463                _ATTRIBUTE ((__format__ (__printf__, 2, 3)));
464 int	_putc_r (struct _reent *, int, FILE *);
465 int	_putc_unlocked_r (struct _reent *, int, FILE *);
466 int	_putchar_unlocked_r (struct _reent *, int);
467 int	_putchar_r (struct _reent *, int);
468 int	_puts_r (struct _reent *, const char *);
469 int	_remove_r (struct _reent *, const char *);
470 int	_rename_r (struct _reent *,
471 			   const char *_old, const char *_new);
472 int	_scanf_r (struct _reent *, const char *__restrict, ...)
473                _ATTRIBUTE ((__format__ (__scanf__, 2, 3)));
474 int	_siprintf_r (struct _reent *, char *, const char *, ...)
475                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
476 int	_siscanf_r (struct _reent *, const char *, const char *, ...)
477                _ATTRIBUTE ((__format__ (__scanf__, 3, 4)));
478 int	_sniprintf_r (struct _reent *, char *, size_t, const char *, ...)
479                _ATTRIBUTE ((__format__ (__printf__, 4, 5)));
480 int	_snprintf_r (struct _reent *, char *__restrict, size_t, const char *__restrict, ...)
481                _ATTRIBUTE ((__format__ (__printf__, 4, 5)));
482 int	_sprintf_r (struct _reent *, char *__restrict, const char *__restrict, ...)
483                _ATTRIBUTE ((__format__ (__printf__, 3, 4)));
484 int	_sscanf_r (struct _reent *, const char *__restrict, const char *__restrict, ...)
485                _ATTRIBUTE ((__format__ (__scanf__, 3, 4)));
486 char *	_tempnam_r (struct _reent *, const char *, const char *);
487 FILE *	_tmpfile_r (struct _reent *);
488 char *	_tmpnam_r (struct _reent *, char *);
489 int	_ungetc_r (struct _reent *, int, FILE *);
490 int	_vasiprintf_r (struct _reent *, char **, const char *, __VALIST)
491                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
492 char *	_vasniprintf_r (struct _reent*, char *, size_t *, const char *, __VALIST)
493                _ATTRIBUTE ((__format__ (__printf__, 4, 0)));
494 char *	_vasnprintf_r (struct _reent*, char *, size_t *, const char *, __VALIST)
495                _ATTRIBUTE ((__format__ (__printf__, 4, 0)));
496 int	_vasprintf_r (struct _reent *, char **, const char *, __VALIST)
497                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
498 int	_vdiprintf_r (struct _reent *, int, const char *, __VALIST)
499                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
500 int	_vdprintf_r (struct _reent *, int, const char *__restrict, __VALIST)
501                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
502 int	_vfiprintf_r (struct _reent *, FILE *, const char *, __VALIST)
503                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
504 int	_vfiscanf_r (struct _reent *, FILE *, const char *, __VALIST)
505                _ATTRIBUTE ((__format__ (__scanf__, 3, 0)));
506 int	_vfprintf_r (struct _reent *, FILE *__restrict, const char *__restrict, __VALIST)
507                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
508 int	_vfscanf_r (struct _reent *, FILE *__restrict, const char *__restrict, __VALIST)
509                _ATTRIBUTE ((__format__ (__scanf__, 3, 0)));
510 int	_viprintf_r (struct _reent *, const char *, __VALIST)
511                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
512 int	_viscanf_r (struct _reent *, const char *, __VALIST)
513                _ATTRIBUTE ((__format__ (__scanf__, 2, 0)));
514 int	_vprintf_r (struct _reent *, const char *__restrict, __VALIST)
515                _ATTRIBUTE ((__format__ (__printf__, 2, 0)));
516 int	_vscanf_r (struct _reent *, const char *__restrict, __VALIST)
517                _ATTRIBUTE ((__format__ (__scanf__, 2, 0)));
518 int	_vsiprintf_r (struct _reent *, char *, const char *, __VALIST)
519                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
520 int	_vsiscanf_r (struct _reent *, const char *, const char *, __VALIST)
521                _ATTRIBUTE ((__format__ (__scanf__, 3, 0)));
522 int	_vsniprintf_r (struct _reent *, char *, size_t, const char *, __VALIST)
523                _ATTRIBUTE ((__format__ (__printf__, 4, 0)));
524 int	_vsnprintf_r (struct _reent *, char *__restrict, size_t, const char *__restrict, __VALIST)
525                _ATTRIBUTE ((__format__ (__printf__, 4, 0)));
526 int	_vsprintf_r (struct _reent *, char *__restrict, const char *__restrict, __VALIST)
527                _ATTRIBUTE ((__format__ (__printf__, 3, 0)));
528 int	_vsscanf_r (struct _reent *, const char *__restrict, const char *__restrict, __VALIST)
529                _ATTRIBUTE ((__format__ (__scanf__, 3, 0)));
530 
531 /* Other extensions.  */
532 
533 int	fpurge (FILE *);
534 ssize_t __getdelim (char **, size_t *, int, FILE *);
535 ssize_t __getline (char **, size_t *, FILE *);
536 
537 #if __MISC_VISIBLE
538 void	clearerr_unlocked (FILE *);
539 int	feof_unlocked (FILE *);
540 int	ferror_unlocked (FILE *);
541 int	fileno_unlocked (FILE *);
542 int	fflush_unlocked (FILE *);
543 int	fgetc_unlocked (FILE *);
544 int	fputc_unlocked (int, FILE *);
545 size_t	fread_unlocked (void *__restrict, size_t _size, size_t _n, FILE *__restrict);
546 size_t	fwrite_unlocked (const void *__restrict , size_t _size, size_t _n, FILE *);
547 #endif
548 
549 #if __GNU_VISIBLE
550 char *  fgets_unlocked (char *__restrict, int, FILE *__restrict);
551 int	fputs_unlocked (const char *__restrict, FILE *__restrict);
552 #endif
553 
554 #ifdef __LARGE64_FILES
555 #if !defined(__CYGWIN__) || defined(_COMPILING_NEWLIB)
556 FILE *	fdopen64 (int, const char *);
557 FILE *  fopen64 (const char *, const char *);
558 FILE *  freopen64 (const char *, const char *, FILE *);
559 _off64_t ftello64 (FILE *);
560 _off64_t fseeko64 (FILE *, _off64_t, int);
561 int     fgetpos64 (FILE *, _fpos64_t *);
562 int     fsetpos64 (FILE *, const _fpos64_t *);
563 FILE *  tmpfile64 (void);
564 
565 FILE *	_fdopen64_r (struct _reent *, int, const char *);
566 FILE *  _fopen64_r (struct _reent *,const char *, const char *);
567 FILE *  _freopen64_r (struct _reent *, const char *, const char *, FILE *);
568 _off64_t _ftello64_r (struct _reent *, FILE *);
569 _off64_t _fseeko64_r (struct _reent *, FILE *, _off64_t, int);
570 int     _fgetpos64_r (struct _reent *, FILE *, _fpos64_t *);
571 int     _fsetpos64_r (struct _reent *, FILE *, const _fpos64_t *);
572 FILE *  _tmpfile64_r (struct _reent *);
573 #endif /* !__CYGWIN__ */
574 #endif /* __LARGE64_FILES */
575 
576 /*
577  * Routines internal to the implementation.
578  */
579 
580 int	__srget_r (struct _reent *, FILE *);
581 int	__swbuf_r (struct _reent *, int, FILE *);
582 
583 /*
584  * Stdio function-access interface.
585  */
586 
587 #if __BSD_VISIBLE
588 # ifdef __LARGE64_FILES
589 FILE	*funopen (const void *__cookie,
590 		int (*__readfn)(void *__c, char *__buf,
591 				_READ_WRITE_BUFSIZE_TYPE __n),
592 		int (*__writefn)(void *__c, const char *__buf,
593 				 _READ_WRITE_BUFSIZE_TYPE __n),
594 		_fpos64_t (*__seekfn)(void *__c, _fpos64_t __off, int __whence),
595 		int (*__closefn)(void *__c));
596 FILE	*_funopen_r (struct _reent *, const void *__cookie,
597 		int (*__readfn)(void *__c, char *__buf,
598 				_READ_WRITE_BUFSIZE_TYPE __n),
599 		int (*__writefn)(void *__c, const char *__buf,
600 				 _READ_WRITE_BUFSIZE_TYPE __n),
601 		_fpos64_t (*__seekfn)(void *__c, _fpos64_t __off, int __whence),
602 		int (*__closefn)(void *__c));
603 # else
604 FILE	*funopen (const void *__cookie,
605 		int (*__readfn)(void *__cookie, char *__buf,
606 				_READ_WRITE_BUFSIZE_TYPE __n),
607 		int (*__writefn)(void *__cookie, const char *__buf,
608 				 _READ_WRITE_BUFSIZE_TYPE __n),
609 		fpos_t (*__seekfn)(void *__cookie, fpos_t __off, int __whence),
610 		int (*__closefn)(void *__cookie));
611 FILE	*_funopen_r (struct _reent *, const void *__cookie,
612 		int (*__readfn)(void *__cookie, char *__buf,
613 				_READ_WRITE_BUFSIZE_TYPE __n),
614 		int (*__writefn)(void *__cookie, const char *__buf,
615 				 _READ_WRITE_BUFSIZE_TYPE __n),
616 		fpos_t (*__seekfn)(void *__cookie, fpos_t __off, int __whence),
617 		int (*__closefn)(void *__cookie));
618 # endif /* !__LARGE64_FILES */
619 
620 # define	fropen(__cookie, __fn) funopen(__cookie, __fn, (int (*)())0, \
621 					       (fpos_t (*)())0, (int (*)())0)
622 # define	fwopen(__cookie, __fn) funopen(__cookie, (int (*)())0, __fn, \
623 					       (fpos_t (*)())0, (int (*)())0)
624 #endif /* __BSD_VISIBLE */
625 
626 #if __GNU_VISIBLE
627 typedef ssize_t cookie_read_function_t(void *__cookie, char *__buf, size_t __n);
628 typedef ssize_t cookie_write_function_t(void *__cookie, const char *__buf,
629 					size_t __n);
630 # ifdef __LARGE64_FILES
631 typedef int cookie_seek_function_t(void *__cookie, _off64_t *__off,
632 				   int __whence);
633 # else
634 typedef int cookie_seek_function_t(void *__cookie, off_t *__off, int __whence);
635 # endif /* !__LARGE64_FILES */
636 typedef int cookie_close_function_t(void *__cookie);
637 typedef struct
638 {
639   /* These four struct member names are dictated by Linux; hopefully,
640      they don't conflict with any macros.  */
641   cookie_read_function_t  *read;
642   cookie_write_function_t *write;
643   cookie_seek_function_t  *seek;
644   cookie_close_function_t *close;
645 } cookie_io_functions_t;
646 FILE *fopencookie (void *__cookie,
647 		const char *__mode, cookie_io_functions_t __functions);
648 FILE *_fopencookie_r (struct _reent *, void *__cookie,
649 		const char *__mode, cookie_io_functions_t __functions);
650 #endif /* __GNU_VISIBLE */
651 
652 #ifndef __CUSTOM_FILE_IO__
653 /*
654  * The __sfoo macros are here so that we can
655  * define function versions in the C library.
656  */
657 #define       __sgetc_raw_r(__ptr, __f) (--(__f)->_r < 0 ? __srget_r(__ptr, __f) : (int)(*(__f)->_p++))
658 
659 #ifdef __SCLE
660 /*  For a platform with CR/LF, additional logic is required by
661   __sgetc_r which would otherwise simply be a macro; therefore we
662   use an inlined function.  The function is only meant to be inlined
663   in place as used and the function body should never be emitted.
664 
665   There are two possible means to this end when compiling with GCC,
666   one when compiling with a standard C99 compiler, and for other
667   compilers we're just stuck.  At the moment, this issue only
668   affects the Cygwin target, so we'll most likely be using GCC. */
669 
670 _ELIDABLE_INLINE int __sgetc_r(struct _reent *__ptr, FILE *__p);
671 
__sgetc_r(struct _reent * __ptr,FILE * __p)672 _ELIDABLE_INLINE int __sgetc_r(struct _reent *__ptr, FILE *__p)
673   {
674     int __c = __sgetc_raw_r(__ptr, __p);
675     if ((__p->_flags & __SCLE) && (__c == '\r'))
676       {
677       int __c2 = __sgetc_raw_r(__ptr, __p);
678       if (__c2 == '\n')
679         __c = __c2;
680       else
681         ungetc(__c2, __p);
682       }
683     return __c;
684   }
685 #else
686 #define __sgetc_r(__ptr, __p) __sgetc_raw_r(__ptr, __p)
687 #endif
688 
689 #ifdef __GNUC__
__sputc_r(struct _reent * _ptr,int _c,FILE * _p)690 _ELIDABLE_INLINE int __sputc_r(struct _reent *_ptr, int _c, FILE *_p) {
691 #ifdef __SCLE
692 	if ((_p->_flags & __SCLE) && _c == '\n')
693 	  __sputc_r (_ptr, '\r', _p);
694 #endif
695 	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
696 		return (*_p->_p++ = _c);
697 	else
698 		return (__swbuf_r(_ptr, _c, _p));
699 }
700 #else
701 /*
702  * This has been tuned to generate reasonable code on the vax using pcc
703  */
704 #define       __sputc_raw_r(__ptr, __c, __p) \
705 	(--(__p)->_w < 0 ? \
706 		(__p)->_w >= (__p)->_lbfsize ? \
707 			(*(__p)->_p = (__c)), *(__p)->_p != '\n' ? \
708 				(int)*(__p)->_p++ : \
709 				__swbuf_r(__ptr, '\n', __p) : \
710 			__swbuf_r(__ptr, (int)(__c), __p) : \
711 		(*(__p)->_p = (__c), (int)*(__p)->_p++))
712 #ifdef __SCLE
713 #define __sputc_r(__ptr, __c, __p) \
714         ((((__p)->_flags & __SCLE) && ((__c) == '\n')) \
715           ? __sputc_raw_r(__ptr, '\r', (__p)) : 0 , \
716         __sputc_raw_r((__ptr), (__c), (__p)))
717 #else
718 #define __sputc_r(__ptr, __c, __p) __sputc_raw_r(__ptr, __c, __p)
719 #endif
720 #endif
721 
722 #define	__sfeof(p)	((int)(((p)->_flags & __SEOF) != 0))
723 #define	__sferror(p)	((int)(((p)->_flags & __SERR) != 0))
724 #define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
725 #define	__sfileno(p)	((p)->_file)
726 
727 #ifndef __cplusplus
728 #ifndef _REENT_SMALL
729 #define	feof(p)		__sfeof(p)
730 #define	ferror(p)	__sferror(p)
731 #define	clearerr(p)	__sclearerr(p)
732 
733 #if __MISC_VISIBLE
734 #define	feof_unlocked(p)	__sfeof(p)
735 #define	ferror_unlocked(p)	__sferror(p)
736 #define	clearerr_unlocked(p)	__sclearerr(p)
737 #endif /* __MISC_VISIBLE */
738 #endif /* _REENT_SMALL */
739 
740 #if 0 /* __POSIX_VISIBLE - FIXME: must initialize stdio first, use fn */
741 #define	fileno(p)	__sfileno(p)
742 #endif
743 
744 static __inline int
_getchar_unlocked(void)745 _getchar_unlocked(void)
746 {
747 	struct _reent *_ptr;
748 
749 	_ptr = _REENT;
750 	return (__sgetc_r(_ptr, _stdin_r(_ptr)));
751 }
752 
753 static __inline int
_putchar_unlocked(int _c)754 _putchar_unlocked(int _c)
755 {
756 	struct _reent *_ptr;
757 
758 	_ptr = _REENT;
759 	return (__sputc_r(_ptr, _c, _stdout_r(_ptr)));
760 }
761 
762 #ifdef __SINGLE_THREAD__
763 #define	getc(_p)	__sgetc_r(_REENT, _p)
764 #define	putc(_c, _p)	__sputc_r(_REENT, _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_r(_REENT, (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