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