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 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 52 | typedef off_t fpos_t; /* stdio file position type */ |
| 53 | |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 54 | struct __sFILE; |
| 55 | typedef struct __sFILE FILE; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 56 | |
Elliott Hughes | 168667c | 2014-11-14 14:42:59 -0800 | [diff] [blame] | 57 | extern FILE* stdin; |
| 58 | extern FILE* stdout; |
| 59 | extern FILE* stderr; |
| 60 | /* C99 and earlier plus current C++ standards say these must be macros. */ |
| 61 | #define stdin stdin |
| 62 | #define stdout stdout |
| 63 | #define stderr stderr |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | |
| 65 | #define __SLBF 0x0001 /* line buffered */ |
| 66 | #define __SNBF 0x0002 /* unbuffered */ |
| 67 | #define __SRD 0x0004 /* OK to read */ |
| 68 | #define __SWR 0x0008 /* OK to write */ |
| 69 | /* RD and WR are never simultaneously asserted */ |
| 70 | #define __SRW 0x0010 /* open for reading & writing */ |
| 71 | #define __SEOF 0x0020 /* found EOF */ |
| 72 | #define __SERR 0x0040 /* found error */ |
| 73 | #define __SMBF 0x0080 /* _buf is from malloc */ |
| 74 | #define __SAPP 0x0100 /* fdopen()ed in append mode */ |
| 75 | #define __SSTR 0x0200 /* this is an sprintf/snprintf string */ |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 76 | #define __SOPT 0x0400 /* do fseek() optimization */ |
| 77 | #define __SNPT 0x0800 /* do not do fseek() optimization */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 78 | #define __SOFF 0x1000 /* set iff _offset is in fact correct */ |
| 79 | #define __SMOD 0x2000 /* true => fgetln modified _p text */ |
| 80 | #define __SALC 0x4000 /* allocate string space dynamically */ |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 81 | #define __SIGN 0x8000 /* ignore this file in _fwalk */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 82 | |
| 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 | |
| 108 | /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */ |
| 109 | #if __BSD_VISIBLE || __XPG_VISIBLE |
| 110 | #define P_tmpdir "/tmp/" |
| 111 | #endif |
| 112 | #define L_tmpnam 1024 /* XXX must be == PATH_MAX */ |
| 113 | #define TMP_MAX 308915776 |
| 114 | |
Elliott Hughes | 1ed337d | 2015-02-02 14:02:09 -0800 | [diff] [blame] | 115 | #define SEEK_SET 0 |
| 116 | #define SEEK_CUR 1 |
| 117 | #define SEEK_END 2 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 118 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 119 | /* |
| 120 | * Functions defined in ANSI C standard. |
| 121 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 122 | void clearerr(FILE *); |
| 123 | int fclose(FILE *); |
| 124 | int feof(FILE *); |
| 125 | int ferror(FILE *); |
| 126 | int fflush(FILE *); |
| 127 | int fgetc(FILE *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 128 | char *fgets(char * __restrict, int, FILE * __restrict); |
| 129 | FILE *fopen(const char * __restrict , const char * __restrict); |
| 130 | int fprintf(FILE * __restrict , const char * __restrict, ...) |
| 131 | __printflike(2, 3); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 132 | int fputc(int, FILE *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 133 | int fputs(const char * __restrict, FILE * __restrict); |
| 134 | size_t fread(void * __restrict, size_t, size_t, FILE * __restrict); |
| 135 | FILE *freopen(const char * __restrict, const char * __restrict, |
| 136 | FILE * __restrict); |
| 137 | int fscanf(FILE * __restrict, const char * __restrict, ...) |
| 138 | __scanflike(2, 3); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 139 | int fseek(FILE *, long, int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 140 | long ftell(FILE *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 141 | 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] | 142 | int getc(FILE *); |
| 143 | int getchar(void); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 144 | ssize_t getdelim(char ** __restrict, size_t * __restrict, int, |
| 145 | FILE * __restrict); |
| 146 | ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict); |
Elliott Hughes | c13fb75 | 2013-12-17 20:43:30 -0800 | [diff] [blame] | 147 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 148 | void perror(const char *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 149 | int printf(const char * __restrict, ...) |
| 150 | __printflike(1, 2); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 151 | int putc(int, FILE *); |
| 152 | int putchar(int); |
| 153 | int puts(const char *); |
| 154 | int remove(const char *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 155 | void rewind(FILE *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 156 | int scanf(const char * __restrict, ...) |
| 157 | __scanflike(1, 2); |
| 158 | void setbuf(FILE * __restrict, char * __restrict); |
| 159 | int setvbuf(FILE * __restrict, char * __restrict, int, size_t); |
| 160 | int sscanf(const char * __restrict, const char * __restrict, ...) |
| 161 | __scanflike(2, 3); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 162 | FILE *tmpfile(void); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 163 | int ungetc(int, FILE *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 164 | int vfprintf(FILE * __restrict, const char * __restrict, __va_list) |
| 165 | __printflike(2, 0); |
| 166 | int vprintf(const char * __restrict, __va_list) |
| 167 | __printflike(1, 0); |
| 168 | |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 169 | int dprintf(int, const char * __restrict, ...) __printflike(2, 3); |
| 170 | int vdprintf(int, const char * __restrict, __va_list) __printflike(2, 0); |
| 171 | |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 172 | #ifndef __AUDIT__ |
Dan Albert | 9635046 | 2014-06-17 23:31:21 +0000 | [diff] [blame] | 173 | #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L |
Yabin Cui | 9b4f77f | 2015-02-23 16:42:07 -0800 | [diff] [blame] | 174 | char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead"))); |
Dan Albert | 9635046 | 2014-06-17 23:31:21 +0000 | [diff] [blame] | 175 | #endif |
Elliott Hughes | c13fb75 | 2013-12-17 20:43:30 -0800 | [diff] [blame] | 176 | int sprintf(char* __restrict, const char* __restrict, ...) |
| 177 | __printflike(2, 3) __warnattr("sprintf is often misused; please use snprintf"); |
Elliott Hughes | c13fb75 | 2013-12-17 20:43:30 -0800 | [diff] [blame] | 178 | int vsprintf(char* __restrict, const char* __restrict, __va_list) |
| 179 | __printflike(2, 0) __warnattr("vsprintf is often misused; please use vsnprintf"); |
Yabin Cui | 9b4f77f | 2015-02-23 16:42:07 -0800 | [diff] [blame] | 180 | char* tmpnam(char*) __attribute__((deprecated("tmpnam is unsafe, use mkstemp or tmpfile instead"))); |
Elliott Hughes | c13fb75 | 2013-12-17 20:43:30 -0800 | [diff] [blame] | 181 | #if __XPG_VISIBLE |
| 182 | char* tempnam(const char*, const char*) |
Yabin Cui | 9b4f77f | 2015-02-23 16:42:07 -0800 | [diff] [blame] | 183 | __attribute__((deprecated("tempnam is unsafe, use mkstemp or tmpfile instead"))); |
Elliott Hughes | c13fb75 | 2013-12-17 20:43:30 -0800 | [diff] [blame] | 184 | #endif |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 185 | #endif |
| 186 | |
Elliott Hughes | f8fcfbc | 2013-10-22 13:28:46 -0700 | [diff] [blame] | 187 | extern int rename(const char*, const char*); |
| 188 | extern int renameat(int, const char*, int, const char*); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 189 | |
Elliott Hughes | 68dc20d | 2015-02-06 22:28:49 -0800 | [diff] [blame] | 190 | #if defined(__USE_FILE_OFFSET64) |
| 191 | /* Not possible. */ |
| 192 | int fgetpos(FILE * __restrict, fpos_t * __restrict) |
| 193 | __attribute__((__error__("not available with _FILE_OFFSET_BITS=64"))); |
| 194 | int fsetpos(FILE *, const fpos_t *) |
| 195 | __attribute__((__error__("not available with _FILE_OFFSET_BITS=64"))); |
| 196 | int fseeko(FILE *, off_t, int) |
| 197 | __attribute__((__error__("not available with _FILE_OFFSET_BITS=64"))); |
| 198 | off_t ftello(FILE *) |
| 199 | __attribute__((__error__("not available with _FILE_OFFSET_BITS=64"))); |
| 200 | #else |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 201 | int fgetpos(FILE * __restrict, fpos_t * __restrict); |
| 202 | int fsetpos(FILE *, const fpos_t *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 203 | int fseeko(FILE *, off_t, int); |
| 204 | off_t ftello(FILE *); |
Elliott Hughes | 68dc20d | 2015-02-06 22:28:49 -0800 | [diff] [blame] | 205 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 206 | |
| 207 | #if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 208 | int snprintf(char * __restrict, size_t, const char * __restrict, ...) |
| 209 | __printflike(3, 4); |
| 210 | int vfscanf(FILE * __restrict, const char * __restrict, __va_list) |
| 211 | __scanflike(2, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 212 | int vscanf(const char *, __va_list) |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 213 | __scanflike(1, 0); |
| 214 | int vsnprintf(char * __restrict, size_t, const char * __restrict, __va_list) |
| 215 | __printflike(3, 0); |
| 216 | int vsscanf(const char * __restrict, const char * __restrict, __va_list) |
| 217 | __scanflike(2, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 218 | #endif /* __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE */ |
| 219 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 220 | /* |
| 221 | * Functions defined in POSIX 1003.1. |
| 222 | */ |
| 223 | #if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE |
| 224 | #define L_ctermid 1024 /* size for ctermid(); PATH_MAX */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 225 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 226 | FILE *fdopen(int, const char *); |
| 227 | int fileno(FILE *); |
| 228 | |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 229 | #if (__POSIX_VISIBLE >= 199209) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 230 | int pclose(FILE *); |
| 231 | FILE *popen(const char *, const char *); |
| 232 | #endif |
| 233 | |
| 234 | #if __POSIX_VISIBLE >= 199506 |
| 235 | void flockfile(FILE *); |
| 236 | int ftrylockfile(FILE *); |
| 237 | void funlockfile(FILE *); |
| 238 | |
| 239 | /* |
| 240 | * These are normally used through macros as defined below, but POSIX |
| 241 | * requires functions as well. |
| 242 | */ |
| 243 | int getc_unlocked(FILE *); |
| 244 | int getchar_unlocked(void); |
| 245 | int putc_unlocked(int, FILE *); |
| 246 | int putchar_unlocked(int); |
| 247 | #endif /* __POSIX_VISIBLE >= 199506 */ |
| 248 | |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 249 | #if __POSIX_VISIBLE >= 200809 |
| 250 | FILE* fmemopen(void*, size_t, const char*); |
| 251 | FILE* open_memstream(char**, size_t*); |
| 252 | #endif /* __POSIX_VISIBLE >= 200809 */ |
| 253 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 254 | #endif /* __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE */ |
| 255 | |
| 256 | /* |
| 257 | * Routines that are purely local. |
| 258 | */ |
| 259 | #if __BSD_VISIBLE |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 260 | int asprintf(char ** __restrict, const char * __restrict, ...) |
| 261 | __printflike(2, 3); |
| 262 | char *fgetln(FILE * __restrict, size_t * __restrict); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 263 | int fpurge(FILE *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 264 | void setbuffer(FILE *, char *, int); |
| 265 | int setlinebuf(FILE *); |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 266 | int vasprintf(char ** __restrict, const char * __restrict, |
| 267 | __va_list) |
| 268 | __printflike(2, 0); |
Elliott Hughes | 2b021e1 | 2014-08-19 17:00:33 -0700 | [diff] [blame] | 269 | |
| 270 | void clearerr_unlocked(FILE*); |
| 271 | int feof_unlocked(FILE*); |
| 272 | int ferror_unlocked(FILE*); |
| 273 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 274 | /* |
| 275 | * Stdio function-access interface. |
| 276 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 277 | FILE *funopen(const void *, |
| 278 | int (*)(void *, char *, int), |
| 279 | int (*)(void *, const char *, int), |
| 280 | fpos_t (*)(void *, fpos_t, int), |
| 281 | int (*)(void *)); |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 282 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 283 | #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) |
| 284 | #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) |
| 285 | #endif /* __BSD_VISIBLE */ |
| 286 | |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 287 | extern char* __fgets_chk(char*, int, FILE*, size_t); |
| 288 | extern char* __fgets_real(char*, int, FILE*) __RENAME(fgets); |
| 289 | __errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer"); |
| 290 | __errordecl(__fgets_too_small_error, "fgets called with size less than zero"); |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 291 | |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 292 | extern size_t __fread_chk(void * __restrict, size_t, size_t, FILE * __restrict, size_t); |
| 293 | extern size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread); |
| 294 | __errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer"); |
| 295 | __errordecl(__fread_overflow, "fread called with overflowing size * count"); |
| 296 | |
| 297 | extern size_t __fwrite_chk(const void * __restrict, size_t, size_t, FILE * __restrict, size_t); |
| 298 | extern size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite); |
| 299 | __errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer"); |
| 300 | __errordecl(__fwrite_overflow, "fwrite called with overflowing size * count"); |
| 301 | |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 302 | #if defined(__BIONIC_FORTIFY) |
Elliott Hughes | ce45fea | 2012-10-22 16:10:27 -0700 | [diff] [blame] | 303 | |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 304 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 305 | __printflike(3, 0) |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 306 | 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] | 307 | { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 308 | return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap); |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 311 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 312 | __printflike(2, 0) |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 313 | int vsprintf(char *dest, const char *format, __va_list ap) |
| 314 | { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 315 | return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap); |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 318 | #if defined(__clang__) |
Nick Kralevich | 7eb28b5 | 2014-03-18 17:03:38 -0700 | [diff] [blame] | 319 | #if !defined(snprintf) |
| 320 | #define __wrap_snprintf(dest, size, ...) __builtin___snprintf_chk(dest, size, 0, __bos(dest), __VA_ARGS__) |
| 321 | #define snprintf(...) __wrap_snprintf(__VA_ARGS__) |
| 322 | #endif |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 323 | #else |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 324 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 325 | __printflike(3, 4) |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 326 | int snprintf(char *dest, size_t size, const char *format, ...) |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 327 | { |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 328 | return __builtin___snprintf_chk(dest, size, 0, |
| 329 | __bos(dest), format, __builtin_va_arg_pack()); |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 330 | } |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 331 | #endif |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 332 | |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 333 | #if defined(__clang__) |
Nick Kralevich | 7eb28b5 | 2014-03-18 17:03:38 -0700 | [diff] [blame] | 334 | #if !defined(sprintf) |
| 335 | #define __wrap_sprintf(dest, ...) __builtin___sprintf_chk(dest, 0, __bos(dest), __VA_ARGS__) |
| 336 | #define sprintf(...) __wrap_sprintf(__VA_ARGS__) |
| 337 | #endif |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 338 | #else |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 339 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | d04c183 | 2013-05-14 16:08:43 -0700 | [diff] [blame] | 340 | __printflike(2, 3) |
Nick Kralevich | 9b549c3 | 2012-06-12 15:59:04 -0700 | [diff] [blame] | 341 | int sprintf(char *dest, const char *format, ...) |
| 342 | { |
| 343 | return __builtin___sprintf_chk(dest, 0, |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 344 | __bos(dest), format, __builtin_va_arg_pack()); |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 345 | } |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 346 | #endif |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 347 | |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 348 | __BIONIC_FORTIFY_INLINE |
| 349 | size_t fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) { |
| 350 | size_t bos = __bos0(buf); |
| 351 | |
| 352 | #if !defined(__clang__) |
| 353 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 354 | return __fread_real(buf, size, count, stream); |
| 355 | } |
| 356 | |
| 357 | if (__builtin_constant_p(size) && __builtin_constant_p(count)) { |
| 358 | size_t total; |
| 359 | if (__size_mul_overflow(size, count, &total)) { |
| 360 | __fread_overflow(); |
| 361 | } |
| 362 | |
| 363 | if (total > bos) { |
| 364 | __fread_too_big_error(); |
| 365 | } |
| 366 | |
| 367 | return __fread_real(buf, size, count, stream); |
| 368 | } |
| 369 | #endif |
| 370 | |
| 371 | return __fread_chk(buf, size, count, stream, bos); |
| 372 | } |
| 373 | |
| 374 | __BIONIC_FORTIFY_INLINE |
| 375 | size_t fwrite(const void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) { |
| 376 | size_t bos = __bos0(buf); |
| 377 | |
| 378 | #if !defined(__clang__) |
| 379 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 380 | return __fwrite_real(buf, size, count, stream); |
| 381 | } |
| 382 | |
| 383 | if (__builtin_constant_p(size) && __builtin_constant_p(count)) { |
| 384 | size_t total; |
| 385 | if (__size_mul_overflow(size, count, &total)) { |
| 386 | __fwrite_overflow(); |
| 387 | } |
| 388 | |
| 389 | if (total > bos) { |
| 390 | __fwrite_too_big_error(); |
| 391 | } |
| 392 | |
| 393 | return __fwrite_real(buf, size, count, stream); |
| 394 | } |
| 395 | #endif |
| 396 | |
| 397 | return __fwrite_chk(buf, size, count, stream, bos); |
| 398 | } |
| 399 | |
Elliott Hughes | cd0609f | 2013-12-19 12:21:07 -0800 | [diff] [blame] | 400 | #if !defined(__clang__) |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 401 | |
| 402 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | cd0609f | 2013-12-19 12:21:07 -0800 | [diff] [blame] | 403 | char *fgets(char* dest, int size, FILE* stream) { |
Nick Kralevich | 9020fd5 | 2013-04-30 11:31:35 -0700 | [diff] [blame] | 404 | size_t bos = __bos(dest); |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 405 | |
| 406 | // Compiler can prove, at compile time, that the passed in size |
| 407 | // is always negative. Force a compiler error. |
| 408 | if (__builtin_constant_p(size) && (size < 0)) { |
| 409 | __fgets_too_small_error(); |
| 410 | } |
| 411 | |
| 412 | // Compiler doesn't know destination size. Don't call __fgets_chk |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 413 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 414 | return __fgets_real(dest, size, stream); |
| 415 | } |
| 416 | |
| 417 | // Compiler can prove, at compile time, that the passed in size |
| 418 | // is always <= the actual object size. Don't call __fgets_chk |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 419 | if (__builtin_constant_p(size) && (size <= (int) bos)) { |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 420 | return __fgets_real(dest, size, stream); |
| 421 | } |
| 422 | |
| 423 | // Compiler can prove, at compile time, that the passed in size |
| 424 | // is always > the actual object size. Force a compiler error. |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 425 | if (__builtin_constant_p(size) && (size > (int) bos)) { |
Nick Kralevich | 965dbc6 | 2012-07-03 11:45:31 -0700 | [diff] [blame] | 426 | __fgets_too_big_error(); |
| 427 | } |
| 428 | |
| 429 | return __fgets_chk(dest, size, stream, bos); |
| 430 | } |
| 431 | |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 432 | #endif /* !defined(__clang__) */ |
| 433 | |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 434 | #endif /* defined(__BIONIC_FORTIFY) */ |
Nick Kralevich | cffdf66 | 2012-06-11 15:50:57 -0700 | [diff] [blame] | 435 | |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 436 | __END_DECLS |
| 437 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 438 | #endif /* _STDIO_H_ */ |