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 typedef struct {
155 	__off64_t __pos;
156 #ifdef __STDIO_MBSTATE
157 	__mbstate_t __mbstate;
158 #endif
159 #ifdef __UCLIBC_HAS_WCHAR__
160 	int __mblen_pending;
161 #endif
162 } __STDIO_fpos64_t;
163 
164 /**********************************************************************/
165 typedef __off64_t __offmax_t;		/* TODO -- rename this? */
166 
167 /**********************************************************************/
168 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
169 
170 typedef __ssize_t __io_read_fn(void *__cookie, char *__buf, size_t __bufsize);
171 typedef __ssize_t __io_write_fn(void *__cookie,
172 					const char *__buf, size_t __bufsize);
173 /* NOTE: GLIBC difference!!! -- fopencookie seek function
174  * For glibc, the type of pos is always (__off64_t *) but in our case
175  * it is type (__off_t *) when the lib is built without large file support.
176  */
177 typedef int __io_seek_fn(void *__cookie, __offmax_t *__pos, int __whence);
178 typedef int __io_close_fn(void *__cookie);
179 
180 typedef struct {
181 	__io_read_fn  *read;
182 	__io_write_fn *write;
183 	__io_seek_fn  *seek;
184 	__io_close_fn *close;
185 } _IO_cookie_io_functions_t;
186 
187 #ifdef __USE_GNU
188 
189 typedef __io_read_fn cookie_read_function_t;
190 typedef __io_write_fn cookie_write_function_t;
191 typedef __io_seek_fn cookie_seek_function_t;
192 typedef __io_close_fn cookie_close_function_t;
193 
194 typedef _IO_cookie_io_functions_t cookie_io_functions_t;
195 
196 #endif
197 
198 #endif
199 /**********************************************************************/
200 
201 #if defined __UCLIBC_HAS_THREADS__ && !defined __UCLIBC_IO_MUTEX
202 /* keep this in sync with uClibc_mutex.h */
203 # ifdef __USE_STDIO_FUTEXES__
204 #  include <bits/stdio-lock.h>
205 #  define __UCLIBC_IO_MUTEX(M) _IO_lock_t M
206 # else
207 #  include <bits/pthreadtypes.h>
208 #  define __UCLIBC_IO_MUTEX(M) pthread_mutex_t M
209 # endif /* __UCLIBC_HAS_THREADS_NATIVE__ */
210 #endif
211 
212 struct __STDIO_FILE_STRUCT {
213 	unsigned short __modeflags;
214 	/* There could be a hole here, but modeflags is used most.*/
215 #ifdef __UCLIBC_HAS_WCHAR__
216 	unsigned char __ungot_width[2]; /* 0: current (building) char; 1: scanf */
217 	/* Move the following futher down to avoid problems with getc/putc
218 	 * macros breaking shared apps when wchar config support is changed. */
219 	/* wchar_t ungot[2]; */
220 #else  /* __UCLIBC_HAS_WCHAR__ */
221 	unsigned char __ungot[2];
222 #endif /* __UCLIBC_HAS_WCHAR__ */
223 	int __filedes;
224 #ifdef __STDIO_BUFFERS
225 	unsigned char *__bufstart;	/* pointer to buffer */
226 	unsigned char *__bufend;	/* pointer to 1 past end of buffer */
227 	unsigned char *__bufpos;
228 	unsigned char *__bufread; /* pointer to 1 past last buffered read char */
229 
230 #ifdef __STDIO_GETC_MACRO
231 	unsigned char *__bufgetc_u;	/* 1 past last readable by getc_unlocked */
232 #endif /* __STDIO_GETC_MACRO */
233 #ifdef __STDIO_PUTC_MACRO
234 	unsigned char *__bufputc_u;	/* 1 past last writeable by putc_unlocked */
235 #endif /* __STDIO_PUTC_MACRO */
236 
237 #endif /* __STDIO_BUFFERS */
238 
239 #ifdef __STDIO_HAS_OPENLIST
240 	struct __STDIO_FILE_STRUCT *__nextopen;
241 #endif
242 #ifdef __UCLIBC_HAS_WCHAR__
243 	wchar_t __ungot[2];
244 #endif
245 #ifdef __STDIO_MBSTATE
246 	__mbstate_t __state;
247 #endif
248 #ifdef __UCLIBC_HAS_XLOCALE__
249 	void *__unused;				/* Placeholder for codeset binding. */
250 #endif
251 #ifdef __UCLIBC_HAS_THREADS__
252 	int __user_locking;
253 	__UCLIBC_IO_MUTEX(__lock);
254 #endif
255 /* Everything after this is unimplemented... and may be trashed. */
256 #if __STDIO_BUILTIN_BUF_SIZE > 0
257 	unsigned char __builtinbuf[__STDIO_BUILTIN_BUF_SIZE];
258 #endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
259 };
260 
261 
262 /***********************************************************************/
263 /* Having ungotten characters implies the stream is reading.
264  * The scheme used here treats the least significant 2 bits of
265  * the stream's modeflags member as follows:
266  *   0 0   Not currently reading.
267  *   0 1   Reading, but no ungetc() or scanf() push back chars.
268  *   1 0   Reading with one ungetc() char (ungot[1] is 1)
269  *         or one scanf() pushed back char (ungot[1] is 0).
270  *   1 1   Reading with both an ungetc() char and a scanf()
271  *         pushed back char.  Note that this must be the result
272  *         of a scanf() push back (in ungot[0]) _followed_ by
273  *         an ungetc() call (in ungot[1]).
274  *
275  * Notes:
276  *   scanf() can NOT use ungetc() to push back characters.
277  *     (See section 7.19.6.2 of the C9X rationale -- WG14/N897.)
278  */
279 
280 #define __MASK_READING		0x0003U /* (0x0001 | 0x0002) */
281 #define __FLAG_READING		0x0001U
282 #define __FLAG_UNGOT		0x0002U
283 #define __FLAG_EOF		0x0004U
284 #define __FLAG_ERROR		0x0008U
285 #define __FLAG_WRITEONLY	0x0010U
286 #define __FLAG_READONLY		0x0020U /* (__FLAG_WRITEONLY << 1) */
287 #define __FLAG_WRITING		0x0040U
288 #define __FLAG_NARROW		0x0080U
289 
290 #define __FLAG_FBF		0x0000U /* must be 0 */
291 #define __FLAG_LBF		0x0100U
292 #define __FLAG_NBF		0x0200U /* (__FLAG_LBF << 1) */
293 #define __MASK_BUFMODE		0x0300U /* (__FLAG_LBF|__FLAG_NBF) */
294 #define __FLAG_APPEND		0x0400U /* fixed! == O_APPEND for linux */
295 #define __FLAG_WIDE		0x0800U
296 /* available slot		0x1000U */
297 #define __FLAG_FREEFILE		0x2000U
298 #define __FLAG_FREEBUF		0x4000U
299 #define __FLAG_LARGEFILE	0x8000U /* fixed! == 0_LARGEFILE for linux */
300 #define __FLAG_FAILED_FREOPEN	__FLAG_LARGEFILE
301 
302 /* Note: In no-buffer mode, it would be possible to pack the necessary
303  * flags into one byte.  Since we wouldn't be buffering and there would
304  * be no support for wchar, the only flags we would need would be:
305  *   2 bits : ungot count
306  *   2 bits : eof + error
307  *   2 bits : readonly + writeonly
308  *   1 bit  : freefile
309  *   1 bit  : appending
310  * So, for a very small system (< 128 files) we might have a
311  * 4-byte FILE struct of:
312  *   unsigned char flags;
313  *   signed char filedes;
314  *   unsigned char ungot[2];
315  */
316 /**********************************************************************
317  * PROTOTYPES OF INTERNAL FUNCTIONS
318  **********************************************************************/
319 #if defined _LIBC && (defined IS_IN_libc || defined NOT_IN_libc)
320 
321 extern void weak_function _stdio_init(void) attribute_hidden;
322 extern void weak_function _stdio_term(void) attribute_hidden;
323 
324 #ifdef __STDIO_HAS_OPENLIST
325 
326 extern struct __STDIO_FILE_STRUCT *_stdio_openlist;
327 
328 #ifdef __UCLIBC_HAS_THREADS__
329 __UCLIBC_IO_MUTEX_EXTERN(_stdio_openlist_add_lock)
330 # ifndef __UCLIBC_HAS_THREADS_NATIVE__
331 	attribute_hidden
332 # endif
333 	;
334 #ifdef __STDIO_BUFFERS
335 __UCLIBC_IO_MUTEX_EXTERN(_stdio_openlist_del_lock)
336 # ifndef __UCLIBC_HAS_THREADS_NATIVE__
337 	attribute_hidden
338 # endif
339 	;
340 #endif
341 extern int _stdio_user_locking;
342 #endif
343 
344 #endif
345 
346 #endif
347 /**********************************************************************/
348 
349 #define __CLEARERR_UNLOCKED(__stream)					\
350 	((void)((__stream)->__modeflags &= ~(__FLAG_EOF|__FLAG_ERROR)))
351 #define __FEOF_UNLOCKED(__stream)	((__stream)->__modeflags & __FLAG_EOF)
352 #define __FERROR_UNLOCKED(__stream)	((__stream)->__modeflags & __FLAG_ERROR)
353 
354 #ifdef __UCLIBC_HAS_THREADS__
355 # define __CLEARERR(__stream)		(clearerr)(__stream)
356 # define __FERROR(__stream)		(ferror)(__stream)
357 # define __FEOF(__stream)		(feof)(__stream)
358 #else
359 # define __CLEARERR(__stream)		__CLEARERR_UNLOCKED(__stream)
360 # define __FERROR(__stream)		__FERROR_UNLOCKED(__stream)
361 # define __FEOF(__stream)		__FEOF_UNLOCKED(__stream)
362 #endif
363 
364 extern int __fgetc_unlocked(FILE *__stream);
365 libc_hidden_proto(__fgetc_unlocked)
366 extern int __fputc_unlocked(int __c, FILE *__stream);
367 libc_hidden_proto(__fputc_unlocked)
368 
369 /* First define the default definitions.
370    They are overridden below as necessary. */
371 #define __FGETC_UNLOCKED(__stream)		(__fgetc_unlocked)((__stream))
372 #define __FGETC(__stream)			(fgetc)((__stream))
373 #define __GETC_UNLOCKED_MACRO(__stream)		(__fgetc_unlocked)((__stream))
374 #define __GETC_UNLOCKED(__stream)		(__fgetc_unlocked)((__stream))
375 #define __GETC(__stream)			(fgetc)((__stream))
376 
377 #define __FPUTC_UNLOCKED(__c, __stream)		(__fputc_unlocked)((__c),(__stream))
378 #define __FPUTC(__c, __stream)			(fputc)((__c),(__stream))
379 #define __PUTC_UNLOCKED_MACRO(__c, __stream)	(__fputc_unlocked)((__c),(__stream))
380 #define __PUTC_UNLOCKED(__c, __stream)		(__fputc_unlocked)((__c),(__stream))
381 #define __PUTC(__c, __stream)			(fputc)((__c),(__stream))
382 
383 
384 #ifdef __STDIO_GETC_MACRO
385 
386 extern FILE *__stdin;			/* For getchar() macro. */
387 
388 # undef  __GETC_UNLOCKED_MACRO
389 # define __GETC_UNLOCKED_MACRO(__stream)				\
390 		( ((__stream)->__bufpos < (__stream)->__bufgetc_u)	\
391 		  ? (*(__stream)->__bufpos++)				\
392 		  : __fgetc_unlocked(__stream) )
393 
394 # if 0
395 	/* Classic macro approach.  getc{_unlocked} can have side effects. */
396 #  undef  __GETC_UNLOCKED
397 #  define __GETC_UNLOCKED(__stream)		__GETC_UNLOCKED_MACRO((__stream))
398 #  ifndef __UCLIBC_HAS_THREADS__
399 #   undef  __GETC
400 #   define __GETC(__stream)				__GETC_UNLOCKED_MACRO((__stream))
401 #  endif
402 
403 # else
404 	/* Using gcc extension for safety and additional inlining. */
405 #  undef  __FGETC_UNLOCKED
406 #  define __FGETC_UNLOCKED(__stream)					\
407 		(__extension__ ({					\
408 			FILE *__S = (__stream);				\
409 			__GETC_UNLOCKED_MACRO(__S);			\
410 		}) )
411 
412 #  undef  __GETC_UNLOCKED
413 #  define __GETC_UNLOCKED(__stream)		__FGETC_UNLOCKED((__stream))
414 
415 #  ifdef __UCLIBC_HAS_THREADS__
416 #   undef  __FGETC
417 #   define __FGETC(__stream)						\
418 		(__extension__ ({					\
419 			FILE *__S = (__stream);				\
420 			((__S->__user_locking )				\
421 			 ? __GETC_UNLOCKED_MACRO(__S)			\
422 			 : (fgetc)(__S));				\
423 		}) )
424 
425 #   undef  __GETC
426 #   define __GETC(__stream)			__FGETC((__stream))
427 
428 #  else
429 
430 #   undef  __FGETC
431 #   define __FGETC(__stream)			__FGETC_UNLOCKED((__stream))
432 #   undef  __GETC
433 #   define __GETC(__stream)			__FGETC_UNLOCKED((__stream))
434 
435 #  endif
436 # endif
437 
438 #else
439 
440 # define __stdin stdin
441 
442 #endif /* __STDIO_GETC_MACRO */
443 
444 
445 #ifdef __STDIO_PUTC_MACRO
446 
447 extern FILE *__stdout;			/* For putchar() macro. */
448 
449 # undef  __PUTC_UNLOCKED_MACRO
450 # define __PUTC_UNLOCKED_MACRO(__c, __stream)				\
451 		( ((__stream)->__bufpos < (__stream)->__bufputc_u)	\
452 		  ? (*(__stream)->__bufpos++) = (__c)			\
453 		  : __fputc_unlocked((__c),(__stream)) )
454 
455 # if 0
456 	/* Classic macro approach.  putc{_unlocked} can have side effects.*/
457 #  undef  __PUTC_UNLOCKED
458 #  define __PUTC_UNLOCKED(__c, __stream)				\
459 					__PUTC_UNLOCKED_MACRO((__c), (__stream))
460 #  ifndef __UCLIBC_HAS_THREADS__
461 #   undef  __PUTC
462 #   define __PUTC(__c, __stream)	__PUTC_UNLOCKED_MACRO((__c), (__stream))
463 #  endif
464 
465 # else
466 	/* Using gcc extension for safety and additional inlining. */
467 
468 #  undef  __FPUTC_UNLOCKED
469 #  define __FPUTC_UNLOCKED(__c, __stream)				\
470 		(__extension__ ({					\
471 			FILE *__S = (__stream);				\
472 			__PUTC_UNLOCKED_MACRO((__c),__S);		\
473 		}) )
474 
475 #  undef  __PUTC_UNLOCKED
476 #  define __PUTC_UNLOCKED(__c, __stream)	__FPUTC_UNLOCKED((__c), (__stream))
477 
478 #  ifdef __UCLIBC_HAS_THREADS__
479 #   undef  __FPUTC
480 #   define __FPUTC(__c, __stream)					\
481 		(__extension__ ({					\
482 			FILE *__S = (__stream);				\
483 			((__S->__user_locking)				\
484 			 ? __PUTC_UNLOCKED_MACRO((__c),__S)		\
485 			 : (fputc)((__c),__S));				\
486 		}) )
487 
488 #   undef  __PUTC
489 #   define __PUTC(__c, __stream)		__FPUTC((__c), (__stream))
490 
491 #  else
492 
493 #   undef  __FPUTC
494 #   define __FPUTC(__c, __stream)		__FPUTC_UNLOCKED((__c),(__stream))
495 #   undef  __PUTC
496 #   define __PUTC(__c, __stream)		__FPUTC_UNLOCKED((__c),(__stream))
497 
498 #  endif
499 # endif
500 
501 #else
502 
503 # define __stdout stdout
504 
505 #endif /* __STDIO_PUTC_MACRO */
506