uClibc_stdio.h 15 KB

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