libio.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>.
  14. As a special exception, if you link the code in this file with
  15. files compiled with a GNU compiler to produce an executable,
  16. that does not cause the resulting executable to be covered by
  17. the GNU Lesser General Public License. This exception does not
  18. however invalidate any other reasons why the executable file
  19. might be covered by the GNU Lesser General Public License.
  20. This exception applies to code released by its copyright holders
  21. in files containing the exception. */
  22. #ifndef _LIBIO_H
  23. #define _LIBIO_H 1
  24. #ifndef _LIBC
  25. # error "libio.h should only be included when building glibc itself"
  26. #endif
  27. #ifdef _ISOMAC
  28. # error "libio.h should not be included under _ISOMAC"
  29. #endif
  30. #include <stdio.h>
  31. #if defined _IO_MTSAFE_IO && !defined _IO_lock_t_defined
  32. # error "Someone forgot to include stdio-lock.h"
  33. #endif
  34. #define __need_wchar_t
  35. #include <stddef.h>
  36. #include <bits/types/__mbstate_t.h>
  37. #include <bits/types/wint_t.h>
  38. #include <gconv.h>
  39. typedef struct
  40. {
  41. struct __gconv_step *step;
  42. struct __gconv_step_data step_data;
  43. } _IO_iconv_t;
  44. #include <shlib-compat.h>
  45. /* _IO_seekoff modes */
  46. #define _IOS_INPUT 1
  47. #define _IOS_OUTPUT 2
  48. /* Magic number and bits for the _flags field. The magic number is
  49. mostly vestigial, but preserved for compatibility. It occupies the
  50. high 16 bits of _flags; the low 16 bits are actual flag bits. */
  51. #define _IO_MAGIC 0xFBAD0000 /* Magic number */
  52. #define _IO_MAGIC_MASK 0xFFFF0000
  53. #define _IO_USER_BUF 0x0001 /* Don't deallocate buffer on close. */
  54. #define _IO_UNBUFFERED 0x0002
  55. #define _IO_NO_READS 0x0004 /* Reading not allowed. */
  56. #define _IO_NO_WRITES 0x0008 /* Writing not allowed. */
  57. #define _IO_EOF_SEEN 0x0010
  58. #define _IO_ERR_SEEN 0x0020
  59. #define _IO_DELETE_DONT_CLOSE 0x0040 /* Don't call close(_fileno) on close. */
  60. #define _IO_LINKED 0x0080 /* In the list of all open files. */
  61. #define _IO_IN_BACKUP 0x0100
  62. #define _IO_LINE_BUF 0x0200
  63. #define _IO_TIED_PUT_GET 0x0400 /* Put and get pointer move in unison. */
  64. #define _IO_CURRENTLY_PUTTING 0x0800
  65. #define _IO_IS_APPENDING 0x1000
  66. #define _IO_IS_FILEBUF 0x2000
  67. /* 0x4000 No longer used, reserved for compat. */
  68. #define _IO_USER_LOCK 0x8000
  69. /* Bits for the _flags2 field. */
  70. #define _IO_FLAGS2_MMAP 1
  71. #define _IO_FLAGS2_NOTCANCEL 2
  72. #define _IO_FLAGS2_USER_WBUF 8
  73. #define _IO_FLAGS2_NOCLOSE 32
  74. #define _IO_FLAGS2_CLOEXEC 64
  75. #define _IO_FLAGS2_NEED_LOCK 128
  76. /* _IO_pos_BAD is an off64_t value indicating error, unknown, or EOF. */
  77. #define _IO_pos_BAD ((off64_t) -1)
  78. /* _IO_pos_adjust adjusts an off64_t by some number of bytes. */
  79. #define _IO_pos_adjust(pos, delta) ((pos) += (delta))
  80. /* _IO_pos_0 is an off64_t value indicating beginning of file. */
  81. #define _IO_pos_0 ((off64_t) 0)
  82. struct _IO_jump_t;
  83. /* A streammarker remembers a position in a buffer. */
  84. struct _IO_marker {
  85. struct _IO_marker *_next;
  86. FILE *_sbuf;
  87. /* If _pos >= 0
  88. it points to _buf->Gbase()+_pos. FIXME comment */
  89. /* if _pos < 0, it points to _buf->eBptr()+_pos. FIXME comment */
  90. int _pos;
  91. };
  92. struct _IO_codecvt
  93. {
  94. _IO_iconv_t __cd_in;
  95. _IO_iconv_t __cd_out;
  96. };
  97. /* Extra data for wide character streams. */
  98. struct _IO_wide_data
  99. {
  100. wchar_t *_IO_read_ptr; /* Current read pointer */
  101. wchar_t *_IO_read_end; /* End of get area. */
  102. wchar_t *_IO_read_base; /* Start of putback+get area. */
  103. wchar_t *_IO_write_base; /* Start of put area. */
  104. wchar_t *_IO_write_ptr; /* Current put pointer. */
  105. wchar_t *_IO_write_end; /* End of put area. */
  106. wchar_t *_IO_buf_base; /* Start of reserve area. */
  107. wchar_t *_IO_buf_end; /* End of reserve area. */
  108. /* The following fields are used to support backing up and undo. */
  109. wchar_t *_IO_save_base; /* Pointer to start of non-current get area. */
  110. wchar_t *_IO_backup_base; /* Pointer to first valid character of
  111. backup area */
  112. wchar_t *_IO_save_end; /* Pointer to end of non-current get area. */
  113. __mbstate_t _IO_state;
  114. __mbstate_t _IO_last_state;
  115. struct _IO_codecvt _codecvt;
  116. wchar_t _shortbuf[1];
  117. const struct _IO_jump_t *_wide_vtable;
  118. };
  119. struct _IO_FILE_plus;
  120. extern struct _IO_FILE_plus _IO_2_1_stdin_;
  121. extern struct _IO_FILE_plus _IO_2_1_stdout_;
  122. extern struct _IO_FILE_plus _IO_2_1_stderr_;
  123. struct _IO_cookie_file;
  124. /* Initialize one of those. */
  125. extern void _IO_cookie_init (struct _IO_cookie_file *__cfile, int __read_write,
  126. void *__cookie, cookie_io_functions_t __fns);
  127. extern int __underflow (FILE *);
  128. extern wint_t __wunderflow (FILE *);
  129. extern wint_t __wuflow (FILE *);
  130. extern wint_t __woverflow (FILE *, wint_t);
  131. #define _IO_getc_unlocked(_fp) __getc_unlocked_body (_fp)
  132. #define _IO_peekc_unlocked(_fp) \
  133. (__glibc_unlikely ((_fp)->_IO_read_ptr >= (_fp)->_IO_read_end) \
  134. && __underflow (_fp) == EOF \
  135. ? EOF \
  136. : *(unsigned char *) (_fp)->_IO_read_ptr)
  137. #define _IO_putc_unlocked(_ch, _fp) __putc_unlocked_body (_ch, _fp)
  138. # define _IO_getwc_unlocked(_fp) \
  139. (__glibc_unlikely ((_fp)->_wide_data == NULL \
  140. || ((_fp)->_wide_data->_IO_read_ptr \
  141. >= (_fp)->_wide_data->_IO_read_end)) \
  142. ? __wuflow (_fp) : (wint_t) *(_fp)->_wide_data->_IO_read_ptr++)
  143. # define _IO_putwc_unlocked(_wch, _fp) \
  144. (__glibc_unlikely ((_fp)->_wide_data == NULL \
  145. || ((_fp)->_wide_data->_IO_write_ptr \
  146. >= (_fp)->_wide_data->_IO_write_end)) \
  147. ? __woverflow (_fp, _wch) \
  148. : (wint_t) (*(_fp)->_wide_data->_IO_write_ptr++ = (_wch)))
  149. #define _IO_feof_unlocked(_fp) __feof_unlocked_body (_fp)
  150. #define _IO_ferror_unlocked(_fp) __ferror_unlocked_body (_fp)
  151. extern int _IO_getc (FILE *__fp);
  152. extern int _IO_putc (int __c, FILE *__fp);
  153. extern int _IO_feof (FILE *__fp) __THROW;
  154. extern int _IO_ferror (FILE *__fp) __THROW;
  155. extern int _IO_peekc_locked (FILE *__fp);
  156. /* This one is for Emacs. */
  157. #define _IO_PENDING_OUTPUT_COUNT(_fp) \
  158. ((_fp)->_IO_write_ptr - (_fp)->_IO_write_base)
  159. extern void _IO_flockfile (FILE *) __THROW;
  160. extern void _IO_funlockfile (FILE *) __THROW;
  161. extern int _IO_ftrylockfile (FILE *) __THROW;
  162. #define _IO_peekc(_fp) _IO_peekc_unlocked (_fp)
  163. #define _IO_flockfile(_fp) /**/
  164. #define _IO_funlockfile(_fp) ((void) 0)
  165. #define _IO_ftrylockfile(_fp) /**/
  166. #ifndef _IO_cleanup_region_start
  167. #define _IO_cleanup_region_start(_fct, _fp) /**/
  168. #endif
  169. #ifndef _IO_cleanup_region_end
  170. #define _IO_cleanup_region_end(_Doit) /**/
  171. #endif
  172. #define _IO_need_lock(_fp) \
  173. (((_fp)->_flags2 & _IO_FLAGS2_NEED_LOCK) != 0)
  174. extern int _IO_vfscanf (FILE * __restrict, const char * __restrict,
  175. __gnuc_va_list, int *__restrict);
  176. extern __ssize_t _IO_padn (FILE *, int, __ssize_t);
  177. extern size_t _IO_sgetn (FILE *, void *, size_t);
  178. extern off64_t _IO_seekoff (FILE *, off64_t, int, int);
  179. extern off64_t _IO_seekpos (FILE *, off64_t, int);
  180. extern void _IO_free_backup_area (FILE *) __THROW;
  181. extern wint_t _IO_getwc (FILE *__fp);
  182. extern wint_t _IO_putwc (wchar_t __wc, FILE *__fp);
  183. extern int _IO_fwide (FILE *__fp, int __mode) __THROW;
  184. #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
  185. # define _IO_fwide_maybe_incompatible \
  186. (__glibc_unlikely (&_IO_stdin_used == NULL))
  187. extern const int _IO_stdin_used;
  188. weak_extern (_IO_stdin_used);
  189. #else
  190. # define _IO_fwide_maybe_incompatible (0)
  191. #endif
  192. /* A special optimized version of the function above. It optimizes the
  193. case of initializing an unoriented byte stream. */
  194. #define _IO_fwide(__fp, __mode) \
  195. ({ int __result = (__mode); \
  196. if (__result < 0 && ! _IO_fwide_maybe_incompatible) \
  197. { \
  198. if ((__fp)->_mode == 0) \
  199. /* We know that all we have to do is to set the flag. */ \
  200. (__fp)->_mode = -1; \
  201. __result = (__fp)->_mode; \
  202. } \
  203. else if (__builtin_constant_p (__mode) && (__mode) == 0) \
  204. __result = _IO_fwide_maybe_incompatible ? -1 : (__fp)->_mode; \
  205. else \
  206. __result = _IO_fwide (__fp, __result); \
  207. __result; })
  208. extern __ssize_t _IO_wpadn (FILE *, wint_t, __ssize_t);
  209. extern void _IO_free_wbackup_area (FILE *) __THROW;
  210. #ifdef __LDBL_COMPAT
  211. __LDBL_REDIR_DECL (_IO_vfscanf)
  212. #endif
  213. libc_hidden_proto (__overflow)
  214. libc_hidden_proto (__underflow)
  215. libc_hidden_proto (__uflow)
  216. libc_hidden_proto (__woverflow)
  217. libc_hidden_proto (__wunderflow)
  218. libc_hidden_proto (__wuflow)
  219. libc_hidden_proto (_IO_free_backup_area)
  220. libc_hidden_proto (_IO_free_wbackup_area)
  221. libc_hidden_proto (_IO_padn)
  222. libc_hidden_proto (_IO_putc)
  223. libc_hidden_proto (_IO_sgetn)
  224. #ifdef _IO_MTSAFE_IO
  225. # undef _IO_peekc
  226. # undef _IO_flockfile
  227. # undef _IO_funlockfile
  228. # undef _IO_ftrylockfile
  229. # define _IO_peekc(_fp) _IO_peekc_locked (_fp)
  230. # define _IO_flockfile(_fp) \
  231. if (((_fp)->_flags & _IO_USER_LOCK) == 0) _IO_lock_lock (*(_fp)->_lock)
  232. # define _IO_funlockfile(_fp) \
  233. if (((_fp)->_flags & _IO_USER_LOCK) == 0) _IO_lock_unlock (*(_fp)->_lock)
  234. #endif /* _IO_MTSAFE_IO */
  235. #endif /* _LIBIO_H */