blob: 517935b4810534f7775a922d6d12ac9343bb2e70 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $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 Hughesf0141df2015-10-12 12:44:23 -070035#ifndef __BIONIC_STDIO_LOCAL_H__
36#define __BIONIC_STDIO_LOCAL_H__
37
38#include <pthread.h>
39#include <stdbool.h>
40#include <wchar.h>
Josh Gaod1620602017-10-05 13:48:08 -070041
Elliott Hughescc3d04f2017-10-26 15:38:06 -070042#if defined(__cplusplus) // Until we fork all of stdio...
43#include "private/bionic_fortify.h"
44#endif
Josh Gaod1620602017-10-05 13:48:08 -070045
Elliott Hughesf0141df2015-10-12 12:44:23 -070046#include "wcio.h"
47
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048/*
49 * Information local to this implementation of stdio,
50 * in particular, macros and private variables.
51 */
52
Elliott Hughesc48c3e42014-11-19 15:16:51 -080053__BEGIN_DECLS
54
Elliott Hughesf0141df2015-10-12 12:44:23 -070055struct __sbuf {
56 unsigned char* _base;
57#if defined(__LP64__)
58 size_t _size;
59#else
60 int _size;
61#endif
62};
63
64struct __sFILE {
65 unsigned char *_p; /* current position in (some) buffer */
66 int _r; /* read space left for getc() */
67 int _w; /* write space left for putc() */
68#if defined(__LP64__)
69 int _flags; /* flags, below; this FILE is free if 0 */
70 int _file; /* fileno, if Unix descriptor, else -1 */
71#else
72 short _flags; /* flags, below; this FILE is free if 0 */
73 short _file; /* fileno, if Unix descriptor, else -1 */
74#endif
75 struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */
76 int _lbfsize; /* 0 or -_bf._size, for inline putc */
77
Elliott Hughes023c3072016-01-22 15:04:51 -080078 // Function pointers used by `funopen`.
79 // Note that `_seek` is ignored if `_seek64` (in __sfileext) is set.
Elliott Hughes023c3072016-01-22 15:04:51 -080080 // TODO: NetBSD has `funopen2` which corrects the `int`s to `size_t`s.
81 // TODO: glibc has `fopencookie` which passes the function pointers in a struct.
82 void* _cookie; /* cookie passed to io functions */
83 int (*_close)(void*);
84 int (*_read)(void*, char*, int);
85 fpos_t (*_seek)(void*, fpos_t, int);
86 int (*_write)(void*, const char*, int);
Elliott Hughesf0141df2015-10-12 12:44:23 -070087
88 /* extension data, to avoid further ABI breakage */
89 struct __sbuf _ext;
90 /* data for long sequences of ungetc() */
91 unsigned char *_up; /* saved _p when _p is doing ungetc data */
92 int _ur; /* saved _r when _r is counting ungetc data */
93
94 /* tricks to meet minimum requirements even when malloc() fails */
95 unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
96 unsigned char _nbuf[1]; /* guarantee a getc() buffer */
97
98 /* separate buffer for fgetln() when line crosses buffer boundary */
99 struct __sbuf _lb; /* buffer for fgetln() */
100
101 /* Unix stdio files get aligned to block boundaries on fseek() */
102 int _blksize; /* stat.st_blksize (may be != _bf._size) */
Elliott Hughesb8776012016-01-22 19:18:48 -0800103
104 fpos_t _unused_0; // This was the `_offset` field (see below).
105
106 // Do not add new fields here. (Or remove or change the size of any above.)
107 // Although bionic currently exports `stdin`, `stdout`, and `stderr` symbols,
108 // that still hasn't made it to the NDK. All NDK-built apps index directly
109 // into an array of this struct (which was in <stdio.h> historically), so if
110 // you need to make any changes, they need to be in the `__sfileext` struct
111 // below, and accessed via `_EXT`.
Elliott Hughesf0141df2015-10-12 12:44:23 -0700112};
113
Elliott Hughesf0141df2015-10-12 12:44:23 -0700114struct __sfileext {
Elliott Hughes023c3072016-01-22 15:04:51 -0800115 // ungetc buffer.
Elliott Hughesf0141df2015-10-12 12:44:23 -0700116 struct __sbuf _ub;
117
Elliott Hughes023c3072016-01-22 15:04:51 -0800118 // Wide char io status.
Elliott Hughesf0141df2015-10-12 12:44:23 -0700119 struct wchar_io_data _wcio;
120
Elliott Hughes023c3072016-01-22 15:04:51 -0800121 // File lock.
Elliott Hughesf0141df2015-10-12 12:44:23 -0700122 pthread_mutex_t _lock;
123
Elliott Hughes023c3072016-01-22 15:04:51 -0800124 // __fsetlocking support.
Yabin Cui76144aa2015-11-19 13:52:16 -0800125 bool _caller_handles_locking;
Elliott Hughes023c3072016-01-22 15:04:51 -0800126
127 // Equivalent to `_seek` but for _FILE_OFFSET_BITS=64.
128 // Callers should use this but fall back to `__sFILE::_seek`.
129 off64_t (*_seek64)(void*, off64_t, int);
Elliott Hughesf0141df2015-10-12 12:44:23 -0700130};
131
Elliott Hughese70e0e92016-01-25 11:10:47 -0800132// Values for `__sFILE::_flags`.
133#define __SLBF 0x0001 // Line buffered.
134#define __SNBF 0x0002 // Unbuffered.
Elliott Hughes5ba2c212017-08-01 15:16:36 -0700135// __SRD and __SWR are mutually exclusive because they indicate what we did last.
136// If you want to know whether we were opened read-write, check __SRW instead.
137#define __SRD 0x0004 // Last operation was read.
138#define __SWR 0x0008 // Last operation was write.
139#define __SRW 0x0010 // Was opened for reading & writing.
Elliott Hughese70e0e92016-01-25 11:10:47 -0800140#define __SEOF 0x0020 // Found EOF.
141#define __SERR 0x0040 // Found error.
142#define __SMBF 0x0080 // `_buf` is from malloc.
Elliott Hughes33a8cb12017-07-25 18:06:46 -0700143// #define __SAPP 0x0100 --- historical (fdopen()ed in append mode).
Elliott Hughese70e0e92016-01-25 11:10:47 -0800144#define __SSTR 0x0200 // This is an sprintf/snprintf string.
145// #define __SOPT 0x0400 --- historical (do fseek() optimization).
146// #define __SNPT 0x0800 --- historical (do not do fseek() optimization).
147// #define __SOFF 0x1000 --- historical (set iff _offset is in fact correct).
Elliott Hughes37ad9592017-10-30 17:47:12 -0700148// #define __SMOD 0x2000 --- historical (set iff fgetln modified _p text).
Elliott Hughese70e0e92016-01-25 11:10:47 -0800149#define __SALC 0x4000 // Allocate string space dynamically.
150#define __SIGN 0x8000 // Ignore this file in _fwalk.
151
Elliott Hughes80e4c152017-07-21 13:57:55 -0700152// TODO: remove remaining references to these obsolete flags (see above).
Elliott Hughes37ad9592017-10-30 17:47:12 -0700153#define __SMOD 0
Elliott Hughes2704bd12016-01-20 17:14:53 -0800154#define __SNPT 0
155#define __SOPT 0
156
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700157#define _EXT(fp) __BIONIC_CAST(reinterpret_cast, struct __sfileext*, (fp)->_ext._base)
Elliott Hughesf0141df2015-10-12 12:44:23 -0700158
159#define _UB(fp) _EXT(fp)->_ub
160#define _FLOCK(fp) _EXT(fp)->_lock
161
Elliott Hughes1a56a262017-12-20 08:53:49 -0800162#define _FILEEXT_SETUP(fp, fext) \
163 do { \
164 (fp)->_ext._base = __BIONIC_CAST(reinterpret_cast, unsigned char*, fext); \
165 memset(_EXT(fp), 0, sizeof(struct __sfileext)); \
166 _EXT(fp)->_caller_handles_locking = true; \
167 } while (0)
Elliott Hughesf0141df2015-10-12 12:44:23 -0700168
Elliott Hughesaa505852014-05-27 11:22:39 -0700169/*
170 * Android <= KitKat had getc/putc macros in <stdio.h> that referred
Elliott Hughes35d90bb2014-05-25 10:38:25 -0700171 * to __srget/__swbuf, so those symbols need to be public for LP32
172 * but can be hidden for LP64.
173 */
Christopher Ferrisa8184452015-10-23 12:32:52 -0700174__LIBC32_LEGACY_PUBLIC__ int __srget(FILE*);
175__LIBC32_LEGACY_PUBLIC__ int __swbuf(int, FILE*);
176__LIBC32_LEGACY_PUBLIC__ int __srefill(FILE*);
Elliott Hughes5f357102014-09-11 16:41:11 -0700177
178/* This was referenced by the apportable middleware for LP32. */
Christopher Ferrisa8184452015-10-23 12:32:52 -0700179__LIBC32_LEGACY_PUBLIC__ int __swsetup(FILE*);
Elliott Hughes35d90bb2014-05-25 10:38:25 -0700180
Elliott Hughesabefc932014-09-24 17:20:53 -0700181/* These were referenced by a couple of different pieces of middleware and the Crystax NDK. */
Christopher Ferrisa8184452015-10-23 12:32:52 -0700182__LIBC32_LEGACY_PUBLIC__ int __sflags(const char*, int*);
183__LIBC32_LEGACY_PUBLIC__ FILE* __sfp(void);
Christopher Ferrisa8184452015-10-23 12:32:52 -0700184__LIBC32_LEGACY_PUBLIC__ void __smakebuf(FILE*);
Elliott Hughesabefc932014-09-24 17:20:53 -0700185
Christopher Ferris78ba8232014-10-09 18:31:01 -0700186/* These are referenced by the Greed for Glory franchise. */
Christopher Ferrisa8184452015-10-23 12:32:52 -0700187__LIBC32_LEGACY_PUBLIC__ int __sflush(FILE *);
188__LIBC32_LEGACY_PUBLIC__ int __sread(void *, char *, int);
189__LIBC32_LEGACY_PUBLIC__ int __swrite(void *, const char *, int);
190__LIBC32_LEGACY_PUBLIC__ fpos_t __sseek(void *, fpos_t, int);
191__LIBC32_LEGACY_PUBLIC__ int __sclose(void *);
192__LIBC32_LEGACY_PUBLIC__ int _fwalk(int (*)(FILE *));
Christopher Ferris78ba8232014-10-09 18:31:01 -0700193
Elliott Hughes023c3072016-01-22 15:04:51 -0800194off64_t __sseek64(void*, off64_t, int);
Kenny Rootf5823402011-02-12 07:13:44 -0800195int __sflush_locked(FILE *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196int __swhatbuf(FILE *, size_t *, int *);
Elliott Hughes01ae00f2014-04-29 16:28:56 -0700197wint_t __fgetwc_unlock(FILE *);
198wint_t __ungetwc(wint_t, FILE *);
Elliott Hughes4a8de0d2017-08-01 10:48:08 -0700199int __vfprintf(FILE *, const char *, va_list);
200int __svfscanf(FILE *, const char *, va_list);
201int __vfwprintf(FILE *, const wchar_t *, va_list);
202int __vfwscanf(FILE *, const wchar_t *, va_list);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204/*
205 * Return true if the given FILE cannot be written now.
206 */
207#define cantwrite(fp) \
208 ((((fp)->_flags & __SWR) == 0 || (fp)->_bf._base == NULL) && \
209 __swsetup(fp))
210
211/*
212 * Test whether the given stdio file has an active ungetc buffer;
213 * release such a buffer, without restoring ordinary unread data.
214 */
215#define HASUB(fp) (_UB(fp)._base != NULL)
216#define FREEUB(fp) { \
217 if (_UB(fp)._base != (fp)->_ubuf) \
218 free(_UB(fp)._base); \
219 _UB(fp)._base = NULL; \
220}
221
Yabin Cui76144aa2015-11-19 13:52:16 -0800222#define FLOCKFILE(fp) if (!_EXT(fp)->_caller_handles_locking) flockfile(fp)
223#define FUNLOCKFILE(fp) if (!_EXT(fp)->_caller_handles_locking) funlockfile(fp)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700224
Elliott Hughese2341d02014-05-02 18:16:32 -0700225#define NO_PRINTF_PERCENT_N
Elliott Hughesf2cea022014-03-13 14:54:53 -0700226
227/* OpenBSD exposes these in <stdio.h>, but we only want them exposed to the implementation. */
Elliott Hughesf2cea022014-03-13 14:54:53 -0700228#define __sferror(p) (((p)->_flags & __SERR) != 0)
229#define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
Elliott Hughesf2cea022014-03-13 14:54:53 -0700230#define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
Elliott Hughes2899de92014-05-22 20:06:23 -0700231
Elliott Hughes37ad9592017-10-30 17:47:12 -0700232/* OpenBSD declares these in fvwrite.h, but we share them with C++ parts of the implementation. */
233struct __siov {
234 void* iov_base;
235 size_t iov_len;
236};
237struct __suio {
238 struct __siov* uio_iov;
239 int uio_iovcnt;
240 size_t uio_resid;
241};
242int __sfvwrite(FILE*, struct __suio*);
243wint_t __fputwc_unlock(wchar_t wc, FILE* fp);
Elliott Hughes2899de92014-05-22 20:06:23 -0700244
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800245/* Remove the if (!__sdidinit) __sinit() idiom from untouched upstream stdio code. */
246extern void __sinit(void); // Not actually implemented.
247#define __sdidinit 1
248
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700249size_t parsefloat(FILE*, char*, char*);
250size_t wparsefloat(FILE*, wchar_t*, wchar_t*);
251
Josh Gaod1620602017-10-05 13:48:08 -0700252// Sanity check a FILE* for nullptr, so we can emit a message while crashing
253// instead of doing a blind null-dereference.
Elliott Hughescc3d04f2017-10-26 15:38:06 -0700254#define CHECK_FP(fp) if (fp == nullptr) __fortify_fatal("%s: null FILE*", __FUNCTION__)
Josh Gaod1620602017-10-05 13:48:08 -0700255
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700256/*
257 * Floating point scanf/printf (input/output) definitions.
258 */
259
260/* 11-bit exponent (VAX G floating point) is 308 decimal digits */
261#define MAXEXP 308
262/* 128 bit fraction takes up 39 decimal digits; max reasonable precision */
263#define MAXFRACT 39
264
265/*
266 * MAXEXPDIG is the maximum number of decimal digits needed to store a
267 * floating point exponent in the largest supported format. It should
268 * be ceil(log10(LDBL_MAX_10_EXP)) or, if hexadecimal floating point
269 * conversions are supported, ceil(log10(LDBL_MAX_EXP)). But since it
270 * is presently never greater than 5 in practice, we fudge it.
271 */
272#define MAXEXPDIG 6
273#if LDBL_MAX_EXP > 999999
274#error "floating point buffers too small"
275#endif
276
277char* __hdtoa(double, const char*, int, int*, int*, char**);
278char* __hldtoa(long double, const char*, int, int*, int*, char**);
279char* __ldtoa(long double*, int, int, int*, int*, char**);
280
281__END_DECLS
282
Elliott Hughesf0141df2015-10-12 12:44:23 -0700283#endif