uClibc_stdio.h 16 KB

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