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