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 | |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 50 | __BEGIN_DECLS |
| 51 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 52 | typedef off_t fpos_t; |
| 53 | typedef off64_t fpos64_t; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 54 | |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 55 | struct __sFILE; |
| 56 | typedef struct __sFILE FILE; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 57 | |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 58 | extern FILE* stdin __INTRODUCED_IN(23); |
| 59 | extern FILE* stdout __INTRODUCED_IN(23); |
| 60 | extern FILE* stderr __INTRODUCED_IN(23); |
Elliott Hughes | 168667c | 2014-11-14 14:42:59 -0800 | [diff] [blame] | 61 | /* C99 and earlier plus current C++ standards say these must be macros. */ |
| 62 | #define stdin stdin |
| 63 | #define stdout stdout |
| 64 | #define stderr stderr |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 65 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 66 | /* |
| 67 | * The following three definitions are for ANSI C, which took them |
| 68 | * from System V, which brilliantly took internal interface macros and |
| 69 | * made them official arguments to setvbuf(), without renaming them. |
| 70 | * Hence, these ugly _IOxxx names are *supposed* to appear in user code. |
| 71 | * |
| 72 | * Although numbered as their counterparts above, the implementation |
| 73 | * does not rely on this. |
| 74 | */ |
| 75 | #define _IOFBF 0 /* setvbuf should set fully buffered */ |
| 76 | #define _IOLBF 1 /* setvbuf should set line buffered */ |
| 77 | #define _IONBF 2 /* setvbuf should set unbuffered */ |
| 78 | |
| 79 | #define BUFSIZ 1024 /* size of buffer used by setbuf */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 80 | #define EOF (-1) |
| 81 | |
| 82 | /* |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 83 | * FOPEN_MAX is a minimum maximum, and is the number of streams that |
| 84 | * stdio can provide without attempting to allocate further resources |
| 85 | * (which could fail). Do not use this for anything. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 86 | */ |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 87 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 88 | #define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */ |
| 89 | #define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */ |
| 90 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 91 | #define L_tmpnam 1024 /* XXX must be == PATH_MAX */ |
| 92 | #define TMP_MAX 308915776 |
| 93 | |
Elliott Hughes | 1ed337d | 2015-02-02 14:02:09 -0800 | [diff] [blame] | 94 | #define SEEK_SET 0 |
| 95 | #define SEEK_CUR 1 |
| 96 | #define SEEK_END 2 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 97 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 98 | /* |
| 99 | * Functions defined in ANSI C standard. |
| 100 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 101 | void clearerr(FILE *); |
| 102 | int fclose(FILE *); |
| 103 | int feof(FILE *); |
| 104 | int ferror(FILE *); |
| 105 | int fflush(FILE *); |
| 106 | int fgetc(FILE *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 107 | char *fgets(char * __restrict, int, FILE * __restrict); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 108 | int fprintf(FILE * __restrict , const char * __restrict, ...) |
| 109 | __printflike(2, 3); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 110 | int fputc(int, FILE *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 111 | int fputs(const char * __restrict, FILE * __restrict); |
| 112 | size_t fread(void * __restrict, size_t, size_t, FILE * __restrict); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 113 | int fscanf(FILE * __restrict, const char * __restrict, ...) |
| 114 | __scanflike(2, 3); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 115 | 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] | 116 | int getc(FILE *); |
| 117 | int getchar(void); |
Josh Gao | 46b4416 | 2016-05-27 11:14:16 -0700 | [diff] [blame] | 118 | ssize_t getdelim(char** __restrict, size_t* __restrict, int, FILE* __restrict) __INTRODUCED_IN(18); |
| 119 | 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] | 120 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 121 | void perror(const char *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 122 | int printf(const char * __restrict, ...) |
| 123 | __printflike(1, 2); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 124 | int putc(int, FILE *); |
| 125 | int putchar(int); |
| 126 | int puts(const char *); |
| 127 | int remove(const char *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 128 | void rewind(FILE *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 129 | int scanf(const char * __restrict, ...) |
| 130 | __scanflike(1, 2); |
| 131 | void setbuf(FILE * __restrict, char * __restrict); |
| 132 | int setvbuf(FILE * __restrict, char * __restrict, int, size_t); |
| 133 | int sscanf(const char * __restrict, const char * __restrict, ...) |
| 134 | __scanflike(2, 3); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 135 | int ungetc(int, FILE *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 136 | int vfprintf(FILE * __restrict, const char * __restrict, __va_list) |
| 137 | __printflike(2, 0); |
| 138 | int vprintf(const char * __restrict, __va_list) |
| 139 | __printflike(1, 0); |
| 140 | |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 141 | int dprintf(int, const char* __restrict, ...) __printflike(2, 3) __INTRODUCED_IN(21); |
| 142 | int vdprintf(int, const char* __restrict, __va_list) __printflike(2, 0) __INTRODUCED_IN(21); |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 143 | |
Elliott Hughes | 3ba55f8 | 2016-06-08 18:11:23 -0700 | [diff] [blame^] | 144 | #if __STDC_VERSION__ < 201112L |
Yabin Cui | 9b4f77f | 2015-02-23 16:42:07 -0800 | [diff] [blame] | 145 | char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead"))); |
Dan Albert | 9635046 | 2014-06-17 23:31:21 +0000 | [diff] [blame] | 146 | #endif |
Elliott Hughes | c13fb75 | 2013-12-17 20:43:30 -0800 | [diff] [blame] | 147 | int sprintf(char* __restrict, const char* __restrict, ...) |
| 148 | __printflike(2, 3) __warnattr("sprintf is often misused; please use snprintf"); |
Elliott Hughes | c13fb75 | 2013-12-17 20:43:30 -0800 | [diff] [blame] | 149 | int vsprintf(char* __restrict, const char* __restrict, __va_list) |
| 150 | __printflike(2, 0) __warnattr("vsprintf is often misused; please use vsnprintf"); |
Yabin Cui | 9b4f77f | 2015-02-23 16:42:07 -0800 | [diff] [blame] | 151 | 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^] | 152 | #if defined(__USE_BSD) || defined(__USE_GNU) |
| 153 | #define P_tmpdir "/tmp/" /* deprecated */ |
Elliott Hughes | c13fb75 | 2013-12-17 20:43:30 -0800 | [diff] [blame] | 154 | char* tempnam(const char*, const char*) |
Yabin Cui | 9b4f77f | 2015-02-23 16:42:07 -0800 | [diff] [blame] | 155 | __attribute__((deprecated("tempnam is unsafe, use mkstemp or tmpfile instead"))); |
Elliott Hughes | c13fb75 | 2013-12-17 20:43:30 -0800 | [diff] [blame] | 156 | #endif |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 157 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 158 | int rename(const char*, const char*); |
| 159 | int renameat(int, const char*, int, const char*); |
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 fseek(FILE*, long, int); |
| 162 | long ftell(FILE*); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 163 | |
Elliott Hughes | 68dc20d | 2015-02-06 22:28:49 -0800 | [diff] [blame] | 164 | #if defined(__USE_FILE_OFFSET64) |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 165 | int fgetpos(FILE*, fpos_t*) __RENAME(fgetpos64); |
| 166 | int fsetpos(FILE*, const fpos_t*) __RENAME(fsetpos64); |
| 167 | int fseeko(FILE*, off_t, int) __RENAME(fseeko64); |
| 168 | off_t ftello(FILE*) __RENAME(ftello64); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 169 | # if defined(__USE_BSD) |
| 170 | FILE* funopen(const void*, |
| 171 | int (*)(void*, char*, int), |
| 172 | int (*)(void*, const char*, int), |
| 173 | fpos_t (*)(void*, fpos_t, int), |
| 174 | int (*)(void*)) __RENAME(funopen64); |
| 175 | # endif |
Elliott Hughes | 68dc20d | 2015-02-06 22:28:49 -0800 | [diff] [blame] | 176 | #else |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 177 | int fgetpos(FILE*, fpos_t*); |
| 178 | int fsetpos(FILE*, const fpos_t*); |
| 179 | int fseeko(FILE*, off_t, int); |
| 180 | off_t ftello(FILE*); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 181 | # if defined(__USE_BSD) |
| 182 | FILE* funopen(const void*, |
| 183 | int (*)(void*, char*, int), |
| 184 | int (*)(void*, const char*, int), |
| 185 | fpos_t (*)(void*, fpos_t, int), |
| 186 | int (*)(void*)); |
| 187 | # endif |
Elliott Hughes | 68dc20d | 2015-02-06 22:28:49 -0800 | [diff] [blame] | 188 | #endif |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 189 | int fgetpos64(FILE*, fpos64_t*) __INTRODUCED_IN(24); |
| 190 | int fsetpos64(FILE*, const fpos64_t*) __INTRODUCED_IN(24); |
| 191 | int fseeko64(FILE*, off64_t, int) __INTRODUCED_IN(24); |
| 192 | off64_t ftello64(FILE*) __INTRODUCED_IN(24); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 193 | #if defined(__USE_BSD) |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 194 | FILE* funopen64(const void*, int (*)(void*, char*, int), int (*)(void*, const char*, int), |
| 195 | fpos64_t (*)(void*, fpos64_t, int), int (*)(void*)) __INTRODUCED_IN(24); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 196 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 197 | |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 198 | FILE* fopen(const char* __restrict, const char* __restrict); |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 199 | FILE* fopen64(const char* __restrict, const char* __restrict) __INTRODUCED_IN(24); |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 200 | FILE* freopen(const char* __restrict, const char* __restrict, FILE* __restrict); |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 201 | FILE* freopen64(const char* __restrict, const char* __restrict, FILE* __restrict) |
| 202 | __INTRODUCED_IN(24); |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 203 | FILE* tmpfile(void); |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 204 | FILE* tmpfile64(void) __INTRODUCED_IN(24); |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 205 | |
Elliott Hughes | 3ba55f8 | 2016-06-08 18:11:23 -0700 | [diff] [blame^] | 206 | int snprintf(char* __restrict, size_t, const char* __restrict, ...) __printflike(3, 4); |
| 207 | int vfscanf(FILE* __restrict, const char* __restrict, __va_list) __scanflike(2, 0); |
| 208 | int vscanf(const char*, __va_list) __scanflike(1, 0); |
| 209 | int vsnprintf(char* __restrict, size_t, const char* __restrict, __va_list) __printflike(3, 0); |
| 210 | int vsscanf(const char* __restrict, const char* __restrict, __va_list) __scanflike(2, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 211 | |
Elliott Hughes | 3ba55f8 | 2016-06-08 18:11:23 -0700 | [diff] [blame^] | 212 | #define L_ctermid 1024 /* size for ctermid() */ |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 213 | char* ctermid(char*); |
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 | FILE* fdopen(int, const char*); |
| 216 | int fileno(FILE*); |
| 217 | int pclose(FILE*); |
| 218 | FILE* popen(const char*, const char*); |
| 219 | void flockfile(FILE*); |
| 220 | int ftrylockfile(FILE*); |
| 221 | void funlockfile(FILE*); |
| 222 | int getc_unlocked(FILE*); |
| 223 | int getchar_unlocked(void); |
| 224 | int putc_unlocked(int, FILE*); |
| 225 | int putchar_unlocked(int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 226 | |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 227 | FILE* fmemopen(void*, size_t, const char*) __INTRODUCED_IN(23); |
| 228 | FILE* open_memstream(char**, size_t*) __INTRODUCED_IN(23); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * Routines that are purely local. |
| 232 | */ |
Elliott Hughes | 3ba55f8 | 2016-06-08 18:11:23 -0700 | [diff] [blame^] | 233 | #if defined(__USE_BSD) |
| 234 | int asprintf(char** __restrict, const char* __restrict, ...) __printflike(2, 3); |
| 235 | char* fgetln(FILE* __restrict, size_t* __restrict); |
| 236 | int fpurge(FILE*); |
| 237 | void setbuffer(FILE*, char*, int); |
| 238 | int setlinebuf(FILE*); |
| 239 | int vasprintf(char** __restrict, const char* __restrict, __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 | |
Josh Gao | 46b4416 | 2016-05-27 11:14:16 -0700 | [diff] [blame] | 248 | extern char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17); |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 249 | extern char* __fgets_real(char*, int, FILE*) __RENAME(fgets); |
| 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 | |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 253 | extern size_t __fread_chk(void* __restrict, size_t, size_t, FILE* __restrict, size_t) |
| 254 | __INTRODUCED_IN(24); |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 255 | extern size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread); |
| 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 | |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 259 | extern size_t __fwrite_chk(const void* __restrict, size_t, size_t, FILE* __restrict, size_t) |
| 260 | __INTRODUCED_IN(24); |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 261 | extern size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite); |
| 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 | |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 265 | #if defined(__BIONIC_FORTIFY) |
Elliott Hughes | ce45fea | 2012-10-22 16:10:27 -0700 | [diff] [blame] | 266 | |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 267 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 268 | __printflike(3, 0) |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 269 | int vsnprintf(char *dest, size_t size, const char *format, __va_list ap) |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 270 | { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 271 | return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap); |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 274 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 275 | __printflike(2, 0) |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 276 | int vsprintf(char *dest, const char *format, __va_list ap) |
| 277 | { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 278 | return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap); |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 281 | #if defined(__clang__) |
Nick Kralevich | 7eb28b5 | 2014-03-18 17:03:38 -0700 | [diff] [blame] | 282 | #if !defined(snprintf) |
| 283 | #define __wrap_snprintf(dest, size, ...) __builtin___snprintf_chk(dest, size, 0, __bos(dest), __VA_ARGS__) |
| 284 | #define snprintf(...) __wrap_snprintf(__VA_ARGS__) |
| 285 | #endif |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 286 | #else |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 287 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 288 | __printflike(3, 4) |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 289 | int snprintf(char *dest, size_t size, const char *format, ...) |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 290 | { |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 291 | return __builtin___snprintf_chk(dest, size, 0, |
| 292 | __bos(dest), format, __builtin_va_arg_pack()); |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 293 | } |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 294 | #endif |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 295 | |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 296 | #if defined(__clang__) |
Nick Kralevich | 7eb28b5 | 2014-03-18 17:03:38 -0700 | [diff] [blame] | 297 | #if !defined(sprintf) |
| 298 | #define __wrap_sprintf(dest, ...) __builtin___sprintf_chk(dest, 0, __bos(dest), __VA_ARGS__) |
| 299 | #define sprintf(...) __wrap_sprintf(__VA_ARGS__) |
| 300 | #endif |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 301 | #else |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 302 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 303 | __printflike(2, 3) |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 304 | int sprintf(char *dest, const char *format, ...) |
| 305 | { |
| 306 | return __builtin___sprintf_chk(dest, 0, |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 307 | __bos(dest), format, __builtin_va_arg_pack()); |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 308 | } |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 309 | #endif |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 310 | |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 311 | __BIONIC_FORTIFY_INLINE |
| 312 | size_t fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) { |
| 313 | size_t bos = __bos0(buf); |
| 314 | |
| 315 | #if !defined(__clang__) |
| 316 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 317 | return __fread_real(buf, size, count, stream); |
| 318 | } |
| 319 | |
| 320 | if (__builtin_constant_p(size) && __builtin_constant_p(count)) { |
| 321 | size_t total; |
| 322 | if (__size_mul_overflow(size, count, &total)) { |
| 323 | __fread_overflow(); |
| 324 | } |
| 325 | |
| 326 | if (total > bos) { |
| 327 | __fread_too_big_error(); |
| 328 | } |
| 329 | |
| 330 | return __fread_real(buf, size, count, stream); |
| 331 | } |
| 332 | #endif |
| 333 | |
| 334 | return __fread_chk(buf, size, count, stream, bos); |
| 335 | } |
| 336 | |
| 337 | __BIONIC_FORTIFY_INLINE |
| 338 | size_t fwrite(const void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) { |
| 339 | size_t bos = __bos0(buf); |
| 340 | |
| 341 | #if !defined(__clang__) |
| 342 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 343 | return __fwrite_real(buf, size, count, stream); |
| 344 | } |
| 345 | |
| 346 | if (__builtin_constant_p(size) && __builtin_constant_p(count)) { |
| 347 | size_t total; |
| 348 | if (__size_mul_overflow(size, count, &total)) { |
| 349 | __fwrite_overflow(); |
| 350 | } |
| 351 | |
| 352 | if (total > bos) { |
| 353 | __fwrite_too_big_error(); |
| 354 | } |
| 355 | |
| 356 | return __fwrite_real(buf, size, count, stream); |
| 357 | } |
| 358 | #endif |
| 359 | |
| 360 | return __fwrite_chk(buf, size, count, stream, bos); |
| 361 | } |
| 362 | |
Elliott Hughes | cd0609f | 2013-12-19 12:21:07 -0800 | [diff] [blame] | 363 | #if !defined(__clang__) |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 364 | |
| 365 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | cd0609f | 2013-12-19 12:21:07 -0800 | [diff] [blame] | 366 | char *fgets(char* dest, int size, FILE* stream) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 367 | size_t bos = __bos(dest); |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 368 | |
| 369 | // Compiler can prove, at compile time, that the passed in size |
| 370 | // is always negative. Force a compiler error. |
| 371 | if (__builtin_constant_p(size) && (size < 0)) { |
| 372 | __fgets_too_small_error(); |
| 373 | } |
| 374 | |
| 375 | // Compiler doesn't know destination size. Don't call __fgets_chk |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 376 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
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. Don't call __fgets_chk |
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 | return __fgets_real(dest, size, stream); |
| 384 | } |
| 385 | |
| 386 | // Compiler can prove, at compile time, that the passed in size |
| 387 | // is always > the actual object size. Force a compiler error. |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 388 | if (__builtin_constant_p(size) && (size > (int) bos)) { |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 389 | __fgets_too_big_error(); |
| 390 | } |
| 391 | |
| 392 | return __fgets_chk(dest, size, stream, bos); |
| 393 | } |
| 394 | |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 395 | #endif /* !defined(__clang__) */ |
| 396 | |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 397 | #endif /* defined(__BIONIC_FORTIFY) */ |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 398 | |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 399 | __END_DECLS |
| 400 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 401 | #endif /* _STDIO_H_ */ |