| 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 | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 35 | #ifndef __BIONIC_STDIO_LOCAL_H__ | 
|  | 36 | #define __BIONIC_STDIO_LOCAL_H__ | 
|  | 37 |  | 
|  | 38 | #include <pthread.h> | 
|  | 39 | #include <stdbool.h> | 
|  | 40 | #include <wchar.h> | 
|  | 41 | #include "wcio.h" | 
|  | 42 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 43 | /* | 
|  | 44 | * Information local to this implementation of stdio, | 
|  | 45 | * in particular, macros and private variables. | 
|  | 46 | */ | 
|  | 47 |  | 
| Elliott Hughes | c48c3e4 | 2014-11-19 15:16:51 -0800 | [diff] [blame] | 48 | __BEGIN_DECLS | 
|  | 49 |  | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 50 | struct __sbuf { | 
|  | 51 | unsigned char* _base; | 
|  | 52 | #if defined(__LP64__) | 
|  | 53 | size_t _size; | 
|  | 54 | #else | 
|  | 55 | int _size; | 
|  | 56 | #endif | 
|  | 57 | }; | 
|  | 58 |  | 
|  | 59 | struct __sFILE { | 
|  | 60 | unsigned char *_p;	/* current position in (some) buffer */ | 
|  | 61 | int	_r;		/* read space left for getc() */ | 
|  | 62 | int	_w;		/* write space left for putc() */ | 
|  | 63 | #if defined(__LP64__) | 
|  | 64 | int	_flags;		/* flags, below; this FILE is free if 0 */ | 
|  | 65 | int	_file;		/* fileno, if Unix descriptor, else -1 */ | 
|  | 66 | #else | 
|  | 67 | short	_flags;		/* flags, below; this FILE is free if 0 */ | 
|  | 68 | short	_file;		/* fileno, if Unix descriptor, else -1 */ | 
|  | 69 | #endif | 
|  | 70 | struct	__sbuf _bf;	/* the buffer (at least 1 byte, if !NULL) */ | 
|  | 71 | int	_lbfsize;	/* 0 or -_bf._size, for inline putc */ | 
|  | 72 |  | 
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 73 | // Function pointers used by `funopen`. | 
|  | 74 | // Note that `_seek` is ignored if `_seek64` (in __sfileext) is set. | 
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 75 | // TODO: NetBSD has `funopen2` which corrects the `int`s to `size_t`s. | 
|  | 76 | // TODO: glibc has `fopencookie` which passes the function pointers in a struct. | 
|  | 77 | void* _cookie;	/* cookie passed to io functions */ | 
|  | 78 | int (*_close)(void*); | 
|  | 79 | int (*_read)(void*, char*, int); | 
|  | 80 | fpos_t (*_seek)(void*, fpos_t, int); | 
|  | 81 | int (*_write)(void*, const char*, int); | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 82 |  | 
|  | 83 | /* extension data, to avoid further ABI breakage */ | 
|  | 84 | struct	__sbuf _ext; | 
|  | 85 | /* data for long sequences of ungetc() */ | 
|  | 86 | unsigned char *_up;	/* saved _p when _p is doing ungetc data */ | 
|  | 87 | int	_ur;		/* saved _r when _r is counting ungetc data */ | 
|  | 88 |  | 
|  | 89 | /* tricks to meet minimum requirements even when malloc() fails */ | 
|  | 90 | unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */ | 
|  | 91 | unsigned char _nbuf[1];	/* guarantee a getc() buffer */ | 
|  | 92 |  | 
|  | 93 | /* separate buffer for fgetln() when line crosses buffer boundary */ | 
|  | 94 | struct	__sbuf _lb;	/* buffer for fgetln() */ | 
|  | 95 |  | 
|  | 96 | /* Unix stdio files get aligned to block boundaries on fseek() */ | 
|  | 97 | int	_blksize;	/* stat.st_blksize (may be != _bf._size) */ | 
| Elliott Hughes | b877601 | 2016-01-22 19:18:48 -0800 | [diff] [blame] | 98 |  | 
|  | 99 | fpos_t _unused_0; // This was the `_offset` field (see below). | 
|  | 100 |  | 
|  | 101 | // Do not add new fields here. (Or remove or change the size of any above.) | 
|  | 102 | // Although bionic currently exports `stdin`, `stdout`, and `stderr` symbols, | 
|  | 103 | // that still hasn't made it to the NDK. All NDK-built apps index directly | 
|  | 104 | // into an array of this struct (which was in <stdio.h> historically), so if | 
|  | 105 | // you need to make any changes, they need to be in the `__sfileext` struct | 
|  | 106 | // below, and accessed via `_EXT`. | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 107 | }; | 
|  | 108 |  | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 109 | struct __sfileext { | 
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 110 | // ungetc buffer. | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 111 | struct __sbuf _ub; | 
|  | 112 |  | 
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 113 | // Wide char io status. | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 114 | struct wchar_io_data _wcio; | 
|  | 115 |  | 
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 116 | // File lock. | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 117 | pthread_mutex_t _lock; | 
|  | 118 |  | 
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 119 | // __fsetlocking support. | 
| Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 120 | bool _caller_handles_locking; | 
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 121 |  | 
|  | 122 | // Equivalent to `_seek` but for _FILE_OFFSET_BITS=64. | 
|  | 123 | // Callers should use this but fall back to `__sFILE::_seek`. | 
|  | 124 | off64_t (*_seek64)(void*, off64_t, int); | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 125 | }; | 
|  | 126 |  | 
| Elliott Hughes | e70e0e9 | 2016-01-25 11:10:47 -0800 | [diff] [blame] | 127 | // Values for `__sFILE::_flags`. | 
|  | 128 | #define __SLBF 0x0001  // Line buffered. | 
|  | 129 | #define __SNBF 0x0002  // Unbuffered. | 
|  | 130 | // RD and WR are never simultaneously asserted: use _SRW instead. | 
|  | 131 | #define __SRD  0x0004  // OK to read. | 
|  | 132 | #define __SWR  0x0008  // OK to write. | 
|  | 133 | #define __SRW  0x0010  // Open for reading & writing. | 
|  | 134 | #define __SEOF 0x0020  // Found EOF. | 
|  | 135 | #define __SERR 0x0040  // Found error. | 
|  | 136 | #define __SMBF 0x0080  // `_buf` is from malloc. | 
| Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 137 | // #define __SAPP 0x0100 --- historical (fdopen()ed in append mode). | 
| Elliott Hughes | e70e0e9 | 2016-01-25 11:10:47 -0800 | [diff] [blame] | 138 | #define __SSTR 0x0200  // This is an sprintf/snprintf string. | 
|  | 139 | // #define __SOPT 0x0400 --- historical (do fseek() optimization). | 
|  | 140 | // #define __SNPT 0x0800 --- historical (do not do fseek() optimization). | 
|  | 141 | // #define __SOFF 0x1000 --- historical (set iff _offset is in fact correct). | 
|  | 142 | #define __SMOD 0x2000  // true => fgetln modified _p text. | 
|  | 143 | #define __SALC 0x4000  // Allocate string space dynamically. | 
|  | 144 | #define __SIGN 0x8000  // Ignore this file in _fwalk. | 
|  | 145 |  | 
| Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame] | 146 | // TODO: remove remaining references to these obsolete flags (see above). | 
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 147 | #define __SNPT 0 | 
|  | 148 | #define __SOPT 0 | 
|  | 149 |  | 
| Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 150 | #define _EXT(fp) __BIONIC_CAST(reinterpret_cast, struct __sfileext*, (fp)->_ext._base) | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 151 |  | 
|  | 152 | #define _UB(fp) _EXT(fp)->_ub | 
|  | 153 | #define _FLOCK(fp)  _EXT(fp)->_lock | 
|  | 154 |  | 
|  | 155 | #define _FILEEXT_INIT(fp) \ | 
|  | 156 | do { \ | 
|  | 157 | _UB(fp)._base = NULL; \ | 
|  | 158 | _UB(fp)._size = 0; \ | 
|  | 159 | WCIO_INIT(fp); \ | 
|  | 160 | pthread_mutexattr_t attr; \ | 
|  | 161 | pthread_mutexattr_init(&attr); \ | 
|  | 162 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \ | 
|  | 163 | pthread_mutex_init(&_FLOCK(fp), &attr); \ | 
|  | 164 | pthread_mutexattr_destroy(&attr); \ | 
| Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 165 | _EXT(fp)->_caller_handles_locking = false; \ | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 166 | } while (0) | 
|  | 167 |  | 
|  | 168 | #define _FILEEXT_SETUP(f, fext) \ | 
|  | 169 | do { \ | 
| Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 170 | (f)->_ext._base = __BIONIC_CAST(reinterpret_cast, unsigned char*, fext); \ | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 171 | _FILEEXT_INIT(f); \ | 
|  | 172 | } while (0) | 
|  | 173 |  | 
| Elliott Hughes | aa50585 | 2014-05-27 11:22:39 -0700 | [diff] [blame] | 174 | /* | 
|  | 175 | * Android <= KitKat had getc/putc macros in <stdio.h> that referred | 
| Elliott Hughes | 35d90bb | 2014-05-25 10:38:25 -0700 | [diff] [blame] | 176 | * to __srget/__swbuf, so those symbols need to be public for LP32 | 
|  | 177 | * but can be hidden for LP64. | 
|  | 178 | */ | 
| Christopher Ferris | a818445 | 2015-10-23 12:32:52 -0700 | [diff] [blame] | 179 | __LIBC32_LEGACY_PUBLIC__ int __srget(FILE*); | 
|  | 180 | __LIBC32_LEGACY_PUBLIC__ int __swbuf(int, FILE*); | 
|  | 181 | __LIBC32_LEGACY_PUBLIC__ int __srefill(FILE*); | 
| Elliott Hughes | 5f35710 | 2014-09-11 16:41:11 -0700 | [diff] [blame] | 182 |  | 
|  | 183 | /* This was referenced by the apportable middleware for LP32. */ | 
| Christopher Ferris | a818445 | 2015-10-23 12:32:52 -0700 | [diff] [blame] | 184 | __LIBC32_LEGACY_PUBLIC__ int __swsetup(FILE*); | 
| Elliott Hughes | 35d90bb | 2014-05-25 10:38:25 -0700 | [diff] [blame] | 185 |  | 
| Elliott Hughes | abefc93 | 2014-09-24 17:20:53 -0700 | [diff] [blame] | 186 | /* 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] | 187 | __LIBC32_LEGACY_PUBLIC__ int __sflags(const char*, int*); | 
|  | 188 | __LIBC32_LEGACY_PUBLIC__ FILE* __sfp(void); | 
| Christopher Ferris | a818445 | 2015-10-23 12:32:52 -0700 | [diff] [blame] | 189 | __LIBC32_LEGACY_PUBLIC__ void __smakebuf(FILE*); | 
| Elliott Hughes | abefc93 | 2014-09-24 17:20:53 -0700 | [diff] [blame] | 190 |  | 
| Christopher Ferris | 78ba823 | 2014-10-09 18:31:01 -0700 | [diff] [blame] | 191 | /* These are referenced by the Greed for Glory franchise. */ | 
| Christopher Ferris | a818445 | 2015-10-23 12:32:52 -0700 | [diff] [blame] | 192 | __LIBC32_LEGACY_PUBLIC__ int __sflush(FILE *); | 
|  | 193 | __LIBC32_LEGACY_PUBLIC__ int __sread(void *, char *, int); | 
|  | 194 | __LIBC32_LEGACY_PUBLIC__ int __swrite(void *, const char *, int); | 
|  | 195 | __LIBC32_LEGACY_PUBLIC__ fpos_t __sseek(void *, fpos_t, int); | 
|  | 196 | __LIBC32_LEGACY_PUBLIC__ int __sclose(void *); | 
|  | 197 | __LIBC32_LEGACY_PUBLIC__ int _fwalk(int (*)(FILE *)); | 
| Christopher Ferris | 78ba823 | 2014-10-09 18:31:01 -0700 | [diff] [blame] | 198 |  | 
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 199 | off64_t __sseek64(void*, off64_t, int); | 
| Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 200 | int	__sflush_locked(FILE *); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 201 | int	__swhatbuf(FILE *, size_t *, int *); | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 202 | wint_t __fgetwc_unlock(FILE *); | 
|  | 203 | wint_t	__ungetwc(wint_t, FILE *); | 
| Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 204 | int	__vfprintf(FILE *, const char *, __va_list); | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 205 | int	__svfscanf(FILE * __restrict, const char * __restrict, __va_list); | 
|  | 206 | int	__vfwprintf(FILE * __restrict, const wchar_t * __restrict, __va_list); | 
|  | 207 | int	__vfwscanf(FILE * __restrict, const wchar_t * __restrict, __va_list); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 208 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 209 | /* | 
|  | 210 | * Return true if the given FILE cannot be written now. | 
|  | 211 | */ | 
|  | 212 | #define	cantwrite(fp) \ | 
|  | 213 | ((((fp)->_flags & __SWR) == 0 || (fp)->_bf._base == NULL) && \ | 
|  | 214 | __swsetup(fp)) | 
|  | 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 | */ | 
|  | 220 | #define	HASUB(fp) (_UB(fp)._base != NULL) | 
|  | 221 | #define	FREEUB(fp) { \ | 
|  | 222 | if (_UB(fp)._base != (fp)->_ubuf) \ | 
|  | 223 | free(_UB(fp)._base); \ | 
|  | 224 | _UB(fp)._base = NULL; \ | 
|  | 225 | } | 
|  | 226 |  | 
| Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 227 | #define FLOCKFILE(fp)   if (!_EXT(fp)->_caller_handles_locking) flockfile(fp) | 
|  | 228 | #define FUNLOCKFILE(fp) if (!_EXT(fp)->_caller_handles_locking) funlockfile(fp) | 
| Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 229 |  | 
|  | 230 | #define FLOATING_POINT | 
| Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 231 | #define PRINTF_WIDE_CHAR | 
|  | 232 | #define SCANF_WIDE_CHAR | 
| Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 233 | #define NO_PRINTF_PERCENT_N | 
| Elliott Hughes | f2cea02 | 2014-03-13 14:54:53 -0700 | [diff] [blame] | 234 |  | 
|  | 235 | /* OpenBSD exposes these in <stdio.h>, but we only want them exposed to the implementation. */ | 
| Elliott Hughes | f2cea02 | 2014-03-13 14:54:53 -0700 | [diff] [blame] | 236 | #define __sferror(p)   (((p)->_flags & __SERR) != 0) | 
|  | 237 | #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF))) | 
| Elliott Hughes | f2cea02 | 2014-03-13 14:54:53 -0700 | [diff] [blame] | 238 | #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++)) | 
| Elliott Hughes | 2899de9 | 2014-05-22 20:06:23 -0700 | [diff] [blame] | 239 |  | 
|  | 240 | /* OpenBSD declares these in fvwrite.h but we want to ensure they're hidden. */ | 
|  | 241 | struct __suio; | 
|  | 242 | extern int __sfvwrite(FILE *, struct __suio *); | 
|  | 243 | wint_t __fputwc_unlock(wchar_t wc, FILE *fp); | 
|  | 244 |  | 
| Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 245 | /* Remove the if (!__sdidinit) __sinit() idiom from untouched upstream stdio code. */ | 
|  | 246 | extern void __sinit(void); // Not actually implemented. | 
|  | 247 | #define __sdidinit 1 | 
|  | 248 |  | 
| Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 249 | size_t parsefloat(FILE*, char*, char*); | 
|  | 250 | size_t wparsefloat(FILE*, wchar_t*, wchar_t*); | 
|  | 251 |  | 
| Elliott Hughes | c48c3e4 | 2014-11-19 15:16:51 -0800 | [diff] [blame] | 252 | __END_DECLS | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 253 |  | 
|  | 254 | #endif |