uClibc_stdio.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. * Header for my stdio library for linux and (soon) elks.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the Free
  16. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  19. *
  20. * This code is currently under development. Also, I plan to port
  21. * it to elks which is a 16-bit environment with a fairly limited
  22. * compiler. Therefore, please refrain from modifying this code
  23. * and, instead, pass any bug-fixes, etc. to me. Thanks. Manuel
  24. *
  25. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  26. #ifndef _STDIO_H
  27. #error Always include <stdio.h> rather than <bits/uClibc_stdio.h>
  28. #endif
  29. /**********************************************************************/
  30. #ifdef __UCLIBC__
  31. #ifdef __UCLIBC_HAS_THREADS__
  32. #define __STDIO_THREADSAFE
  33. #endif
  34. #ifdef __UCLIBC_HAS_LFS__
  35. #define __STDIO_LARGE_FILES
  36. #endif /* __UCLIBC_HAS_LFS__ */
  37. #ifdef __UCLIBC_HAS_WCHAR__
  38. #define __STDIO_WIDE
  39. #endif
  40. /* Make sure defines related to large files are consistent. */
  41. #ifdef _LIBC
  42. #ifdef __UCLIBC_HAS_LFS__
  43. #undef __USE_LARGEFILE
  44. #undef __USE_LARGEFILE64
  45. #undef __USE_FILE_OFFSET64
  46. /* if we're actually building uClibc with large file support, only define... */
  47. #define __USE_LARGEFILE64 1
  48. #endif /* __UCLIBC_HAS_LFS__ */
  49. #else /* not _LIBC */
  50. #ifndef __UCLIBC_HAS_LFS__
  51. #if defined(__LARGEFILE64_SOURCE) || defined(__USE_LARGEFILE64) \
  52. || defined(__USE_FILE_OFFSET64)
  53. #error Sorry... uClibc was built without large file support!
  54. #endif
  55. #endif /* __UCLIBC_HAS_LFS__ */
  56. #endif /* _LIBC */
  57. #endif /* __UCLIBC__ */
  58. /**********************************************************************/
  59. /* These are the stdio configuration options. Keep them here until
  60. uClibc's configuration process gets reworked. */
  61. #ifdef __STDIO_WIDE
  62. #define __need_wchar_t
  63. #include <stddef.h>
  64. /* Note: we don't really need mbstate for 8-bit locales. We do for UTF-8.
  65. * For now, always use it. */
  66. #define __STDIO_MBSTATE
  67. #define __need_mbstate_t
  68. #include <wchar.h>
  69. #endif
  70. #define __STDIO_BUFFERS
  71. #define __STDIO_GETC_MACRO
  72. #define __STDIO_PUTC_MACRO
  73. /* For uClibc, these are currently handled above. */
  74. /* #define __STDIO_LARGE_FILES */
  75. /* #define __STDIO_THREADSAFE */
  76. /* L mode extension for fopen. */
  77. #define __STDIO_FOPEN_LARGEFILE_MODE
  78. /* size of builtin buf -- only tested with 0 */
  79. #define _STDIO_BUILTIN_BUF_SIZE 0
  80. /* TODO - enable features based on __STDIO_GLIBC_FEATURES */
  81. /* #define __STDIO_GLIBC_FEATURES */
  82. #define __STDIO_AUTO_RW_TRANSITION
  83. #define __STDIO_FOPEN_EXCLUSIVE_MODE
  84. #define __STDIO_PRINTF_M_SPEC
  85. #define __STDIO_GLIBC_CUSTOM_STREAMS
  86. /* ANSI/ISO mandate at least 256. */
  87. #define _STDIO_BUFSIZ 256
  88. /* Currently unimplemented/untested */
  89. /* #define __STDIO_FLEXIBLE_SETVBUF */
  90. /**********************************************************************/
  91. /* TODO -- posix or gnu -- belongs in limits.h and >= 9 for sus */
  92. /* NOTE: for us it is currently _always_ 9 */
  93. /*#define NL_ARGMAX 9*/
  94. /**********************************************************************/
  95. /* These are consistency checks on the different options */
  96. #ifndef __STDIO_BUFFERS
  97. #undef __STDIO_GETC_MACRO
  98. #undef __STDIO_PUTC_MACRO
  99. #endif
  100. #ifdef __BCC__
  101. #undef __STDIO_LARGE_FILES
  102. #endif
  103. #ifndef __STDIO_LARGE_FILES
  104. #undef __STDIO_FOPEN_LARGEFILE_MODE
  105. #endif
  106. /**********************************************************************/
  107. #ifdef __STDIO_THREADSAFE
  108. /* Need this for pthread_mutex_t. */
  109. #include <bits/pthreadtypes.h>
  110. #define __STDIO_THREADLOCK(STREAM) \
  111. if ((STREAM)->user_locking == 0) { \
  112. pthread_mutex_lock(&(STREAM)->lock); \
  113. }
  114. #define __STDIO_THREADUNLOCK(STREAM) \
  115. if ((STREAM)->user_locking == 0) { \
  116. pthread_mutex_unlock(&(STREAM)->lock); \
  117. }
  118. #define __STDIO_THREADTRYLOCK(STREAM) \
  119. if ((STREAM)->user_locking == 0) { \
  120. pthread_mutex_trylock(&(STREAM)->lock); \
  121. }
  122. #else /* __STDIO_THREADSAFE */
  123. #define __STDIO_THREADLOCK(STREAM)
  124. #define __STDIO_THREADUNLOCK(STREAM)
  125. #define __STDIO_THREADTRYLOCK(STREAM)
  126. #endif /* __STDIO_THREADSAFE */
  127. /* This file may eventually have two personalities:
  128. 1) core stuff (similar to glibc's libio.h)
  129. 2) extern inlines (for glibc's bits/stdio.h)
  130. Right now, only (1) is implemented. */
  131. #define _STDIO_IOFBF 0 /* Fully buffered. */
  132. #define _STDIO_IOLBF 1 /* Line buffered. */
  133. #define _STDIO_IONBF 2 /* No buffering. */
  134. typedef struct {
  135. __off_t __pos;
  136. #ifdef __STDIO_MBSTATE
  137. __mbstate_t __mbstate;
  138. #endif
  139. } __stdio_fpos_t;
  140. #ifdef __STDIO_LARGE_FILES
  141. typedef struct {
  142. __off64_t __pos;
  143. #ifdef __STDIO_MBSTATE
  144. __mbstate_t __mbstate;
  145. #endif
  146. } __stdio_fpos64_t;
  147. #endif
  148. /**********************************************************************/
  149. #ifdef __STDIO_LARGE_FILES
  150. typedef __off64_t __offmax_t; /* TODO -- rename this? */
  151. #else
  152. typedef __off_t __offmax_t; /* TODO -- rename this? */
  153. #endif
  154. /**********************************************************************/
  155. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  156. typedef __ssize_t __io_read_fn(void *cookie,
  157. char *buf, size_t bufsize);
  158. typedef __ssize_t __io_write_fn(void *cookie,
  159. const char *buf, size_t bufsize);
  160. /* NOTE: GLIBC difference!!! -- fopencookie seek function
  161. * For glibc, the type of pos is always (__off64_t *) but in our case
  162. * it is type (__off_t *) when the lib is built without large file support.
  163. */
  164. typedef int __io_seek_fn(void *cookie,
  165. __offmax_t *pos, int whence);
  166. typedef int __io_close_fn(void *cookie);
  167. typedef struct {
  168. __io_read_fn *read;
  169. __io_write_fn *write;
  170. __io_seek_fn *seek;
  171. __io_close_fn *close;
  172. } _IO_cookie_io_functions_t;
  173. #if defined(_LIBC) || defined(_GNU_SOURCE)
  174. typedef __io_read_fn cookie_read_function_t;
  175. typedef __io_write_fn cookie_write_function_t;
  176. typedef __io_seek_fn cookie_seek_function_t;
  177. typedef __io_close_fn cookie_close_function_t;
  178. typedef _IO_cookie_io_functions_t cookie_io_functions_t;
  179. #endif /* _GNU_SOURCE */
  180. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  181. /*
  182. * ungot scheme:
  183. * 0 0 none
  184. * 0 1 one user (unused ungot is 1) or one scanf (unused ungot is 0)
  185. * 1 0 must be scanf[0] and user[1]
  186. * 1 1 illegal -- could be used to signal safe for setbuf
  187. */
  188. #ifdef __UCLIBC__
  189. #define __stdio_file_struct _UC_FILE
  190. #endif
  191. struct __stdio_file_struct {
  192. unsigned short modeflags;
  193. /* There could be a hole here, but modeflags is used most.*/
  194. #ifdef __STDIO_WIDE
  195. /* TOOD - ungot_width could be combined with ungot. But what about hole? */
  196. unsigned char ungot_width[2];
  197. wchar_t ungot[2];
  198. #else /* __STDIO_WIDE */
  199. unsigned char ungot[2];
  200. #endif /* __STDIO_WIDE */
  201. int filedes;
  202. #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  203. struct __stdio_file_struct *nextopen;
  204. #endif /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  205. #ifdef __STDIO_BUFFERS
  206. unsigned char *bufstart; /* pointer to buffer */
  207. unsigned char *bufend; /* pointer to 1 past end of buffer */
  208. unsigned char *bufpos;
  209. unsigned char *bufread; /* pointer to 1 past last buffered read char. */
  210. #ifdef __STDIO_GETC_MACRO
  211. unsigned char *bufgetc; /* 1 past last readable by getc */
  212. #endif /* __STDIO_GETC_MACRO */
  213. #ifdef __STDIO_PUTC_MACRO
  214. unsigned char *bufputc; /* 1 past last writeable by putc */
  215. #endif /* __STDIO_PUTC_MACRO */
  216. #endif /* __STDIO_BUFFERS */
  217. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  218. void *cookie;
  219. _IO_cookie_io_functions_t gcs;
  220. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  221. #ifdef __STDIO_MBSTATE
  222. __mbstate_t state;
  223. #endif
  224. #ifdef __STDIO_THREADSAFE
  225. int user_locking;
  226. pthread_mutex_t lock;
  227. #endif
  228. /* Everything after this is unimplemented... and may be trashed. */
  229. #if __STDIO_BUILTIN_BUF_SIZE > 0
  230. unsigned char builtinbuf[__STDIO_BUILTIN_BUF_SIZE];
  231. #endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  232. };
  233. /***********************************************************************/
  234. #define __MASK_UNGOT (0x0002|0x0001)
  235. #define __MASK_UNGOT1 0x0001
  236. #define __MASK_UNGOT2 0x0002
  237. #define __FLAG_EOF 0x0004 /* EOF reached? */
  238. #define __FLAG_ERROR 0x0008 /* stream in error state? */
  239. #define __FLAG_WRITEONLY 0x0010 /* unreadable */
  240. #define __FLAG_READONLY 0x0020 /* unwriteable */
  241. #define __FLAG_FREEFILE 0x0040 /* free FILE struct after use */
  242. #define __FLAG_NARROW 0x0080
  243. #define __FLAG_FBF 0 /* convenience value */
  244. #define __FLAG_LBF 0x0100
  245. #define __FLAG_NBF 0x0200
  246. #define __MASK_BUFMODE 0x0300
  247. #define __FLAG_APPEND 0x0400
  248. #define __FLAG_WIDE 0x0800
  249. #define __FLAG_READING 0x1000
  250. #define __FLAG_WRITING 0x2000
  251. #define __FLAG_FREEBUF 0x4000 /* free buffer after use */
  252. #define __FLAG_LARGEFILE 0x8000
  253. /**********************************************************************/
  254. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  255. extern __ssize_t _cs_read(void *cookie, char *buf, size_t bufsize);
  256. extern __ssize_t _cs_write(void *cookie, const char *buf, size_t bufsize);
  257. extern int _cs_seek(void *cookie, __offmax_t *pos, int whence);
  258. extern int _cs_close(void *cookie);
  259. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  260. /**********************************************************************/
  261. #ifdef __STDIO_MBSTATE
  262. #define __COPY_MBSTATE(dest,src) ((dest)->mask = (src)->mask, (dest)->wc = (src)->wc)
  263. #define __INIT_MBSTATE(dest) ((dest)->mask = 0)
  264. #else
  265. #define __COPY_MBSTATE(dest,src)
  266. #define __INIT_MBSTATE(dest)
  267. #endif
  268. /**********************************************************************/
  269. /* TODO -- thread safety issues */
  270. #define __CLEARERR(stream) \
  271. ((stream)->modeflags &= ~(__FLAG_EOF|__FLAG_ERROR), (void)0)
  272. #define __FEOF(stream) ((stream)->modeflags & __FLAG_EOF)
  273. #define __FERROR(stream) ((stream)->modeflags & __FLAG_ERROR)
  274. #define __FEOF_OR_FERROR(stream) \
  275. ((stream)->modeflags & (__FLAG_EOF|__FLAG_ERROR))
  276. /* TODO: check this
  277. * If we want to implement the getc and putc macros, we need to take
  278. * into account wide streams. So... would need two additional variables
  279. * if we have wide streams (bufread and bufwrite), and one otherwise
  280. * (bufwrite). getc would be effective for FBF streams. It isn't for
  281. * LBF streams because other LBF streams need to be flushed. putc
  282. * thouch is only effective for FBF streams. Of course, to support
  283. * threads, we have to use functions.
  284. */
  285. #ifdef __STDIO_GETC_MACRO
  286. #define __GETC(stream) ( ((stream)->bufpos < (stream)->bufgetc) \
  287. ? (*(stream)->bufpos++) \
  288. : fgetc_unlocked(stream) )
  289. #else /* __STDIO_GETC_MACRO */
  290. #define __GETC(stream) fgetc_unlocked(stream)
  291. #endif /* __STDIO_GETC_MACRO */
  292. #ifdef __STDIO_PUTC_MACRO
  293. #define __PUTC(c, stream) ( ((stream)->bufpos < (stream)->bufputc) \
  294. ? (*(stream)->bufpos++) = (c) \
  295. : fputc_unlocked((c),(stream)) )
  296. #else /* __STDIO_PUTC_MACRO */
  297. #define __PUTC(c, stream) fputc_unlocked(c, stream);
  298. #endif /* __STDIO_PUTC_MACRO */
  299. #if 0
  300. /* TODO: disabled for now */
  301. /* Masking macros for the above _are_ allowed by the standard. */
  302. #define clearerr(stream) __CLEARERR(stream)
  303. #define feof(stream) __FEOF(stream)
  304. #define ferror(stream) __FERROR(stream)
  305. #endif
  306. #if 0
  307. /* TODO -- what about custom streams!!! */
  308. /* Only use the macro below if you know fp is a valid FILE for a valid fd. */
  309. #define __fileno(fp) ((fp)->filedes)
  310. #endif
  311. /**********************************************************************
  312. * PROTOTYPES OF INTERNAL FUNCTIONS
  313. **********************************************************************/
  314. extern FILE *_stdio_openlist;
  315. #ifdef __STDIO_THREADSAFE
  316. extern pthread_mutex_t _stdio_openlist_lock;
  317. extern void __stdio_init_mutex(pthread_mutex_t *m);
  318. #endif
  319. extern int _stdio_adjpos(FILE * __restrict stream, __offmax_t * pos);
  320. extern int _stdio_lseek(FILE *stream, __offmax_t *pos, int whence);
  321. /* TODO: beware of signals with _stdio_fwrite!!!! */
  322. extern size_t _stdio_fwrite(const unsigned char *buffer, size_t bytes,
  323. FILE *stream);
  324. extern size_t _stdio_fread(unsigned char *buffer, size_t bytes,
  325. FILE *stream);
  326. extern FILE *_stdio_fopen(const char * __restrict filename,
  327. const char * __restrict mode,
  328. FILE * __restrict stream, int filedes);
  329. extern FILE *_stdio_fsfopen(const char * __restrict filename,
  330. const char * __restrict mode,
  331. register FILE * __restrict stream);
  332. extern void _stdio_init(void);
  333. extern void _stdio_term(void);
  334. #ifndef NDEBUG
  335. extern void __stdio_validate_FILE(FILE *stream);
  336. #else
  337. #define __stdio_validate_FILE(stream) ((void)0)
  338. #endif
  339. /**********************************************************************
  340. * UTILITY functions
  341. **********************************************************************/
  342. #ifdef _STDIO_UTILITY
  343. #include <features.h>
  344. #include <limits.h>
  345. #include <stdint.h>
  346. #if INTMAX_MAX <= 2147483647L
  347. #define __UIM_BUFLEN 12 /* 10 digits + 1 nul + 1 sign */
  348. #elif INTMAX_MAX <= 9223372036854775807LL
  349. #define __UIM_BUFLEN 22 /* 20 digits + 1 nul + 1 sign */
  350. #else
  351. #error unknown number of digits for intmax_t!
  352. #endif
  353. #ifdef LLONG_MAX /* --------------- */
  354. #if LLONG_MAX <= 2147483647L
  355. #define __UIM_BUFLEN_LLONG 12 /* 10 digits + 1 nul + 1 sign */
  356. #elif LLONG_MAX <= 9223372036854775807LL
  357. #define __UIM_BUFLEN_LLONG 22 /* 20 digits + 1 nul + 1 sign */
  358. #else
  359. #error unknown number of digits for long long!
  360. #endif
  361. #endif /* ULLONG_MAX ----------------------------- */
  362. #if LONG_MAX <= 2147483647L
  363. #define __UIM_BUFLEN_LONG 12 /* 10 digits + 1 nul + 1 sign */
  364. #elif LONG_MAX <= 9223372036854775807LL
  365. #define __UIM_BUFLEN_LONG 22 /* 20 digits + 1 nul + 1 sign */
  366. #else
  367. #error unknown number of digits for long!
  368. #endif
  369. #if INT_MAX <= 32767
  370. #define __UIM_BUFLEN_INT 7 /* 10 digits + 1 nul + 1 sign */
  371. #elif INT_MAX <= 2147483647L
  372. #define __UIM_BUFLEN_INT 12 /* 10 digits + 1 nul + 1 sign */
  373. #else
  374. #error unknown number of digits for int!
  375. #endif
  376. typedef enum {
  377. __UIM_DECIMAL = 0,
  378. __UIM_LOWER = 'a' - 10,
  379. __UIM_UPPER = 'A' - 10,
  380. } __UIM_CASE;
  381. /* Write a NULL-terminated list of "char *" args to file descriptor fd.
  382. * For an example of usage, see __assert.c.
  383. */
  384. extern void _stdio_fdout(int fd, ...);
  385. /* Convert the int val to a string in base abs(base). val is treated as
  386. * an unsigned ??? int type if base > 0, and signed if base < 0. This
  387. * is an internal function with _no_ error checking done unless assert()s
  388. * are enabled.
  389. *
  390. * Note: bufend is a pointer to the END of the buffer passed.
  391. * Call like this:
  392. * char buf[SIZE], *p;
  393. * p = _xltostr(buf + sizeof(buf) - 1, {unsigned int}, 10, __UIM_DECIMAL)
  394. * p = _xltostr(buf + sizeof(buf) - 1, {int}, -10, __UIM_DECIMAL)
  395. *
  396. * WARNING: If base > 10, case _must_be_ either __UIM_LOWER or __UIM_UPPER
  397. * for lower and upper case alphas respectively.
  398. * WARNING: If val is really a signed type, make sure base is negative!
  399. * Otherwise, you could overflow your buffer.
  400. */
  401. extern char *_uintmaxtostr(char * __restrict bufend, uintmax_t uval,
  402. int base, __UIM_CASE alphacase);
  403. /* TODO -- make this either a (possibly inline) function? */
  404. #ifndef __BCC__
  405. #define _int10tostr(bufend, intval) \
  406. _uintmaxtostr((bufend), (intval), -10, __UIM_DECIMAL)
  407. #else /* bcc doesn't do prototypes, we need to explicitly cast */
  408. #define _int10tostr(bufend, intval) \
  409. _uintmaxtostr((bufend), (uintmax_t)(intval), -10, __UIM_DECIMAL)
  410. #endif
  411. #define __BUFLEN_INT10TOSTR __UIM_BUFLEN_INT
  412. #endif /* _STDIO_UTILITY */
  413. /**********************************************************************/
  414. /* uClibc translations */
  415. /**********************************************************************/
  416. /* TODO: note done above.. typedef struct __stdio_file_struct _UC_FILE; */
  417. typedef __stdio_fpos_t _UC_fpos_t;
  418. #ifdef __STDIO_LARGE_FILES
  419. typedef __stdio_fpos64_t _UC_fpos64_t;
  420. #endif
  421. #define _UC_IOFBF _STDIO_IOFBF /* Fully buffered. */
  422. #define _UC_IOLBF _STDIO_IOLBF /* Line buffered. */
  423. #define _UC_IONBF _STDIO_IONBF /* No buffering. */
  424. #define _UC_BUFSIZ _STDIO_BUFSIZ