The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* $OpenBSD: stdio.h,v 1.35 2006/01/13 18:10:09 miod Exp $ */ |
| 2 | /* $NetBSD: stdio.h,v 1.18 1996/04/25 18:29:21 jtc Exp $ */ |
| 3 | |
| 4 | /*- |
| 5 | * Copyright (c) 1990 The Regents of the University of California. |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * This code is derived from software contributed to Berkeley by |
| 9 | * Chris Torek. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * 1. Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer. |
| 16 | * 2. Redistributions in binary form must reproduce the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer in the |
| 18 | * documentation and/or other materials provided with the distribution. |
| 19 | * 3. Neither the name of the University nor the names of its contributors |
| 20 | * may be used to endorse or promote products derived from this software |
| 21 | * without specific prior written permission. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 | * SUCH DAMAGE. |
| 34 | * |
| 35 | * @(#)stdio.h 5.17 (Berkeley) 6/3/91 |
| 36 | */ |
| 37 | |
| 38 | #ifndef _STDIO_H_ |
| 39 | #define _STDIO_H_ |
| 40 | |
| 41 | #include <sys/cdefs.h> |
Elliott Hughes | 3975cec | 2012-11-29 17:25:23 -0800 | [diff] [blame] | 42 | #include <sys/types.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 43 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 44 | #include <stdarg.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 | #include <stddef.h> |
| 46 | |
Elliott Hughes | 7d56ccb | 2012-10-01 17:56:58 -0700 | [diff] [blame] | 47 | #define __need_NULL |
| 48 | #include <stddef.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 49 | |
Elliott Hughes | fd936ae | 2016-08-12 10:16:34 -0700 | [diff] [blame^] | 50 | #include <bits/seek_constants.h> |
| 51 | |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 52 | __BEGIN_DECLS |
| 53 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 54 | #if defined(__clang__) |
| 55 | #pragma clang diagnostic push |
| 56 | #pragma clang diagnostic ignored "-Wnullability-completeness" |
| 57 | #endif |
| 58 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 59 | typedef off_t fpos_t; |
| 60 | typedef off64_t fpos64_t; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 61 | |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 62 | struct __sFILE; |
| 63 | typedef struct __sFILE FILE; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | |
Dan Albert | 32c79c2 | 2016-07-15 11:32:23 -0700 | [diff] [blame] | 65 | #if __ANDROID_API__ >= 23 |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 66 | extern FILE* stdin __INTRODUCED_IN(23); |
| 67 | extern FILE* stdout __INTRODUCED_IN(23); |
| 68 | extern FILE* stderr __INTRODUCED_IN(23); |
Dan Albert | 32c79c2 | 2016-07-15 11:32:23 -0700 | [diff] [blame] | 69 | |
Elliott Hughes | 168667c | 2014-11-14 14:42:59 -0800 | [diff] [blame] | 70 | /* C99 and earlier plus current C++ standards say these must be macros. */ |
| 71 | #define stdin stdin |
| 72 | #define stdout stdout |
| 73 | #define stderr stderr |
Dan Albert | 32c79c2 | 2016-07-15 11:32:23 -0700 | [diff] [blame] | 74 | #else |
| 75 | /* Before M the actual symbols for stdin and friends had different names. */ |
| 76 | extern FILE* __sF[] __REMOVED_IN(23); |
| 77 | |
| 78 | #define stdin __sF[0] |
| 79 | #define stdout __sF[1] |
| 80 | #define stderr __sF[2] |
| 81 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 82 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 83 | /* |
| 84 | * The following three definitions are for ANSI C, which took them |
| 85 | * from System V, which brilliantly took internal interface macros and |
| 86 | * made them official arguments to setvbuf(), without renaming them. |
| 87 | * Hence, these ugly _IOxxx names are *supposed* to appear in user code. |
| 88 | * |
| 89 | * Although numbered as their counterparts above, the implementation |
| 90 | * does not rely on this. |
| 91 | */ |
| 92 | #define _IOFBF 0 /* setvbuf should set fully buffered */ |
| 93 | #define _IOLBF 1 /* setvbuf should set line buffered */ |
| 94 | #define _IONBF 2 /* setvbuf should set unbuffered */ |
| 95 | |
| 96 | #define BUFSIZ 1024 /* size of buffer used by setbuf */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 97 | #define EOF (-1) |
| 98 | |
| 99 | /* |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 100 | * FOPEN_MAX is a minimum maximum, and is the number of streams that |
| 101 | * stdio can provide without attempting to allocate further resources |
| 102 | * (which could fail). Do not use this for anything. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 103 | */ |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 104 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 105 | #define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */ |
| 106 | #define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */ |
| 107 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 108 | #define L_tmpnam 1024 /* XXX must be == PATH_MAX */ |
| 109 | #define TMP_MAX 308915776 |
| 110 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 111 | /* |
| 112 | * Functions defined in ANSI C standard. |
| 113 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 114 | void clearerr(FILE *); |
| 115 | int fclose(FILE *); |
| 116 | int feof(FILE *); |
| 117 | int ferror(FILE *); |
| 118 | int fflush(FILE *); |
| 119 | int fgetc(FILE *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 120 | char *fgets(char * __restrict, int, FILE * __restrict); |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 121 | int fprintf(FILE * __restrict , const char * __restrict _Nonnull, ...) __printflike(2, 3); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 122 | int fputc(int, FILE *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 123 | int fputs(const char * __restrict, FILE * __restrict); |
| 124 | size_t fread(void * __restrict, size_t, size_t, FILE * __restrict); |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 125 | int fscanf(FILE * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 126 | size_t fwrite(const void * __restrict, size_t, size_t, FILE * __restrict); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 127 | int getc(FILE *); |
| 128 | int getchar(void); |
Josh Gao | 46b4416 | 2016-05-27 11:14:16 -0700 | [diff] [blame] | 129 | ssize_t getdelim(char** __restrict, size_t* __restrict, int, FILE* __restrict) __INTRODUCED_IN(18); |
| 130 | ssize_t getline(char** __restrict, size_t* __restrict, FILE* __restrict) __INTRODUCED_IN(18); |
Elliott Hughes | c13fb75 | 2013-12-17 20:43:30 -0800 | [diff] [blame] | 131 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 132 | void perror(const char *); |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 133 | int printf(const char * __restrict _Nonnull, ...) __printflike(1, 2); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 134 | int putc(int, FILE *); |
| 135 | int putchar(int); |
| 136 | int puts(const char *); |
| 137 | int remove(const char *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 138 | void rewind(FILE *); |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 139 | int scanf(const char * __restrict _Nonnull, ...) __scanflike(1, 2); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 140 | void setbuf(FILE * __restrict, char * __restrict); |
| 141 | int setvbuf(FILE * __restrict, char * __restrict, int, size_t); |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 142 | int sscanf(const char * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 143 | int ungetc(int, FILE *); |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 144 | int vfprintf(FILE * __restrict, const char * __restrict _Nonnull, __va_list) __printflike(2, 0); |
| 145 | int vprintf(const char * __restrict _Nonnull, __va_list) __printflike(1, 0); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 146 | |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 147 | int dprintf(int, const char* __restrict _Nonnull, ...) __printflike(2, 3) __INTRODUCED_IN(21); |
| 148 | int vdprintf(int, const char* __restrict _Nonnull, __va_list) __printflike(2, 0) __INTRODUCED_IN(21); |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 149 | |
Elliott Hughes | f6495c7 | 2016-07-25 09:20:57 -0700 | [diff] [blame] | 150 | #if (defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L) || \ |
| 151 | (defined(__cplusplus) && __cplusplus <= 201103L) |
Yabin Cui | 9b4f77f | 2015-02-23 16:42:07 -0800 | [diff] [blame] | 152 | char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead"))); |
Dan Albert | 9635046 | 2014-06-17 23:31:21 +0000 | [diff] [blame] | 153 | #endif |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 154 | int sprintf(char* __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3); |
| 155 | int vsprintf(char* __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0); |
Yabin Cui | 9b4f77f | 2015-02-23 16:42:07 -0800 | [diff] [blame] | 156 | char* tmpnam(char*) __attribute__((deprecated("tmpnam is unsafe, use mkstemp or tmpfile instead"))); |
Elliott Hughes | 3ba55f8 | 2016-06-08 18:11:23 -0700 | [diff] [blame] | 157 | #define P_tmpdir "/tmp/" /* deprecated */ |
Elliott Hughes | c13fb75 | 2013-12-17 20:43:30 -0800 | [diff] [blame] | 158 | char* tempnam(const char*, const char*) |
Yabin Cui | 9b4f77f | 2015-02-23 16:42:07 -0800 | [diff] [blame] | 159 | __attribute__((deprecated("tempnam is unsafe, use mkstemp or tmpfile instead"))); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 160 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 161 | int rename(const char*, const char*); |
| 162 | int renameat(int, const char*, int, const char*); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 163 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 164 | int fseek(FILE*, long, int); |
| 165 | long ftell(FILE*); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 166 | |
Elliott Hughes | 68dc20d | 2015-02-06 22:28:49 -0800 | [diff] [blame] | 167 | #if defined(__USE_FILE_OFFSET64) |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 168 | int fgetpos(FILE*, fpos_t*) __RENAME(fgetpos64); |
| 169 | int fsetpos(FILE*, const fpos_t*) __RENAME(fsetpos64); |
| 170 | int fseeko(FILE*, off_t, int) __RENAME(fseeko64); |
| 171 | off_t ftello(FILE*) __RENAME(ftello64); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 172 | # if defined(__USE_BSD) |
| 173 | FILE* funopen(const void*, |
| 174 | int (*)(void*, char*, int), |
| 175 | int (*)(void*, const char*, int), |
| 176 | fpos_t (*)(void*, fpos_t, int), |
| 177 | int (*)(void*)) __RENAME(funopen64); |
| 178 | # endif |
Elliott Hughes | 68dc20d | 2015-02-06 22:28:49 -0800 | [diff] [blame] | 179 | #else |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 180 | int fgetpos(FILE*, fpos_t*); |
| 181 | int fsetpos(FILE*, const fpos_t*); |
| 182 | int fseeko(FILE*, off_t, int); |
| 183 | off_t ftello(FILE*); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 184 | # if defined(__USE_BSD) |
| 185 | FILE* funopen(const void*, |
| 186 | int (*)(void*, char*, int), |
| 187 | int (*)(void*, const char*, int), |
| 188 | fpos_t (*)(void*, fpos_t, int), |
| 189 | int (*)(void*)); |
| 190 | # endif |
Elliott Hughes | 68dc20d | 2015-02-06 22:28:49 -0800 | [diff] [blame] | 191 | #endif |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 192 | int fgetpos64(FILE*, fpos64_t*) __INTRODUCED_IN(24); |
| 193 | int fsetpos64(FILE*, const fpos64_t*) __INTRODUCED_IN(24); |
| 194 | int fseeko64(FILE*, off64_t, int) __INTRODUCED_IN(24); |
| 195 | off64_t ftello64(FILE*) __INTRODUCED_IN(24); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 196 | #if defined(__USE_BSD) |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 197 | FILE* funopen64(const void*, int (*)(void*, char*, int), int (*)(void*, const char*, int), |
| 198 | fpos64_t (*)(void*, fpos64_t, int), int (*)(void*)) __INTRODUCED_IN(24); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 199 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 200 | |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 201 | FILE* fopen(const char* __restrict, const char* __restrict); |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 202 | FILE* fopen64(const char* __restrict, const char* __restrict) __INTRODUCED_IN(24); |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 203 | FILE* freopen(const char* __restrict, const char* __restrict, FILE* __restrict); |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 204 | FILE* freopen64(const char* __restrict, const char* __restrict, FILE* __restrict) |
| 205 | __INTRODUCED_IN(24); |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 206 | FILE* tmpfile(void); |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 207 | FILE* tmpfile64(void) __INTRODUCED_IN(24); |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 208 | |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 209 | int snprintf(char* __restrict, size_t, const char* __restrict _Nonnull, ...) __printflike(3, 4); |
| 210 | int vfscanf(FILE* __restrict, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0); |
| 211 | int vscanf(const char* _Nonnull , __va_list) __scanflike(1, 0); |
| 212 | int vsnprintf(char* __restrict, size_t, const char* __restrict _Nonnull, __va_list) __printflike(3, 0); |
| 213 | int vsscanf(const char* __restrict _Nonnull, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 214 | |
Elliott Hughes | 3ba55f8 | 2016-06-08 18:11:23 -0700 | [diff] [blame] | 215 | #define L_ctermid 1024 /* size for ctermid() */ |
Josh Gao | 95fa26e | 2016-06-10 16:33:05 -0700 | [diff] [blame] | 216 | char* ctermid(char*) __INTRODUCED_IN_FUTURE; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 217 | |
Elliott Hughes | 3ba55f8 | 2016-06-08 18:11:23 -0700 | [diff] [blame] | 218 | FILE* fdopen(int, const char*); |
| 219 | int fileno(FILE*); |
| 220 | int pclose(FILE*); |
| 221 | FILE* popen(const char*, const char*); |
| 222 | void flockfile(FILE*); |
| 223 | int ftrylockfile(FILE*); |
| 224 | void funlockfile(FILE*); |
| 225 | int getc_unlocked(FILE*); |
| 226 | int getchar_unlocked(void); |
| 227 | int putc_unlocked(int, FILE*); |
| 228 | int putchar_unlocked(int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 229 | |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 230 | FILE* fmemopen(void*, size_t, const char*) __INTRODUCED_IN(23); |
| 231 | FILE* open_memstream(char**, size_t*) __INTRODUCED_IN(23); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 232 | |
Elliott Hughes | 9c8d711 | 2016-06-13 13:23:42 -0700 | [diff] [blame] | 233 | #if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */ |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 234 | int asprintf(char** __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3); |
Elliott Hughes | 3ba55f8 | 2016-06-08 18:11:23 -0700 | [diff] [blame] | 235 | char* fgetln(FILE* __restrict, size_t* __restrict); |
| 236 | int fpurge(FILE*); |
| 237 | void setbuffer(FILE*, char*, int); |
| 238 | int setlinebuf(FILE*); |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 239 | int vasprintf(char** __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0); |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 240 | void clearerr_unlocked(FILE*) __INTRODUCED_IN(23); |
| 241 | int feof_unlocked(FILE*) __INTRODUCED_IN(23); |
| 242 | int ferror_unlocked(FILE*) __INTRODUCED_IN(23); |
| 243 | int fileno_unlocked(FILE*) __INTRODUCED_IN(24); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 244 | #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) |
| 245 | #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) |
Elliott Hughes | 3ba55f8 | 2016-06-08 18:11:23 -0700 | [diff] [blame] | 246 | #endif /* __USE_BSD */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 247 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 248 | char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17); |
| 249 | char* __fgets_real(char*, int, FILE*) __RENAME(fgets); |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 250 | __errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer"); |
| 251 | __errordecl(__fgets_too_small_error, "fgets called with size less than zero"); |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 252 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 253 | size_t __fread_chk(void* __restrict, size_t, size_t, FILE* __restrict, size_t) |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 254 | __INTRODUCED_IN(24); |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 255 | size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread); |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 256 | __errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer"); |
| 257 | __errordecl(__fread_overflow, "fread called with overflowing size * count"); |
| 258 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 259 | size_t __fwrite_chk(const void* __restrict, size_t, size_t, FILE* __restrict, size_t) |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 260 | __INTRODUCED_IN(24); |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 261 | size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite); |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 262 | __errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer"); |
| 263 | __errordecl(__fwrite_overflow, "fwrite called with overflowing size * count"); |
| 264 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 265 | #if defined(__BIONIC_FORTIFY) && !defined(__BIONIC_NO_STDIO_FORTIFY) |
Elliott Hughes | ce45fea | 2012-10-22 16:10:27 -0700 | [diff] [blame] | 266 | |
Dan Albert | dfa6bbb | 2016-08-02 15:08:32 -0700 | [diff] [blame] | 267 | #if __ANDROID_API__ >= 17 |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 268 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 269 | __printflike(3, 0) int vsnprintf(char* dest, size_t size, const char* _Nonnull format, __va_list ap) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 270 | return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap); |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 273 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 274 | __printflike(2, 0) int vsprintf(char* dest, const char* _Nonnull format, __va_list ap) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 275 | return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap); |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 278 | #if defined(__clang__) |
Nick Kralevich | 7eb28b5 | 2014-03-18 17:03:38 -0700 | [diff] [blame] | 279 | #if !defined(snprintf) |
| 280 | #define __wrap_snprintf(dest, size, ...) __builtin___snprintf_chk(dest, size, 0, __bos(dest), __VA_ARGS__) |
| 281 | #define snprintf(...) __wrap_snprintf(__VA_ARGS__) |
| 282 | #endif |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 283 | #else |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 284 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 285 | __printflike(3, 4) int snprintf(char* dest, size_t size, const char* _Nonnull format, ...) { |
| 286 | return __builtin___snprintf_chk(dest, size, 0, __bos(dest), format, __builtin_va_arg_pack()); |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 287 | } |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 288 | #endif |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 289 | |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 290 | #if defined(__clang__) |
Nick Kralevich | 7eb28b5 | 2014-03-18 17:03:38 -0700 | [diff] [blame] | 291 | #if !defined(sprintf) |
| 292 | #define __wrap_sprintf(dest, ...) __builtin___sprintf_chk(dest, 0, __bos(dest), __VA_ARGS__) |
| 293 | #define sprintf(...) __wrap_sprintf(__VA_ARGS__) |
| 294 | #endif |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 295 | #else |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 296 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | 9eb3ae1 | 2016-06-30 09:12:40 -0700 | [diff] [blame] | 297 | __printflike(2, 3) int sprintf(char* dest, const char* _Nonnull format, ...) { |
| 298 | return __builtin___sprintf_chk(dest, 0, __bos(dest), format, __builtin_va_arg_pack()); |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 299 | } |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 300 | #endif |
Dan Albert | dfa6bbb | 2016-08-02 15:08:32 -0700 | [diff] [blame] | 301 | #endif /* __ANDROID_API__ >= 17 */ |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 302 | |
Dan Albert | dfa6bbb | 2016-08-02 15:08:32 -0700 | [diff] [blame] | 303 | #if __ANDROID_API__ >= 24 |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 304 | __BIONIC_FORTIFY_INLINE |
| 305 | size_t fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) { |
| 306 | size_t bos = __bos0(buf); |
| 307 | |
| 308 | #if !defined(__clang__) |
| 309 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 310 | return __fread_real(buf, size, count, stream); |
| 311 | } |
| 312 | |
| 313 | if (__builtin_constant_p(size) && __builtin_constant_p(count)) { |
| 314 | size_t total; |
| 315 | if (__size_mul_overflow(size, count, &total)) { |
| 316 | __fread_overflow(); |
| 317 | } |
| 318 | |
| 319 | if (total > bos) { |
| 320 | __fread_too_big_error(); |
| 321 | } |
| 322 | |
| 323 | return __fread_real(buf, size, count, stream); |
| 324 | } |
| 325 | #endif |
| 326 | |
| 327 | return __fread_chk(buf, size, count, stream, bos); |
| 328 | } |
| 329 | |
| 330 | __BIONIC_FORTIFY_INLINE |
| 331 | size_t fwrite(const void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) { |
| 332 | size_t bos = __bos0(buf); |
| 333 | |
| 334 | #if !defined(__clang__) |
| 335 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 336 | return __fwrite_real(buf, size, count, stream); |
| 337 | } |
| 338 | |
| 339 | if (__builtin_constant_p(size) && __builtin_constant_p(count)) { |
| 340 | size_t total; |
| 341 | if (__size_mul_overflow(size, count, &total)) { |
| 342 | __fwrite_overflow(); |
| 343 | } |
| 344 | |
| 345 | if (total > bos) { |
| 346 | __fwrite_too_big_error(); |
| 347 | } |
| 348 | |
| 349 | return __fwrite_real(buf, size, count, stream); |
| 350 | } |
| 351 | #endif |
| 352 | |
| 353 | return __fwrite_chk(buf, size, count, stream, bos); |
| 354 | } |
Dan Albert | dfa6bbb | 2016-08-02 15:08:32 -0700 | [diff] [blame] | 355 | #endif /* __ANDROID_API__ >= 24 */ |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 356 | |
Elliott Hughes | cd0609f | 2013-12-19 12:21:07 -0800 | [diff] [blame] | 357 | #if !defined(__clang__) |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 358 | |
| 359 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | cd0609f | 2013-12-19 12:21:07 -0800 | [diff] [blame] | 360 | char *fgets(char* dest, int size, FILE* stream) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 361 | size_t bos = __bos(dest); |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 362 | |
| 363 | // Compiler can prove, at compile time, that the passed in size |
| 364 | // is always negative. Force a compiler error. |
| 365 | if (__builtin_constant_p(size) && (size < 0)) { |
| 366 | __fgets_too_small_error(); |
| 367 | } |
| 368 | |
| 369 | // Compiler doesn't know destination size. Don't call __fgets_chk |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 370 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 371 | return __fgets_real(dest, size, stream); |
| 372 | } |
| 373 | |
| 374 | // Compiler can prove, at compile time, that the passed in size |
| 375 | // is always <= the actual object size. Don't call __fgets_chk |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 376 | if (__builtin_constant_p(size) && (size <= (int) bos)) { |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 377 | return __fgets_real(dest, size, stream); |
| 378 | } |
| 379 | |
| 380 | // Compiler can prove, at compile time, that the passed in size |
| 381 | // is always > the actual object size. Force a compiler error. |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 382 | if (__builtin_constant_p(size) && (size > (int) bos)) { |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 383 | __fgets_too_big_error(); |
| 384 | } |
| 385 | |
| 386 | return __fgets_chk(dest, size, stream, bos); |
| 387 | } |
| 388 | |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 389 | #endif /* !defined(__clang__) */ |
| 390 | |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 391 | #endif /* defined(__BIONIC_FORTIFY) */ |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 392 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 393 | #if defined(__clang__) |
| 394 | #pragma clang diagnostic pop |
| 395 | #endif |
| 396 | |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 397 | __END_DECLS |
| 398 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 399 | #endif /* _STDIO_H_ */ |