blob: 38021efe9aa2b824886293663f72b500ee553db7 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $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 Hughes3975cec2012-11-29 17:25:23 -080042#include <sys/types.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044#include <stdarg.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045#include <stddef.h>
46
Elliott Hughes7d56ccb2012-10-01 17:56:58 -070047#define __need_NULL
48#include <stddef.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049
Elliott Hughesfd936ae2016-08-12 10:16:34 -070050#include <bits/seek_constants.h>
51
Dan Albert658727e2014-10-07 11:10:36 -070052__BEGIN_DECLS
53
Elliott Hughes5470c182016-07-22 11:36:17 -070054#if defined(__clang__)
55#pragma clang diagnostic push
56#pragma clang diagnostic ignored "-Wnullability-completeness"
57#endif
58
Elliott Hughes9677fab2016-01-25 15:50:59 -080059typedef off_t fpos_t;
60typedef off64_t fpos64_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061
Elliott Hughesf0141df2015-10-12 12:44:23 -070062struct __sFILE;
63typedef struct __sFILE FILE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
Dan Albert32c79c22016-07-15 11:32:23 -070065#if __ANDROID_API__ >= 23
Josh Gao14adff12016-04-29 12:00:55 -070066extern FILE* stdin __INTRODUCED_IN(23);
67extern FILE* stdout __INTRODUCED_IN(23);
68extern FILE* stderr __INTRODUCED_IN(23);
Dan Albert32c79c22016-07-15 11:32:23 -070069
Elliott Hughes168667c2014-11-14 14:42:59 -080070/* 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 Albert32c79c22016-07-15 11:32:23 -070074#else
75/* Before M the actual symbols for stdin and friends had different names. */
76extern 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 Project1dc9e472009-03-03 19:28:35 -080082
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083/*
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 Project1dc9e472009-03-03 19:28:35 -080097#define EOF (-1)
98
99/*
Elliott Hughesd04c1832013-05-14 16:08:43 -0700100 * 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 Project1dc9e472009-03-03 19:28:35 -0800103 */
Elliott Hughesd04c1832013-05-14 16:08:43 -0700104
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105#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 Project1dc9e472009-03-03 19:28:35 -0800108#define L_tmpnam 1024 /* XXX must be == PATH_MAX */
109#define TMP_MAX 308915776
110
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111/*
112 * Functions defined in ANSI C standard.
113 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114void clearerr(FILE *);
115int fclose(FILE *);
116int feof(FILE *);
117int ferror(FILE *);
118int fflush(FILE *);
119int fgetc(FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700120char *fgets(char * __restrict, int, FILE * __restrict);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700121int fprintf(FILE * __restrict , const char * __restrict _Nonnull, ...) __printflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122int fputc(int, FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700123int fputs(const char * __restrict, FILE * __restrict);
124size_t fread(void * __restrict, size_t, size_t, FILE * __restrict);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700125int fscanf(FILE * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700126size_t fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127int getc(FILE *);
128int getchar(void);
Josh Gao46b44162016-05-27 11:14:16 -0700129ssize_t getdelim(char** __restrict, size_t* __restrict, int, FILE* __restrict) __INTRODUCED_IN(18);
130ssize_t getline(char** __restrict, size_t* __restrict, FILE* __restrict) __INTRODUCED_IN(18);
Elliott Hughesc13fb752013-12-17 20:43:30 -0800131
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132void perror(const char *);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700133int printf(const char * __restrict _Nonnull, ...) __printflike(1, 2);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134int putc(int, FILE *);
135int putchar(int);
136int puts(const char *);
137int remove(const char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138void rewind(FILE *);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700139int scanf(const char * __restrict _Nonnull, ...) __scanflike(1, 2);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700140void setbuf(FILE * __restrict, char * __restrict);
141int setvbuf(FILE * __restrict, char * __restrict, int, size_t);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700142int sscanf(const char * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143int ungetc(int, FILE *);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700144int vfprintf(FILE * __restrict, const char * __restrict _Nonnull, __va_list) __printflike(2, 0);
145int vprintf(const char * __restrict _Nonnull, __va_list) __printflike(1, 0);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700146
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700147int dprintf(int, const char* __restrict _Nonnull, ...) __printflike(2, 3) __INTRODUCED_IN(21);
148int vdprintf(int, const char* __restrict _Nonnull, __va_list) __printflike(2, 0) __INTRODUCED_IN(21);
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700149
Elliott Hughesf6495c72016-07-25 09:20:57 -0700150#if (defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L) || \
151 (defined(__cplusplus) && __cplusplus <= 201103L)
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800152char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead")));
Dan Albert96350462014-06-17 23:31:21 +0000153#endif
Elliott Hughes5470c182016-07-22 11:36:17 -0700154int sprintf(char* __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3);
155int vsprintf(char* __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0);
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800156char* tmpnam(char*) __attribute__((deprecated("tmpnam is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700157#define P_tmpdir "/tmp/" /* deprecated */
Elliott Hughesc13fb752013-12-17 20:43:30 -0800158char* tempnam(const char*, const char*)
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800159 __attribute__((deprecated("tempnam is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughesd04c1832013-05-14 16:08:43 -0700160
Elliott Hughes9677fab2016-01-25 15:50:59 -0800161int rename(const char*, const char*);
162int renameat(int, const char*, int, const char*);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700163
Elliott Hughes9677fab2016-01-25 15:50:59 -0800164int fseek(FILE*, long, int);
165long ftell(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800166
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800167#if defined(__USE_FILE_OFFSET64)
Elliott Hughes9677fab2016-01-25 15:50:59 -0800168int fgetpos(FILE*, fpos_t*) __RENAME(fgetpos64);
169int fsetpos(FILE*, const fpos_t*) __RENAME(fsetpos64);
170int fseeko(FILE*, off_t, int) __RENAME(fseeko64);
171off_t ftello(FILE*) __RENAME(ftello64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800172# if defined(__USE_BSD)
173FILE* 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 Hughes68dc20d2015-02-06 22:28:49 -0800179#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800180int fgetpos(FILE*, fpos_t*);
181int fsetpos(FILE*, const fpos_t*);
182int fseeko(FILE*, off_t, int);
183off_t ftello(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800184# if defined(__USE_BSD)
185FILE* 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 Hughes68dc20d2015-02-06 22:28:49 -0800191#endif
Josh Gao14adff12016-04-29 12:00:55 -0700192int fgetpos64(FILE*, fpos64_t*) __INTRODUCED_IN(24);
193int fsetpos64(FILE*, const fpos64_t*) __INTRODUCED_IN(24);
194int fseeko64(FILE*, off64_t, int) __INTRODUCED_IN(24);
195off64_t ftello64(FILE*) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800196#if defined(__USE_BSD)
Josh Gao14adff12016-04-29 12:00:55 -0700197FILE* 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 Hughes03e65eb2016-01-26 14:13:04 -0800199#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200
Elliott Hughesf226ee52016-02-03 11:24:28 -0800201FILE* fopen(const char* __restrict, const char* __restrict);
Josh Gao14adff12016-04-29 12:00:55 -0700202FILE* fopen64(const char* __restrict, const char* __restrict) __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800203FILE* freopen(const char* __restrict, const char* __restrict, FILE* __restrict);
Josh Gao14adff12016-04-29 12:00:55 -0700204FILE* freopen64(const char* __restrict, const char* __restrict, FILE* __restrict)
205 __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800206FILE* tmpfile(void);
Josh Gao14adff12016-04-29 12:00:55 -0700207FILE* tmpfile64(void) __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800208
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700209int snprintf(char* __restrict, size_t, const char* __restrict _Nonnull, ...) __printflike(3, 4);
210int vfscanf(FILE* __restrict, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0);
211int vscanf(const char* _Nonnull , __va_list) __scanflike(1, 0);
212int vsnprintf(char* __restrict, size_t, const char* __restrict _Nonnull, __va_list) __printflike(3, 0);
213int vsscanf(const char* __restrict _Nonnull, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700215#define L_ctermid 1024 /* size for ctermid() */
Josh Gao95fa26e2016-06-10 16:33:05 -0700216char* ctermid(char*) __INTRODUCED_IN_FUTURE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700218FILE* fdopen(int, const char*);
219int fileno(FILE*);
220int pclose(FILE*);
221FILE* popen(const char*, const char*);
222void flockfile(FILE*);
223int ftrylockfile(FILE*);
224void funlockfile(FILE*);
225int getc_unlocked(FILE*);
226int getchar_unlocked(void);
227int putc_unlocked(int, FILE*);
228int putchar_unlocked(int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229
Josh Gao14adff12016-04-29 12:00:55 -0700230FILE* fmemopen(void*, size_t, const char*) __INTRODUCED_IN(23);
231FILE* open_memstream(char**, size_t*) __INTRODUCED_IN(23);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800232
Elliott Hughes9c8d7112016-06-13 13:23:42 -0700233#if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700234int asprintf(char** __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3);
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700235char* fgetln(FILE* __restrict, size_t* __restrict);
236int fpurge(FILE*);
237void setbuffer(FILE*, char*, int);
238int setlinebuf(FILE*);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700239int vasprintf(char** __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0);
Josh Gao14adff12016-04-29 12:00:55 -0700240void clearerr_unlocked(FILE*) __INTRODUCED_IN(23);
241int feof_unlocked(FILE*) __INTRODUCED_IN(23);
242int ferror_unlocked(FILE*) __INTRODUCED_IN(23);
243int fileno_unlocked(FILE*) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800244#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
245#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700246#endif /* __USE_BSD */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800247
Elliott Hughes5470c182016-07-22 11:36:17 -0700248char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17);
249char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
Dan Albert658727e2014-10-07 11:10:36 -0700250__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 Kralevichcffdf662012-06-11 15:50:57 -0700252
Elliott Hughes5470c182016-07-22 11:36:17 -0700253size_t __fread_chk(void* __restrict, size_t, size_t, FILE* __restrict, size_t)
Josh Gao14adff12016-04-29 12:00:55 -0700254 __INTRODUCED_IN(24);
Elliott Hughes5470c182016-07-22 11:36:17 -0700255size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
Daniel Micayfed26592015-07-18 13:55:51 -0400256__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 Hughes5470c182016-07-22 11:36:17 -0700259size_t __fwrite_chk(const void* __restrict, size_t, size_t, FILE* __restrict, size_t)
Josh Gao14adff12016-04-29 12:00:55 -0700260 __INTRODUCED_IN(24);
Elliott Hughes5470c182016-07-22 11:36:17 -0700261size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
Daniel Micayfed26592015-07-18 13:55:51 -0400262__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 Hughes53cf3482016-08-09 13:06:41 -0700265#if defined(__BIONIC_FORTIFY) && !defined(__BIONIC_NO_STDIO_FORTIFY)
Elliott Hughesce45fea2012-10-22 16:10:27 -0700266
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700267#if __ANDROID_API__ >= 17
Nick Kralevichcffdf662012-06-11 15:50:57 -0700268__BIONIC_FORTIFY_INLINE
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700269__printflike(3, 0) int vsnprintf(char* dest, size_t size, const char* _Nonnull format, __va_list ap) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700270 return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
Nick Kralevichcffdf662012-06-11 15:50:57 -0700271}
272
Nick Kralevich9b549c32012-06-12 15:59:04 -0700273__BIONIC_FORTIFY_INLINE
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700274__printflike(2, 0) int vsprintf(char* dest, const char* _Nonnull format, __va_list ap) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700275 return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
Nick Kralevich9b549c32012-06-12 15:59:04 -0700276}
277
Nick Kralevich621b19d2013-06-25 10:02:35 -0700278#if defined(__clang__)
Nick Kralevich7eb28b52014-03-18 17:03:38 -0700279 #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 Kralevich621b19d2013-06-25 10:02:35 -0700283#else
Nick Kralevichcffdf662012-06-11 15:50:57 -0700284__BIONIC_FORTIFY_INLINE
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700285__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 Kralevich9b549c32012-06-12 15:59:04 -0700287}
Nick Kralevich621b19d2013-06-25 10:02:35 -0700288#endif
Nick Kralevich9b549c32012-06-12 15:59:04 -0700289
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700290#if defined(__clang__)
Nick Kralevich7eb28b52014-03-18 17:03:38 -0700291 #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 Kralevichc6eb9852013-06-24 11:44:00 -0700295#else
Nick Kralevich9b549c32012-06-12 15:59:04 -0700296__BIONIC_FORTIFY_INLINE
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700297__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 Kralevichcffdf662012-06-11 15:50:57 -0700299}
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700300#endif
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700301#endif /* __ANDROID_API__ >= 17 */
Nick Kralevichcffdf662012-06-11 15:50:57 -0700302
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700303#if __ANDROID_API__ >= 24
Daniel Micayfed26592015-07-18 13:55:51 -0400304__BIONIC_FORTIFY_INLINE
305size_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
331size_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 Albertdfa6bbb2016-08-02 15:08:32 -0700355#endif /* __ANDROID_API__ >= 24 */
Daniel Micayfed26592015-07-18 13:55:51 -0400356
Elliott Hughescd0609f2013-12-19 12:21:07 -0800357#if !defined(__clang__)
Nick Kralevich965dbc62012-07-03 11:45:31 -0700358
359__BIONIC_FORTIFY_INLINE
Elliott Hughescd0609f2013-12-19 12:21:07 -0800360char *fgets(char* dest, int size, FILE* stream) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700361 size_t bos = __bos(dest);
Nick Kralevich965dbc62012-07-03 11:45:31 -0700362
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 Kralevich9b6cc222012-07-13 14:46:36 -0700370 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700371 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 Hughes41b31792013-01-28 10:36:31 -0800376 if (__builtin_constant_p(size) && (size <= (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700377 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 Hughes41b31792013-01-28 10:36:31 -0800382 if (__builtin_constant_p(size) && (size > (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700383 __fgets_too_big_error();
384 }
385
386 return __fgets_chk(dest, size, stream, bos);
387}
388
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700389#endif /* !defined(__clang__) */
390
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700391#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevichcffdf662012-06-11 15:50:57 -0700392
Elliott Hughes5470c182016-07-22 11:36:17 -0700393#if defined(__clang__)
394#pragma clang diagnostic pop
395#endif
396
Dan Albert658727e2014-10-07 11:10:36 -0700397__END_DECLS
398
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800399#endif /* _STDIO_H_ */