| 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 |  | 
|  | 73 | /* operations */ | 
|  | 74 | void	*_cookie;	/* cookie passed to io functions */ | 
|  | 75 | int	(*_close)(void *); | 
|  | 76 | int	(*_read)(void *, char *, int); | 
|  | 77 | fpos_t	(*_seek)(void *, fpos_t, int); | 
|  | 78 | int	(*_write)(void *, const char *, int); | 
|  | 79 |  | 
|  | 80 | /* extension data, to avoid further ABI breakage */ | 
|  | 81 | struct	__sbuf _ext; | 
|  | 82 | /* data for long sequences of ungetc() */ | 
|  | 83 | unsigned char *_up;	/* saved _p when _p is doing ungetc data */ | 
|  | 84 | int	_ur;		/* saved _r when _r is counting ungetc data */ | 
|  | 85 |  | 
|  | 86 | /* tricks to meet minimum requirements even when malloc() fails */ | 
|  | 87 | unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */ | 
|  | 88 | unsigned char _nbuf[1];	/* guarantee a getc() buffer */ | 
|  | 89 |  | 
|  | 90 | /* separate buffer for fgetln() when line crosses buffer boundary */ | 
|  | 91 | struct	__sbuf _lb;	/* buffer for fgetln() */ | 
|  | 92 |  | 
|  | 93 | /* Unix stdio files get aligned to block boundaries on fseek() */ | 
|  | 94 | int	_blksize;	/* stat.st_blksize (may be != _bf._size) */ | 
|  | 95 | fpos_t	_offset;	/* current lseek offset */ | 
|  | 96 | }; | 
|  | 97 |  | 
|  | 98 | /* | 
|  | 99 | * file extension | 
|  | 100 | */ | 
|  | 101 | struct __sfileext { | 
|  | 102 | /* ungetc buffer */ | 
|  | 103 | struct __sbuf _ub; | 
|  | 104 |  | 
|  | 105 | /* wide char io status */ | 
|  | 106 | struct wchar_io_data _wcio; | 
|  | 107 |  | 
|  | 108 | /* file lock */ | 
|  | 109 | pthread_mutex_t _lock; | 
|  | 110 |  | 
|  | 111 | /* __fsetlocking support */ | 
|  | 112 | bool _stdio_handles_locking; | 
|  | 113 | }; | 
|  | 114 |  | 
|  | 115 | #if defined(__cplusplus) | 
|  | 116 | #define _EXT(fp) reinterpret_cast<__sfileext*>((fp)->_ext._base) | 
|  | 117 | #else | 
|  | 118 | #define _EXT(fp) ((struct __sfileext *)((fp)->_ext._base)) | 
|  | 119 | #endif | 
|  | 120 |  | 
|  | 121 | #define _UB(fp) _EXT(fp)->_ub | 
|  | 122 | #define _FLOCK(fp)  _EXT(fp)->_lock | 
|  | 123 |  | 
|  | 124 | #define _FILEEXT_INIT(fp) \ | 
|  | 125 | do { \ | 
|  | 126 | _UB(fp)._base = NULL; \ | 
|  | 127 | _UB(fp)._size = 0; \ | 
|  | 128 | WCIO_INIT(fp); \ | 
|  | 129 | pthread_mutexattr_t attr; \ | 
|  | 130 | pthread_mutexattr_init(&attr); \ | 
|  | 131 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \ | 
|  | 132 | pthread_mutex_init(&_FLOCK(fp), &attr); \ | 
|  | 133 | pthread_mutexattr_destroy(&attr); \ | 
|  | 134 | _EXT(fp)->_stdio_handles_locking = true; \ | 
|  | 135 | } while (0) | 
|  | 136 |  | 
|  | 137 | #define _FILEEXT_SETUP(f, fext) \ | 
|  | 138 | do { \ | 
|  | 139 | (f)->_ext._base = (unsigned char *)(fext); \ | 
|  | 140 | _FILEEXT_INIT(f); \ | 
|  | 141 | } while (0) | 
|  | 142 |  | 
| Elliott Hughes | aa50585 | 2014-05-27 11:22:39 -0700 | [diff] [blame] | 143 | /* | 
|  | 144 | * Android <= KitKat had getc/putc macros in <stdio.h> that referred | 
| Elliott Hughes | 35d90bb | 2014-05-25 10:38:25 -0700 | [diff] [blame] | 145 | * to __srget/__swbuf, so those symbols need to be public for LP32 | 
|  | 146 | * but can be hidden for LP64. | 
|  | 147 | */ | 
| Christopher Ferris | a818445 | 2015-10-23 12:32:52 -0700 | [diff] [blame] | 148 | __LIBC32_LEGACY_PUBLIC__ int __srget(FILE*); | 
|  | 149 | __LIBC32_LEGACY_PUBLIC__ int __swbuf(int, FILE*); | 
|  | 150 | __LIBC32_LEGACY_PUBLIC__ int __srefill(FILE*); | 
| Elliott Hughes | 5f35710 | 2014-09-11 16:41:11 -0700 | [diff] [blame] | 151 |  | 
|  | 152 | /* This was referenced by the apportable middleware for LP32. */ | 
| Christopher Ferris | a818445 | 2015-10-23 12:32:52 -0700 | [diff] [blame] | 153 | __LIBC32_LEGACY_PUBLIC__ int __swsetup(FILE*); | 
| Elliott Hughes | 35d90bb | 2014-05-25 10:38:25 -0700 | [diff] [blame] | 154 |  | 
| Elliott Hughes | abefc93 | 2014-09-24 17:20:53 -0700 | [diff] [blame] | 155 | /* 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] | 156 | __LIBC32_LEGACY_PUBLIC__ extern int __sdidinit; | 
|  | 157 | __LIBC32_LEGACY_PUBLIC__ int __sflags(const char*, int*); | 
|  | 158 | __LIBC32_LEGACY_PUBLIC__ FILE* __sfp(void); | 
|  | 159 | __LIBC32_LEGACY_PUBLIC__ void __sinit(void); | 
|  | 160 | __LIBC32_LEGACY_PUBLIC__ void __smakebuf(FILE*); | 
| Elliott Hughes | abefc93 | 2014-09-24 17:20:53 -0700 | [diff] [blame] | 161 |  | 
| Christopher Ferris | 78ba823 | 2014-10-09 18:31:01 -0700 | [diff] [blame] | 162 | /* These are referenced by the Greed for Glory franchise. */ | 
| Christopher Ferris | a818445 | 2015-10-23 12:32:52 -0700 | [diff] [blame] | 163 | __LIBC32_LEGACY_PUBLIC__ int __sflush(FILE *); | 
|  | 164 | __LIBC32_LEGACY_PUBLIC__ int __sread(void *, char *, int); | 
|  | 165 | __LIBC32_LEGACY_PUBLIC__ int __swrite(void *, const char *, int); | 
|  | 166 | __LIBC32_LEGACY_PUBLIC__ fpos_t __sseek(void *, fpos_t, int); | 
|  | 167 | __LIBC32_LEGACY_PUBLIC__ int __sclose(void *); | 
|  | 168 | __LIBC32_LEGACY_PUBLIC__ int _fwalk(int (*)(FILE *)); | 
| Christopher Ferris | 78ba823 | 2014-10-09 18:31:01 -0700 | [diff] [blame] | 169 |  | 
| Elliott Hughes | 2899de9 | 2014-05-22 20:06:23 -0700 | [diff] [blame] | 170 | #pragma GCC visibility push(hidden) | 
|  | 171 |  | 
| Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 172 | int	__sflush_locked(FILE *); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 173 | void	_cleanup(void); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 174 | int	__swhatbuf(FILE *, size_t *, int *); | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 175 | wint_t __fgetwc_unlock(FILE *); | 
|  | 176 | wint_t	__ungetwc(wint_t, FILE *); | 
| Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 177 | int	__vfprintf(FILE *, const char *, __va_list); | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 178 | int	__svfscanf(FILE * __restrict, const char * __restrict, __va_list); | 
|  | 179 | int	__vfwprintf(FILE * __restrict, const wchar_t * __restrict, __va_list); | 
|  | 180 | 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] | 181 |  | 
| Elliott Hughes | 4c2da03 | 2014-05-16 16:50:34 -0700 | [diff] [blame] | 182 | extern void __atexit_register_cleanup(void (*)(void)); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 183 |  | 
|  | 184 | /* | 
|  | 185 | * Return true if the given FILE cannot be written now. | 
|  | 186 | */ | 
|  | 187 | #define	cantwrite(fp) \ | 
|  | 188 | ((((fp)->_flags & __SWR) == 0 || (fp)->_bf._base == NULL) && \ | 
|  | 189 | __swsetup(fp)) | 
|  | 190 |  | 
|  | 191 | /* | 
|  | 192 | * Test whether the given stdio file has an active ungetc buffer; | 
|  | 193 | * release such a buffer, without restoring ordinary unread data. | 
|  | 194 | */ | 
|  | 195 | #define	HASUB(fp) (_UB(fp)._base != NULL) | 
|  | 196 | #define	FREEUB(fp) { \ | 
|  | 197 | if (_UB(fp)._base != (fp)->_ubuf) \ | 
|  | 198 | free(_UB(fp)._base); \ | 
|  | 199 | _UB(fp)._base = NULL; \ | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | /* | 
|  | 203 | * test for an fgetln() buffer. | 
|  | 204 | */ | 
|  | 205 | #define	HASLB(fp) ((fp)->_lb._base != NULL) | 
|  | 206 | #define	FREELB(fp) { \ | 
|  | 207 | free((char *)(fp)->_lb._base); \ | 
|  | 208 | (fp)->_lb._base = NULL; \ | 
|  | 209 | } | 
| Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 210 |  | 
| Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 211 | #define FLOCKFILE(fp)   if (_EXT(fp)->_stdio_handles_locking) flockfile(fp) | 
|  | 212 | #define FUNLOCKFILE(fp) if (_EXT(fp)->_stdio_handles_locking) funlockfile(fp) | 
| Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 213 |  | 
|  | 214 | #define FLOATING_POINT | 
| Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 215 | #define PRINTF_WIDE_CHAR | 
|  | 216 | #define SCANF_WIDE_CHAR | 
| Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 217 | #define NO_PRINTF_PERCENT_N | 
| Elliott Hughes | f2cea02 | 2014-03-13 14:54:53 -0700 | [diff] [blame] | 218 |  | 
|  | 219 | /* 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] | 220 | #define __sfeof(p)     (((p)->_flags & __SEOF) != 0) | 
|  | 221 | #define __sferror(p)   (((p)->_flags & __SERR) != 0) | 
|  | 222 | #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF))) | 
|  | 223 | #define __sfileno(p)   ((p)->_file) | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 224 | #if !defined(__cplusplus) | 
| Elliott Hughes | f2cea02 | 2014-03-13 14:54:53 -0700 | [diff] [blame] | 225 | #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++)) | 
|  | 226 | static __inline int __sputc(int _c, FILE* _p) { | 
|  | 227 | if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n')) { | 
|  | 228 | return (*_p->_p++ = _c); | 
|  | 229 | } else { | 
|  | 230 | return (__swbuf(_c, _p)); | 
|  | 231 | } | 
|  | 232 | } | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 233 | #endif | 
| Elliott Hughes | 2899de9 | 2014-05-22 20:06:23 -0700 | [diff] [blame] | 234 |  | 
|  | 235 | /* OpenBSD declares these in fvwrite.h but we want to ensure they're hidden. */ | 
|  | 236 | struct __suio; | 
|  | 237 | extern int __sfvwrite(FILE *, struct __suio *); | 
|  | 238 | wint_t __fputwc_unlock(wchar_t wc, FILE *fp); | 
|  | 239 |  | 
|  | 240 | #pragma GCC visibility pop | 
| Elliott Hughes | c48c3e4 | 2014-11-19 15:16:51 -0800 | [diff] [blame] | 241 |  | 
|  | 242 | __END_DECLS | 
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 243 |  | 
|  | 244 | #endif |