The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* $OpenBSD: local.h,v 1.12 2005/10/10 17:37:44 espie Exp $ */ |
| 2 | |
| 3 | /*- |
| 4 | * Copyright (c) 1990, 1993 |
| 5 | * The Regents of the University of California. All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software contributed to Berkeley by |
| 8 | * Chris Torek. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. Neither the name of the University nor the names of its contributors |
| 19 | * may be used to endorse or promote products derived from this software |
| 20 | * without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 32 | * SUCH DAMAGE. |
| 33 | */ |
| 34 | |
Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 35 | #pragma once |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 36 | |
| 37 | #include <pthread.h> |
| 38 | #include <stdbool.h> |
| 39 | #include <wchar.h> |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 40 | |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 41 | #if defined(__cplusplus) // Until we fork all of stdio... |
Elliott Hughes | cc3d04f | 2017-10-26 15:38:06 -0700 | [diff] [blame] | 42 | #include "private/bionic_fortify.h" |
| 43 | #endif |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 44 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 | /* |
| 46 | * Information local to this implementation of stdio, |
| 47 | * in particular, macros and private variables. |
| 48 | */ |
| 49 | |
Elliott Hughes | c48c3e4 | 2014-11-19 15:16:51 -0800 | [diff] [blame] | 50 | __BEGIN_DECLS |
| 51 | |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 52 | struct __sbuf { |
| 53 | unsigned char* _base; |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 54 | size_t _size; |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | struct __sFILE { |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 58 | unsigned char* _p; /* current position in (some) buffer */ |
| 59 | int _r; /* read space left for getc() */ |
| 60 | int _w; /* write space left for putc() */ |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 61 | #if defined(__LP64__) |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 62 | int _flags; /* flags, below; this FILE is free if 0 */ |
| 63 | int _file; /* fileno, if Unix descriptor, else -1 */ |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 64 | #else |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 65 | short _flags; /* flags, below; this FILE is free if 0 */ |
| 66 | short _file; /* fileno, if Unix descriptor, else -1 */ |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 67 | #endif |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 68 | struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ |
| 69 | int _lbfsize; /* 0 or -_bf._size, for inline putc */ |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 70 | |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 71 | // Function pointers used by `funopen`. |
| 72 | // Note that `_seek` is ignored if `_seek64` (in __sfileext) is set. |
| 73 | // TODO: NetBSD has `funopen2` which corrects the `int`s to `size_t`s. |
| 74 | // TODO: glibc has `fopencookie` which passes the function pointers in a struct. |
| 75 | void* _cookie; /* cookie passed to io functions */ |
| 76 | int (*_close)(void*); |
| 77 | int (*_read)(void*, char*, int); |
| 78 | fpos_t (*_seek)(void*, fpos_t, int); |
| 79 | int (*_write)(void*, const char*, int); |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 80 | |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 81 | /* extension data, to avoid further ABI breakage */ |
| 82 | struct __sbuf _ext; |
| 83 | /* data for long sequences of ungetc() */ |
| 84 | unsigned char* _up; /* saved _p when _p is doing ungetc data */ |
| 85 | int _ur; /* saved _r when _r is counting ungetc data */ |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 86 | |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 87 | /* tricks to meet minimum requirements even when malloc() fails */ |
| 88 | unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ |
| 89 | unsigned char _nbuf[1]; /* guarantee a getc() buffer */ |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 90 | |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 91 | /* separate buffer for fgetln() when line crosses buffer boundary */ |
| 92 | struct __sbuf _lb; /* buffer for fgetln() */ |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 93 | |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 94 | /* Unix stdio files get aligned to block boundaries on fseek() */ |
| 95 | int _blksize; /* stat.st_blksize (may be != _bf._size) */ |
Elliott Hughes | b877601 | 2016-01-22 19:18:48 -0800 | [diff] [blame] | 96 | |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 97 | fpos_t _unused_0; // This was the `_offset` field (see below). |
Elliott Hughes | b877601 | 2016-01-22 19:18:48 -0800 | [diff] [blame] | 98 | |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 99 | // Do not add new fields here. (Or remove or change the size of any above.) |
| 100 | // Although bionic currently exports `stdin`, `stdout`, and `stderr` symbols, |
| 101 | // that still hasn't made it to the NDK. All NDK-built apps index directly |
| 102 | // into an array of this struct (which was in <stdio.h> historically), so if |
| 103 | // you need to make any changes, they need to be in the `__sfileext` struct |
| 104 | // below, and accessed via `_EXT`. |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Elliott Hughes | 5a98234 | 2018-02-28 12:55:46 -0800 | [diff] [blame] | 107 | /* minimal requirement of SUSv2 */ |
| 108 | #define WCIO_UNGETWC_BUFSIZE 1 |
| 109 | |
| 110 | struct wchar_io_data { |
| 111 | mbstate_t wcio_mbstate_in; |
| 112 | mbstate_t wcio_mbstate_out; |
| 113 | |
| 114 | wchar_t wcio_ungetwc_buf[WCIO_UNGETWC_BUFSIZE]; |
| 115 | size_t wcio_ungetwc_inbuf; |
| 116 | |
| 117 | int wcio_mode; /* orientation */ |
| 118 | }; |
| 119 | |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 120 | struct __sfileext { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 121 | // ungetc buffer. |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 122 | struct __sbuf _ub; |
| 123 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 124 | // Wide char io status. |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 125 | struct wchar_io_data _wcio; |
| 126 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 127 | // File lock. |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 128 | pthread_mutex_t _lock; |
| 129 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 130 | // __fsetlocking support. |
Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 131 | bool _caller_handles_locking; |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 132 | |
| 133 | // Equivalent to `_seek` but for _FILE_OFFSET_BITS=64. |
| 134 | // Callers should use this but fall back to `__sFILE::_seek`. |
| 135 | off64_t (*_seek64)(void*, off64_t, int); |
Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 136 | |
| 137 | // The pid of the child if this FILE* is from popen(3). |
| 138 | pid_t _popen_pid; |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Elliott Hughes | e70e0e9 | 2016-01-25 11:10:47 -0800 | [diff] [blame] | 141 | // Values for `__sFILE::_flags`. |
| 142 | #define __SLBF 0x0001 // Line buffered. |
| 143 | #define __SNBF 0x0002 // Unbuffered. |
Elliott Hughes | 5ba2c21 | 2017-08-01 15:16:36 -0700 | [diff] [blame] | 144 | // __SRD and __SWR are mutually exclusive because they indicate what we did last. |
| 145 | // If you want to know whether we were opened read-write, check __SRW instead. |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 146 | #define __SRD 0x0004 // Last operation was read. |
| 147 | #define __SWR 0x0008 // Last operation was write. |
| 148 | #define __SRW 0x0010 // Was opened for reading & writing. |
Elliott Hughes | e70e0e9 | 2016-01-25 11:10:47 -0800 | [diff] [blame] | 149 | #define __SEOF 0x0020 // Found EOF. |
| 150 | #define __SERR 0x0040 // Found error. |
| 151 | #define __SMBF 0x0080 // `_buf` is from malloc. |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 152 | // #define __SAPP 0x0100 --- historical (fdopen()ed in append mode). |
Elliott Hughes | e70e0e9 | 2016-01-25 11:10:47 -0800 | [diff] [blame] | 153 | #define __SSTR 0x0200 // This is an sprintf/snprintf string. |
| 154 | // #define __SOPT 0x0400 --- historical (do fseek() optimization). |
| 155 | // #define __SNPT 0x0800 --- historical (do not do fseek() optimization). |
| 156 | // #define __SOFF 0x1000 --- historical (set iff _offset is in fact correct). |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 157 | // #define __SMOD 0x2000 --- historical (set iff fgetln modified _p text). |
Elliott Hughes | e70e0e9 | 2016-01-25 11:10:47 -0800 | [diff] [blame] | 158 | #define __SALC 0x4000 // Allocate string space dynamically. |
| 159 | #define __SIGN 0x8000 // Ignore this file in _fwalk. |
| 160 | |
Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame] | 161 | // TODO: remove remaining references to these obsolete flags (see above). |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 162 | #define __SMOD 0 |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 163 | #define __SNPT 0 |
| 164 | #define __SOPT 0 |
| 165 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 166 | #define _EXT(fp) __BIONIC_CAST(reinterpret_cast, struct __sfileext*, (fp)->_ext._base) |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 167 | |
| 168 | #define _UB(fp) _EXT(fp)->_ub |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 169 | |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 170 | #define _FILEEXT_SETUP(fp, fext) \ |
| 171 | do { \ |
Elliott Hughes | 1a56a26 | 2017-12-20 08:53:49 -0800 | [diff] [blame] | 172 | (fp)->_ext._base = __BIONIC_CAST(reinterpret_cast, unsigned char*, fext); \ |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 173 | memset(_EXT(fp), 0, sizeof(struct __sfileext)); \ |
| 174 | _EXT(fp)->_caller_handles_locking = true; \ |
Elliott Hughes | 1a56a26 | 2017-12-20 08:53:49 -0800 | [diff] [blame] | 175 | } while (0) |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 176 | |
Elliott Hughes | 733c1e4 | 2018-02-05 13:29:33 -0800 | [diff] [blame] | 177 | // Android <= 19 had getc/putc macros in <stdio.h> that referred |
| 178 | // to __srget/__swbuf, so those symbols need to be public for LP32 |
| 179 | // but can be hidden for LP64. Moreover, the NDK continued to ship |
| 180 | // those macros until r15 made unified headers the default. |
Christopher Ferris | a818445 | 2015-10-23 12:32:52 -0700 | [diff] [blame] | 181 | __LIBC32_LEGACY_PUBLIC__ int __srget(FILE*); |
| 182 | __LIBC32_LEGACY_PUBLIC__ int __swbuf(int, FILE*); |
| 183 | __LIBC32_LEGACY_PUBLIC__ int __srefill(FILE*); |
Elliott Hughes | 5f35710 | 2014-09-11 16:41:11 -0700 | [diff] [blame] | 184 | |
| 185 | /* This was referenced by the apportable middleware for LP32. */ |
Christopher Ferris | a818445 | 2015-10-23 12:32:52 -0700 | [diff] [blame] | 186 | __LIBC32_LEGACY_PUBLIC__ int __swsetup(FILE*); |
Elliott Hughes | 35d90bb | 2014-05-25 10:38:25 -0700 | [diff] [blame] | 187 | |
Elliott Hughes | abefc93 | 2014-09-24 17:20:53 -0700 | [diff] [blame] | 188 | /* These were referenced by a couple of different pieces of middleware and the Crystax NDK. */ |
Christopher Ferris | a818445 | 2015-10-23 12:32:52 -0700 | [diff] [blame] | 189 | __LIBC32_LEGACY_PUBLIC__ int __sflags(const char*, int*); |
| 190 | __LIBC32_LEGACY_PUBLIC__ FILE* __sfp(void); |
Christopher Ferris | a818445 | 2015-10-23 12:32:52 -0700 | [diff] [blame] | 191 | __LIBC32_LEGACY_PUBLIC__ void __smakebuf(FILE*); |
Elliott Hughes | abefc93 | 2014-09-24 17:20:53 -0700 | [diff] [blame] | 192 | |
Christopher Ferris | 78ba823 | 2014-10-09 18:31:01 -0700 | [diff] [blame] | 193 | /* These are referenced by the Greed for Glory franchise. */ |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 194 | __LIBC32_LEGACY_PUBLIC__ int __sflush(FILE*); |
| 195 | __LIBC32_LEGACY_PUBLIC__ int __sread(void*, char*, int); |
| 196 | __LIBC32_LEGACY_PUBLIC__ int __swrite(void*, const char*, int); |
| 197 | __LIBC32_LEGACY_PUBLIC__ fpos_t __sseek(void*, fpos_t, int); |
| 198 | __LIBC32_LEGACY_PUBLIC__ int __sclose(void*); |
| 199 | __LIBC32_LEGACY_PUBLIC__ int _fwalk(int (*)(FILE*)); |
Christopher Ferris | 78ba823 | 2014-10-09 18:31:01 -0700 | [diff] [blame] | 200 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 201 | off64_t __sseek64(void*, off64_t, int); |
Ryan Prichard | c485cdb | 2019-04-30 14:47:34 -0700 | [diff] [blame] | 202 | int __sflush_locked(FILE*); |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 203 | int __swhatbuf(FILE*, size_t*, int*); |
| 204 | wint_t __fgetwc_unlock(FILE*); |
| 205 | wint_t __ungetwc(wint_t, FILE*); |
| 206 | int __vfprintf(FILE*, const char*, va_list); |
| 207 | int __svfscanf(FILE*, const char*, va_list); |
| 208 | int __vfwprintf(FILE*, const wchar_t*, va_list); |
| 209 | int __vfwscanf(FILE*, const wchar_t*, va_list); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 210 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 211 | /* |
| 212 | * Return true if the given FILE cannot be written now. |
| 213 | */ |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 214 | #define cantwrite(fp) ((((fp)->_flags & __SWR) == 0 || (fp)->_bf._base == NULL) && __swsetup(fp)) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * Test whether the given stdio file has an active ungetc buffer; |
| 218 | * release such a buffer, without restoring ordinary unread data. |
| 219 | */ |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 220 | #define HASUB(fp) (_UB(fp)._base != NULL) |
| 221 | #define FREEUB(fp) \ |
| 222 | { \ |
| 223 | if (_UB(fp)._base != (fp)->_ubuf) free(_UB(fp)._base); \ |
| 224 | _UB(fp)._base = NULL; \ |
| 225 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 226 | |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 227 | #define FLOCKFILE(fp) \ |
| 228 | if (!_EXT(fp)->_caller_handles_locking) flockfile(fp) |
| 229 | #define FUNLOCKFILE(fp) \ |
| 230 | if (!_EXT(fp)->_caller_handles_locking) funlockfile(fp) |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 231 | |
Elliott Hughes | f2cea02 | 2014-03-13 14:54:53 -0700 | [diff] [blame] | 232 | /* OpenBSD exposes these in <stdio.h>, but we only want them exposed to the implementation. */ |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 233 | #define __sferror(p) (((p)->_flags & __SERR) != 0) |
| 234 | #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR | __SEOF))) |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 235 | #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : __BIONIC_CAST(static_cast, int, *(p)->_p++)) |
Elliott Hughes | 2899de9 | 2014-05-22 20:06:23 -0700 | [diff] [blame] | 236 | |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 237 | /* OpenBSD declares these in fvwrite.h, but we share them with C++ parts of the implementation. */ |
| 238 | struct __siov { |
| 239 | void* iov_base; |
| 240 | size_t iov_len; |
| 241 | }; |
| 242 | struct __suio { |
| 243 | struct __siov* uio_iov; |
| 244 | int uio_iovcnt; |
| 245 | size_t uio_resid; |
| 246 | }; |
| 247 | int __sfvwrite(FILE*, struct __suio*); |
| 248 | wint_t __fputwc_unlock(wchar_t wc, FILE* fp); |
Elliott Hughes | 2899de9 | 2014-05-22 20:06:23 -0700 | [diff] [blame] | 249 | |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 250 | /* Remove the if (!__sdidinit) __sinit() idiom from untouched upstream stdio code. */ |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 251 | extern void __sinit(void); // Not actually implemented. |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 252 | #define __sdidinit 1 |
| 253 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 254 | size_t parsefloat(FILE*, char*, char*); |
| 255 | size_t wparsefloat(FILE*, wchar_t*, wchar_t*); |
| 256 | |
Elliott Hughes | 3f31610 | 2020-07-23 13:39:54 -0700 | [diff] [blame] | 257 | // Check a FILE* isn't nullptr, so we can emit a clear diagnostic message |
| 258 | // instead of just crashing with SIGSEGV. |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 259 | #define CHECK_FP(fp) \ |
| 260 | if (fp == nullptr) __fortify_fatal("%s: null FILE*", __FUNCTION__) |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 261 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 262 | /* |
| 263 | * Floating point scanf/printf (input/output) definitions. |
| 264 | */ |
| 265 | |
| 266 | /* 11-bit exponent (VAX G floating point) is 308 decimal digits */ |
| 267 | #define MAXEXP 308 |
| 268 | /* 128 bit fraction takes up 39 decimal digits; max reasonable precision */ |
| 269 | #define MAXFRACT 39 |
| 270 | |
| 271 | /* |
| 272 | * MAXEXPDIG is the maximum number of decimal digits needed to store a |
| 273 | * floating point exponent in the largest supported format. It should |
| 274 | * be ceil(log10(LDBL_MAX_10_EXP)) or, if hexadecimal floating point |
| 275 | * conversions are supported, ceil(log10(LDBL_MAX_EXP)). But since it |
| 276 | * is presently never greater than 5 in practice, we fudge it. |
| 277 | */ |
| 278 | #define MAXEXPDIG 6 |
| 279 | #if LDBL_MAX_EXP > 999999 |
| 280 | #error "floating point buffers too small" |
| 281 | #endif |
| 282 | |
| 283 | char* __hdtoa(double, const char*, int, int*, int*, char**); |
| 284 | char* __hldtoa(long double, const char*, int, int*, int*, char**); |
| 285 | char* __ldtoa(long double*, int, int, int*, int*, char**); |
| 286 | |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 287 | #define WCIO_GET(fp) (_EXT(fp) ? &(_EXT(fp)->_wcio) : NULL) |
Elliott Hughes | 5a98234 | 2018-02-28 12:55:46 -0800 | [diff] [blame] | 288 | |
Elliott Hughes | 531199c | 2023-05-09 16:11:49 -0700 | [diff] [blame] | 289 | #define ORIENT_BYTES (-1) |
| 290 | #define ORIENT_UNKNOWN 0 |
| 291 | #define ORIENT_CHARS 1 |
| 292 | |
| 293 | #define _SET_ORIENTATION(fp, mode) \ |
| 294 | do { \ |
| 295 | struct wchar_io_data* _wcio = WCIO_GET(fp); \ |
| 296 | if (_wcio && _wcio->wcio_mode == ORIENT_UNKNOWN) _wcio->wcio_mode = (mode); \ |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 297 | } while (0) |
Elliott Hughes | 5a98234 | 2018-02-28 12:55:46 -0800 | [diff] [blame] | 298 | |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 299 | #define WCIO_FREE(fp) \ |
| 300 | do { \ |
| 301 | struct wchar_io_data* _wcio = WCIO_GET(fp); \ |
| 302 | if (_wcio) { \ |
Elliott Hughes | 531199c | 2023-05-09 16:11:49 -0700 | [diff] [blame] | 303 | _wcio->wcio_mode = ORIENT_UNKNOWN; \ |
Elliott Hughes | 9412cb3 | 2018-03-01 16:07:54 -0800 | [diff] [blame] | 304 | _wcio->wcio_ungetwc_inbuf = 0; \ |
| 305 | } \ |
| 306 | } while (0) |
Elliott Hughes | 5a98234 | 2018-02-28 12:55:46 -0800 | [diff] [blame] | 307 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 308 | __END_DECLS |