1 /* Copyright (C) 2002-2004   Manuel Novoa III    <mjn3@codepoet.org>
2  *
3  * GNU Library General Public License (LGPL) version 2 or later.
4  *
5  * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
6  */
7 
8 #ifndef _STDIO_H
9 #error Always include <stdio.h> rather than <bits/uClibc_stdio.h>
10 #endif
11 
12 /**********************************************************************/
13 
14 #define __STDIO_BUFFERS
15 /* ANSI/ISO mandate at least 256. */
16 #if defined(__UCLIBC_HAS_STDIO_BUFSIZ_NONE__)
17 /* Fake this because some apps use stdio.h BUFSIZ. */
18 #define __STDIO_BUFSIZ			256
19 #undef __STDIO_BUFFERS
20 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_256__)
21 #define __STDIO_BUFSIZ			256
22 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_512__)
23 #define __STDIO_BUFSIZ			512
24 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_1024__)
25 #define __STDIO_BUFSIZ		   1024
26 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_2048__)
27 #define __STDIO_BUFSIZ		   2048
28 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_4096__)
29 #define __STDIO_BUFSIZ		   4096
30 #elif defined(__UCLIBC_HAS_STDIO_BUFSIZ_8192__)
31 #define __STDIO_BUFSIZ		   8192
32 #else
33 #error config seems to be out of sync regarding bufsiz options
34 #endif
35 
36 #ifdef __UCLIBC_HAS_STDIO_BUFSIZ_NONE__
37 #define __STDIO_BUILTIN_BUF_SIZE		0
38 #else  /* __UCLIBC_HAS_STDIO_BUFSIZ_NONE__ */
39 #if defined(__UCLIBC_HAS_STDIO_BUILTIN_BUFFER_NONE__)
40 #define __STDIO_BUILTIN_BUF_SIZE		0
41 #elif defined(__UCLIBC_HAS_STDIO_BUILTIN_BUFFER_4__)
42 #define __STDIO_BUILTIN_BUF_SIZE		4
43 #elif defined(__UCLIBC_HAS_STDIO_BUILTIN_BUFFER_8__)
44 #define __STDIO_BUILTIN_BUF_SIZE		8
45 #else
46 #error config seems to be out of sync regarding builtin buffer size
47 #endif
48 #endif
49 
50 #if defined(__STDIO_BUFFERS) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__) || defined(__UCLIBC_HAS_THREADS__)
51 #define __STDIO_HAS_OPENLIST 1
52 #else
53 #undef __STDIO_HAS_OPENLIST
54 #endif
55 
56 /**********************************************************************/
57 #ifdef __UCLIBC_HAS_WCHAR__
58 
59 #define __need_wchar_t
60 #include <stddef.h>
61 
62 /* Note: we don't really need mbstate for 8-bit locales.  We do for UTF-8.
63  * For now, always use it. */
64 #define __STDIO_MBSTATE
65 #define __need_mbstate_t
66 #include <wchar.h>
67 
68 #endif
69 /**********************************************************************/
70 /* Currently unimplemented/untested */
71 /* #define __STDIO_FLEXIBLE_SETVBUF */
72 
73 #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
74 #define __STDIO_GETC_MACRO
75 #endif
76 
77 #ifdef __UCLIBC_HAS_STDIO_PUTC_MACRO__
78 #define __STDIO_PUTC_MACRO
79 #endif
80 
81 #ifdef _LIBC
82 /**********************************************************************/
83 #include <bits/uClibc_mutex.h>
84 
85 /* user_locking
86  * 0 : do auto locking/unlocking
87  * 1 : user does locking/unlocking
88  * 2 : initial state prior to thread initialization
89  *     with no auto locking/unlocking
90  *
91  * When threading is initialized, walk the stdio open stream list
92  * and do  "if (user_locking == 2) user_locking = 0;".
93  *
94  * This way, we avoid calling the weak lock/unlock functions.
95  */
96 
97 #define __STDIO_AUTO_THREADLOCK_VAR						\
98         __UCLIBC_MUTEX_AUTO_LOCK_VAR(__infunc_user_locking)
99 
100 #define __STDIO_AUTO_THREADLOCK(__stream)					\
101         __UCLIBC_IO_MUTEX_AUTO_LOCK((__stream)->__lock, __infunc_user_locking,	\
102 	(__stream)->__user_locking)
103 
104 #define __STDIO_AUTO_THREADUNLOCK(__stream)					\
105         __UCLIBC_IO_MUTEX_AUTO_UNLOCK((__stream)->__lock, __infunc_user_locking)
106 
107 #define __STDIO_ALWAYS_THREADLOCK(__stream)					\
108         __UCLIBC_IO_MUTEX_LOCK((__stream)->__lock)
109 
110 #define __STDIO_ALWAYS_THREADUNLOCK(__stream)					\
111         __UCLIBC_IO_MUTEX_UNLOCK((__stream)->__lock)
112 
113 #define __STDIO_ALWAYS_THREADLOCK_CANCEL_UNSAFE(__stream)			\
114         __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE((__stream)->__lock)
115 
116 #define __STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(__stream)			\
117         __UCLIBC_IO_MUTEX_TRYLOCK_CANCEL_UNSAFE((__stream)->__lock)
118 
119 #define __STDIO_ALWAYS_THREADUNLOCK_CANCEL_UNSAFE(__stream)			\
120         __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE((__stream)->__lock)
121 
122 #ifdef __UCLIBC_HAS_THREADS__
123 #define __STDIO_SET_USER_LOCKING(__stream)	((__stream)->__user_locking = 1)
124 #else
125 #define __STDIO_SET_USER_LOCKING(__stream)		((void)0)
126 #endif
127 
128 #ifdef __UCLIBC_HAS_THREADS__
129 #ifdef __USE_STDIO_FUTEXES__
130 #define STDIO_INIT_MUTEX(M) _IO_lock_init(M)
131 #else
132 #define STDIO_INIT_MUTEX(M) __stdio_init_mutex(& M)
133 #endif
134 #endif
135 
136 #endif /* _LIBC */
137 
138 /**********************************************************************/
139 
140 #define __STDIO_IOFBF 0		/* Fully buffered.  */
141 #define __STDIO_IOLBF 1		/* Line buffered.  */
142 #define __STDIO_IONBF 2		/* No buffering.  */
143 
144 typedef struct {
145 	__off_t __pos;
146 #ifdef __STDIO_MBSTATE
147 	__mbstate_t __mbstate;
148 #endif
149 #ifdef __UCLIBC_HAS_WCHAR__
150 	int __mblen_pending;
151 #endif
152 } __STDIO_fpos_t;
153 
154 #ifdef __UCLIBC_HAS_LFS__
155 typedef struct {
156 	__off64_t __pos;
157 #ifdef __STDIO_MBSTATE
158 	__mbstate_t __mbstate;
159 #endif
160 #ifdef __UCLIBC_HAS_WCHAR__
161 	int __mblen_pending;
162 #endif
163 } __STDIO_fpos64_t;
164 #endif
165 
166 /**********************************************************************/
167 #ifdef __UCLIBC_HAS_LFS__
168 typedef __off64_t __offmax_t;		/* TODO -- rename this? */
169 #else
170 typedef __off_t __offmax_t;		/* TODO -- rename this? */
171 #endif
172 
173 /**********************************************************************/
174 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
175 
176 typedef __ssize_t __io_read_fn(void *__cookie, char *__buf, size_t __bufsize);
177 typedef __ssize_t __io_write_fn(void *__cookie,
178 					const char *__buf, size_t __bufsize);
179 /* NOTE: GLIBC difference!!! -- fopencookie seek function
180  * For glibc, the type of pos is always (__off64_t *) but in our case
181  * it is type (__off_t *) when the lib is built without large file support.
182  */
183 typedef int __io_seek_fn(void *__cookie, __offmax_t *__pos, int __whence);
184 typedef int __io_close_fn(void *__cookie);
185 
186 typedef struct {
187 	__io_read_fn  *read;
188 	__io_write_fn *write;
189 	__io_seek_fn  *seek;
190 	__io_close_fn *close;
191 } _IO_cookie_io_functions_t;
192 
193 #ifdef __USE_GNU
194 
195 typedef __io_read_fn cookie_read_function_t;
196 typedef __io_write_fn cookie_write_function_t;
197 typedef __io_seek_fn cookie_seek_function_t;
198 typedef __io_close_fn cookie_close_function_t;
199 
200 typedef _IO_cookie_io_functions_t cookie_io_functions_t;
201 
202 #endif
203 
204 #endif
205 /**********************************************************************/
206 
207 #if defined __UCLIBC_HAS_THREADS__ && !defined __UCLIBC_IO_MUTEX
208 /* keep this in sync with uClibc_mutex.h */
209 # ifdef __USE_STDIO_FUTEXES__
210 #  include <bits/stdio-lock.h>
211 #  define __UCLIBC_IO_MUTEX(M) _IO_lock_t M
212 # else
213 #  include <bits/pthreadtypes.h>
214 #  define __UCLIBC_IO_MUTEX(M) pthread_mutex_t M
215 # endif /* __UCLIBC_HAS_THREADS_NATIVE__ */
216 #endif
217 
218 struct __STDIO_FILE_STRUCT {
219 	unsigned short __modeflags;
220 	/* There could be a hole here, but modeflags is used most.*/
221 #ifdef __UCLIBC_HAS_WCHAR__
222 	unsigned char __ungot_width[2]; /* 0: current (building) char; 1: scanf */
223 	/* Move the following futher down to avoid problems with getc/putc
224 	 * macros breaking shared apps when wchar config support is changed. */
225 	/* wchar_t ungot[2]; */
226 #else  /* __UCLIBC_HAS_WCHAR__ */
227 	unsigned char __ungot[2];
228 #endif /* __UCLIBC_HAS_WCHAR__ */
229 	int __filedes;
230 #ifdef __STDIO_BUFFERS
231 	unsigned char *__bufstart;	/* pointer to buffer */
232 	unsigned char *__bufend;	/* pointer to 1 past end of buffer */
233 	unsigned char *__bufpos;
234 	unsigned char *__bufread; /* pointer to 1 past last buffered read char */
235 
236 #ifdef __STDIO_GETC_MACRO
237 	unsigned char *__bufgetc_u;	/* 1 past last readable by getc_unlocked */
238 #endif /* __STDIO_GETC_MACRO */
239 #ifdef __STDIO_PUTC_MACRO
240 	unsigned char *__bufputc_u;	/* 1 past last writeable by putc_unlocked */
241 #endif /* __STDIO_PUTC_MACRO */
242 
243 #endif /* __STDIO_BUFFERS */
244 
245 #ifdef __STDIO_HAS_OPENLIST
246 	struct __STDIO_FILE_STRUCT *__nextopen;
247 #endif
248 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
249 	void *__cookie;
250 	_IO_cookie_io_functions_t __gcs;
251 #endif
252 #ifdef __UCLIBC_HAS_WCHAR__
253 	wchar_t __ungot[2];
254 #endif
255 #ifdef __STDIO_MBSTATE
256 	__mbstate_t __state;
257 #endif
258 #ifdef __UCLIBC_HAS_XLOCALE__
259 	void *__unused;				/* Placeholder for codeset binding. */
260 #endif
261 #ifdef __UCLIBC_HAS_THREADS__
262 	int __user_locking;
263 	__UCLIBC_IO_MUTEX(__lock);
264 #endif
265 /* Everything after this is unimplemented... and may be trashed. */
266 #if __STDIO_BUILTIN_BUF_SIZE > 0
267 	unsigned char __builtinbuf[__STDIO_BUILTIN_BUF_SIZE];
268 #endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
269 };
270 
271 
272 /***********************************************************************/
273 /* Having ungotten characters implies the stream is reading.
274  * The scheme used here treats the least significant 2 bits of
275  * the stream's modeflags member as follows:
276  *   0 0   Not currently reading.
277  *   0 1   Reading, but no ungetc() or scanf() push back chars.
278  *   1 0   Reading with one ungetc() char (ungot[1] is 1)
279  *         or one scanf() pushed back char (ungot[1] is 0).
280  *   1 1   Reading with both an ungetc() char and a scanf()
281  *         pushed back char.  Note that this must be the result
282  *         of a scanf() push back (in ungot[0]) _followed_ by
283  *         an ungetc() call (in ungot[1]).
284  *
285  * Notes:
286  *   scanf() can NOT use ungetc() to push back characters.
287  *     (See section 7.19.6.2 of the C9X rationale -- WG14/N897.)
288  */
289 
290 #define __MASK_READING		0x0003U /* (0x0001 | 0x0002) */
291 #define __FLAG_READING		0x0001U
292 #define __FLAG_UNGOT		0x0002U
293 #define __FLAG_EOF		0x0004U
294 #define __FLAG_ERROR		0x0008U
295 #define __FLAG_WRITEONLY	0x0010U
296 #define __FLAG_READONLY		0x0020U /* (__FLAG_WRITEONLY << 1) */
297 #define __FLAG_WRITING		0x0040U
298 #define __FLAG_NARROW		0x0080U
299 
300 #define __FLAG_FBF		0x0000U /* must be 0 */
301 #define __FLAG_LBF		0x0100U
302 #define __FLAG_NBF		0x0200U /* (__FLAG_LBF << 1) */
303 #define __MASK_BUFMODE		0x0300U /* (__FLAG_LBF|__FLAG_NBF) */
304 #define __FLAG_APPEND		0x0400U /* fixed! == O_APPEND for linux */
305 #define __FLAG_WIDE		0x0800U
306 /* available slot		0x1000U */
307 #define __FLAG_FREEFILE		0x2000U
308 #define __FLAG_FREEBUF		0x4000U
309 #define __FLAG_LARGEFILE	0x8000U /* fixed! == 0_LARGEFILE for linux */
310 #define __FLAG_FAILED_FREOPEN	__FLAG_LARGEFILE
311 
312 /* Note: In no-buffer mode, it would be possible to pack the necessary
313  * flags into one byte.  Since we wouldn't be buffering and there would
314  * be no support for wchar, the only flags we would need would be:
315  *   2 bits : ungot count
316  *   2 bits : eof + error
317  *   2 bits : readonly + writeonly
318  *   1 bit  : freefile
319  *   1 bit  : appending
320  * So, for a very small system (< 128 files) we might have a
321  * 4-byte FILE struct of:
322  *   unsigned char flags;
323  *   signed char filedes;
324  *   unsigned char ungot[2];
325  */
326 /**********************************************************************
327  * PROTOTYPES OF INTERNAL FUNCTIONS
328  **********************************************************************/
329 #if defined _LIBC && (defined IS_IN_libc || defined NOT_IN_libc)
330 
331 extern void weak_function _stdio_init(void) attribute_hidden;
332 extern void weak_function _stdio_term(void) attribute_hidden;
333 
334 #ifdef __STDIO_HAS_OPENLIST
335 
336 extern struct __STDIO_FILE_STRUCT *_stdio_openlist;
337 
338 #ifdef __UCLIBC_HAS_THREADS__
339 __UCLIBC_IO_MUTEX_EXTERN(_stdio_openlist_add_lock)
340 # ifndef __UCLIBC_HAS_THREADS_NATIVE__
341 	attribute_hidden
342 # endif
343 	;
344 #ifdef __STDIO_BUFFERS
345 __UCLIBC_IO_MUTEX_EXTERN(_stdio_openlist_del_lock)
346 # ifndef __UCLIBC_HAS_THREADS_NATIVE__
347 	attribute_hidden
348 # endif
349 	;
350 #endif
351 extern int _stdio_user_locking;
352 #endif
353 
354 #endif
355 
356 #endif
357 /**********************************************************************/
358 
359 #define __CLEARERR_UNLOCKED(__stream)					\
360 	((void)((__stream)->__modeflags &= ~(__FLAG_EOF|__FLAG_ERROR)))
361 #define __FEOF_UNLOCKED(__stream)	((__stream)->__modeflags & __FLAG_EOF)
362 #define __FERROR_UNLOCKED(__stream)	((__stream)->__modeflags & __FLAG_ERROR)
363 
364 #ifdef __UCLIBC_HAS_THREADS__
365 # define __CLEARERR(__stream)		(clearerr)(__stream)
366 # define __FERROR(__stream)		(ferror)(__stream)
367 # define __FEOF(__stream)		(feof)(__stream)
368 #else
369 # define __CLEARERR(__stream)		__CLEARERR_UNLOCKED(__stream)
370 # define __FERROR(__stream)		__FERROR_UNLOCKED(__stream)
371 # define __FEOF(__stream)		__FEOF_UNLOCKED(__stream)
372 #endif
373 
374 extern int __fgetc_unlocked(FILE *__stream);
375 libc_hidden_proto(__fgetc_unlocked)
376 extern int __fputc_unlocked(int __c, FILE *__stream);
377 libc_hidden_proto(__fputc_unlocked)
378 
379 /* First define the default definitions.
380    They are overridden below as necessary. */
381 #define __FGETC_UNLOCKED(__stream)		(__fgetc_unlocked)((__stream))
382 #define __FGETC(__stream)			(fgetc)((__stream))
383 #define __GETC_UNLOCKED_MACRO(__stream)		(__fgetc_unlocked)((__stream))
384 #define __GETC_UNLOCKED(__stream)		(__fgetc_unlocked)((__stream))
385 #define __GETC(__stream)			(fgetc)((__stream))
386 
387 #define __FPUTC_UNLOCKED(__c, __stream)		(__fputc_unlocked)((__c),(__stream))
388 #define __FPUTC(__c, __stream)			(fputc)((__c),(__stream))
389 #define __PUTC_UNLOCKED_MACRO(__c, __stream)	(__fputc_unlocked)((__c),(__stream))
390 #define __PUTC_UNLOCKED(__c, __stream)		(__fputc_unlocked)((__c),(__stream))
391 #define __PUTC(__c, __stream)			(fputc)((__c),(__stream))
392 
393 
394 #ifdef __STDIO_GETC_MACRO
395 
396 extern FILE *__stdin;			/* For getchar() macro. */
397 
398 # undef  __GETC_UNLOCKED_MACRO
399 # define __GETC_UNLOCKED_MACRO(__stream)				\
400 		( ((__stream)->__bufpos < (__stream)->__bufgetc_u)	\
401 		  ? (*(__stream)->__bufpos++)				\
402 		  : __fgetc_unlocked(__stream) )
403 
404 # if 0
405 	/* Classic macro approach.  getc{_unlocked} can have side effects. */
406 #  undef  __GETC_UNLOCKED
407 #  define __GETC_UNLOCKED(__stream)		__GETC_UNLOCKED_MACRO((__stream))
408 #  ifndef __UCLIBC_HAS_THREADS__
409 #   undef  __GETC
410 #   define __GETC(__stream)				__GETC_UNLOCKED_MACRO((__stream))
411 #  endif
412 
413 # else
414 	/* Using gcc extension for safety and additional inlining. */
415 #  undef  __FGETC_UNLOCKED
416 #  define __FGETC_UNLOCKED(__stream)					\
417 		(__extension__ ({					\
418 			FILE *__S = (__stream);				\
419 			__GETC_UNLOCKED_MACRO(__S);			\
420 		}) )
421 
422 #  undef  __GETC_UNLOCKED
423 #  define __GETC_UNLOCKED(__stream)		__FGETC_UNLOCKED((__stream))
424 
425 #  ifdef __UCLIBC_HAS_THREADS__
426 #   undef  __FGETC
427 #   define __FGETC(__stream)						\
428 		(__extension__ ({					\
429 			FILE *__S = (__stream);				\
430 			((__S->__user_locking )				\
431 			 ? __GETC_UNLOCKED_MACRO(__S)			\
432 			 : (fgetc)(__S));				\
433 		}) )
434 
435 #   undef  __GETC
436 #   define __GETC(__stream)			__FGETC((__stream))
437 
438 #  else
439 
440 #   undef  __FGETC
441 #   define __FGETC(__stream)			__FGETC_UNLOCKED((__stream))
442 #   undef  __GETC
443 #   define __GETC(__stream)			__FGETC_UNLOCKED((__stream))
444 
445 #  endif
446 # endif
447 
448 #else
449 
450 # define __stdin stdin
451 
452 #endif /* __STDIO_GETC_MACRO */
453 
454 
455 #ifdef __STDIO_PUTC_MACRO
456 
457 extern FILE *__stdout;			/* For putchar() macro. */
458 
459 # undef  __PUTC_UNLOCKED_MACRO
460 # define __PUTC_UNLOCKED_MACRO(__c, __stream)				\
461 		( ((__stream)->__bufpos < (__stream)->__bufputc_u)	\
462 		  ? (*(__stream)->__bufpos++) = (__c)			\
463 		  : __fputc_unlocked((__c),(__stream)) )
464 
465 # if 0
466 	/* Classic macro approach.  putc{_unlocked} can have side effects.*/
467 #  undef  __PUTC_UNLOCKED
468 #  define __PUTC_UNLOCKED(__c, __stream)				\
469 					__PUTC_UNLOCKED_MACRO((__c), (__stream))
470 #  ifndef __UCLIBC_HAS_THREADS__
471 #   undef  __PUTC
472 #   define __PUTC(__c, __stream)	__PUTC_UNLOCKED_MACRO((__c), (__stream))
473 #  endif
474 
475 # else
476 	/* Using gcc extension for safety and additional inlining. */
477 
478 #  undef  __FPUTC_UNLOCKED
479 #  define __FPUTC_UNLOCKED(__c, __stream)				\
480 		(__extension__ ({					\
481 			FILE *__S = (__stream);				\
482 			__PUTC_UNLOCKED_MACRO((__c),__S);		\
483 		}) )
484 
485 #  undef  __PUTC_UNLOCKED
486 #  define __PUTC_UNLOCKED(__c, __stream)	__FPUTC_UNLOCKED((__c), (__stream))
487 
488 #  ifdef __UCLIBC_HAS_THREADS__
489 #   undef  __FPUTC
490 #   define __FPUTC(__c, __stream)					\
491 		(__extension__ ({					\
492 			FILE *__S = (__stream);				\
493 			((__S->__user_locking)				\
494 			 ? __PUTC_UNLOCKED_MACRO((__c),__S)		\
495 			 : (fputc)((__c),__S));				\
496 		}) )
497 
498 #   undef  __PUTC
499 #   define __PUTC(__c, __stream)		__FPUTC((__c), (__stream))
500 
501 #  else
502 
503 #   undef  __FPUTC
504 #   define __FPUTC(__c, __stream)		__FPUTC_UNLOCKED((__c),(__stream))
505 #   undef  __PUTC
506 #   define __PUTC(__c, __stream)		__FPUTC_UNLOCKED((__c),(__stream))
507 
508 #  endif
509 # endif
510 
511 #else
512 
513 # define __stdout stdout
514 
515 #endif /* __STDIO_PUTC_MACRO */
516