blob: 621caa84ea56c1f7b7bb4889439d408819647ac6 [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
Dan Albert658727e2014-10-07 11:10:36 -070050__BEGIN_DECLS
51
Elliott Hughes9677fab2016-01-25 15:50:59 -080052typedef off_t fpos_t;
53typedef off64_t fpos64_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054
Elliott Hughesf0141df2015-10-12 12:44:23 -070055struct __sFILE;
56typedef struct __sFILE FILE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057
Dan Albert32c79c22016-07-15 11:32:23 -070058#if __ANDROID_API__ >= 23
Josh Gao14adff12016-04-29 12:00:55 -070059extern FILE* stdin __INTRODUCED_IN(23);
60extern FILE* stdout __INTRODUCED_IN(23);
61extern FILE* stderr __INTRODUCED_IN(23);
Dan Albert32c79c22016-07-15 11:32:23 -070062
Elliott Hughes168667c2014-11-14 14:42:59 -080063/* C99 and earlier plus current C++ standards say these must be macros. */
64#define stdin stdin
65#define stdout stdout
66#define stderr stderr
Dan Albert32c79c22016-07-15 11:32:23 -070067#else
68/* Before M the actual symbols for stdin and friends had different names. */
69extern FILE* __sF[] __REMOVED_IN(23);
70
71#define stdin __sF[0]
72#define stdout __sF[1]
73#define stderr __sF[2]
74#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076/*
77 * The following three definitions are for ANSI C, which took them
78 * from System V, which brilliantly took internal interface macros and
79 * made them official arguments to setvbuf(), without renaming them.
80 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
81 *
82 * Although numbered as their counterparts above, the implementation
83 * does not rely on this.
84 */
85#define _IOFBF 0 /* setvbuf should set fully buffered */
86#define _IOLBF 1 /* setvbuf should set line buffered */
87#define _IONBF 2 /* setvbuf should set unbuffered */
88
89#define BUFSIZ 1024 /* size of buffer used by setbuf */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090#define EOF (-1)
91
92/*
Elliott Hughesd04c1832013-05-14 16:08:43 -070093 * FOPEN_MAX is a minimum maximum, and is the number of streams that
94 * stdio can provide without attempting to allocate further resources
95 * (which could fail). Do not use this for anything.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096 */
Elliott Hughesd04c1832013-05-14 16:08:43 -070097
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098#define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */
99#define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */
100
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101#define L_tmpnam 1024 /* XXX must be == PATH_MAX */
102#define TMP_MAX 308915776
103
Elliott Hughes1ed337d2015-02-02 14:02:09 -0800104#define SEEK_SET 0
105#define SEEK_CUR 1
106#define SEEK_END 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108/*
109 * Functions defined in ANSI C standard.
110 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111void clearerr(FILE *);
112int fclose(FILE *);
113int feof(FILE *);
114int ferror(FILE *);
115int fflush(FILE *);
116int fgetc(FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700117char *fgets(char * __restrict, int, FILE * __restrict);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700118int fprintf(FILE * __restrict , const char * __restrict _Nonnull, ...) __printflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119int fputc(int, FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700120int fputs(const char * __restrict, FILE * __restrict);
121size_t fread(void * __restrict, size_t, size_t, FILE * __restrict);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700122int fscanf(FILE * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700123size_t fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124int getc(FILE *);
125int getchar(void);
Josh Gao46b44162016-05-27 11:14:16 -0700126ssize_t getdelim(char** __restrict, size_t* __restrict, int, FILE* __restrict) __INTRODUCED_IN(18);
127ssize_t getline(char** __restrict, size_t* __restrict, FILE* __restrict) __INTRODUCED_IN(18);
Elliott Hughesc13fb752013-12-17 20:43:30 -0800128
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129void perror(const char *);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700130int printf(const char * __restrict _Nonnull, ...) __printflike(1, 2);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131int putc(int, FILE *);
132int putchar(int);
133int puts(const char *);
134int remove(const char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135void rewind(FILE *);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700136int scanf(const char * __restrict _Nonnull, ...) __scanflike(1, 2);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700137void setbuf(FILE * __restrict, char * __restrict);
138int setvbuf(FILE * __restrict, char * __restrict, int, size_t);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700139int sscanf(const char * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800140int ungetc(int, FILE *);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700141int vfprintf(FILE * __restrict, const char * __restrict _Nonnull, __va_list) __printflike(2, 0);
142int vprintf(const char * __restrict _Nonnull, __va_list) __printflike(1, 0);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700143
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700144int dprintf(int, const char* __restrict _Nonnull, ...) __printflike(2, 3) __INTRODUCED_IN(21);
145int vdprintf(int, const char* __restrict _Nonnull, __va_list) __printflike(2, 0) __INTRODUCED_IN(21);
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700146
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700147#if __STDC_VERSION__ < 201112L
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800148char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead")));
Dan Albert96350462014-06-17 23:31:21 +0000149#endif
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700150int sprintf(char* __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3) __warnattr("sprintf is often misused; please use snprintf");
151int vsprintf(char* __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0) __warnattr("vsprintf is often misused; please use vsnprintf");
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800152char* tmpnam(char*) __attribute__((deprecated("tmpnam is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700153#if defined(__USE_BSD) || defined(__USE_GNU)
154#define P_tmpdir "/tmp/" /* deprecated */
Elliott Hughesc13fb752013-12-17 20:43:30 -0800155char* tempnam(const char*, const char*)
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800156 __attribute__((deprecated("tempnam is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughesc13fb752013-12-17 20:43:30 -0800157#endif
Elliott Hughesd04c1832013-05-14 16:08:43 -0700158
Elliott Hughes9677fab2016-01-25 15:50:59 -0800159int rename(const char*, const char*);
160int renameat(int, const char*, int, const char*);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700161
Elliott Hughes9677fab2016-01-25 15:50:59 -0800162int fseek(FILE*, long, int);
163long ftell(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800164
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800165#if defined(__USE_FILE_OFFSET64)
Elliott Hughes9677fab2016-01-25 15:50:59 -0800166int fgetpos(FILE*, fpos_t*) __RENAME(fgetpos64);
167int fsetpos(FILE*, const fpos_t*) __RENAME(fsetpos64);
168int fseeko(FILE*, off_t, int) __RENAME(fseeko64);
169off_t ftello(FILE*) __RENAME(ftello64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800170# if defined(__USE_BSD)
171FILE* funopen(const void*,
172 int (*)(void*, char*, int),
173 int (*)(void*, const char*, int),
174 fpos_t (*)(void*, fpos_t, int),
175 int (*)(void*)) __RENAME(funopen64);
176# endif
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800177#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800178int fgetpos(FILE*, fpos_t*);
179int fsetpos(FILE*, const fpos_t*);
180int fseeko(FILE*, off_t, int);
181off_t ftello(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800182# if defined(__USE_BSD)
183FILE* funopen(const void*,
184 int (*)(void*, char*, int),
185 int (*)(void*, const char*, int),
186 fpos_t (*)(void*, fpos_t, int),
187 int (*)(void*));
188# endif
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800189#endif
Josh Gao14adff12016-04-29 12:00:55 -0700190int fgetpos64(FILE*, fpos64_t*) __INTRODUCED_IN(24);
191int fsetpos64(FILE*, const fpos64_t*) __INTRODUCED_IN(24);
192int fseeko64(FILE*, off64_t, int) __INTRODUCED_IN(24);
193off64_t ftello64(FILE*) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800194#if defined(__USE_BSD)
Josh Gao14adff12016-04-29 12:00:55 -0700195FILE* funopen64(const void*, int (*)(void*, char*, int), int (*)(void*, const char*, int),
196 fpos64_t (*)(void*, fpos64_t, int), int (*)(void*)) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800197#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800198
Elliott Hughesf226ee52016-02-03 11:24:28 -0800199FILE* fopen(const char* __restrict, const char* __restrict);
Josh Gao14adff12016-04-29 12:00:55 -0700200FILE* fopen64(const char* __restrict, const char* __restrict) __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800201FILE* freopen(const char* __restrict, const char* __restrict, FILE* __restrict);
Josh Gao14adff12016-04-29 12:00:55 -0700202FILE* freopen64(const char* __restrict, const char* __restrict, FILE* __restrict)
203 __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800204FILE* tmpfile(void);
Josh Gao14adff12016-04-29 12:00:55 -0700205FILE* tmpfile64(void) __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800206
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700207int snprintf(char* __restrict, size_t, const char* __restrict _Nonnull, ...) __printflike(3, 4);
208int vfscanf(FILE* __restrict, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0);
209int vscanf(const char* _Nonnull , __va_list) __scanflike(1, 0);
210int vsnprintf(char* __restrict, size_t, const char* __restrict _Nonnull, __va_list) __printflike(3, 0);
211int vsscanf(const char* __restrict _Nonnull, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700213#define L_ctermid 1024 /* size for ctermid() */
Josh Gao95fa26e2016-06-10 16:33:05 -0700214char* ctermid(char*) __INTRODUCED_IN_FUTURE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800215
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700216FILE* fdopen(int, const char*);
217int fileno(FILE*);
218int pclose(FILE*);
219FILE* popen(const char*, const char*);
220void flockfile(FILE*);
221int ftrylockfile(FILE*);
222void funlockfile(FILE*);
223int getc_unlocked(FILE*);
224int getchar_unlocked(void);
225int putc_unlocked(int, FILE*);
226int putchar_unlocked(int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227
Josh Gao14adff12016-04-29 12:00:55 -0700228FILE* fmemopen(void*, size_t, const char*) __INTRODUCED_IN(23);
229FILE* open_memstream(char**, size_t*) __INTRODUCED_IN(23);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230
Elliott Hughes9c8d7112016-06-13 13:23:42 -0700231#if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700232int asprintf(char** __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3);
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700233char* fgetln(FILE* __restrict, size_t* __restrict);
234int fpurge(FILE*);
235void setbuffer(FILE*, char*, int);
236int setlinebuf(FILE*);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700237int vasprintf(char** __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0);
Josh Gao14adff12016-04-29 12:00:55 -0700238void clearerr_unlocked(FILE*) __INTRODUCED_IN(23);
239int feof_unlocked(FILE*) __INTRODUCED_IN(23);
240int ferror_unlocked(FILE*) __INTRODUCED_IN(23);
241int fileno_unlocked(FILE*) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800242#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
243#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700244#endif /* __USE_BSD */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245
Josh Gao46b44162016-05-27 11:14:16 -0700246extern char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17);
Dan Albert658727e2014-10-07 11:10:36 -0700247extern char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
248__errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
249__errordecl(__fgets_too_small_error, "fgets called with size less than zero");
Nick Kralevichcffdf662012-06-11 15:50:57 -0700250
Josh Gao14adff12016-04-29 12:00:55 -0700251extern size_t __fread_chk(void* __restrict, size_t, size_t, FILE* __restrict, size_t)
252 __INTRODUCED_IN(24);
Daniel Micayfed26592015-07-18 13:55:51 -0400253extern size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
254__errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
255__errordecl(__fread_overflow, "fread called with overflowing size * count");
256
Josh Gao14adff12016-04-29 12:00:55 -0700257extern size_t __fwrite_chk(const void* __restrict, size_t, size_t, FILE* __restrict, size_t)
258 __INTRODUCED_IN(24);
Daniel Micayfed26592015-07-18 13:55:51 -0400259extern size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
260__errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
261__errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
262
Dan Albert658727e2014-10-07 11:10:36 -0700263#if defined(__BIONIC_FORTIFY)
Elliott Hughesce45fea2012-10-22 16:10:27 -0700264
Nick Kralevichcffdf662012-06-11 15:50:57 -0700265__BIONIC_FORTIFY_INLINE
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700266__printflike(3, 0) int vsnprintf(char* dest, size_t size, const char* _Nonnull format, __va_list ap) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700267 return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
Nick Kralevichcffdf662012-06-11 15:50:57 -0700268}
269
Nick Kralevich9b549c32012-06-12 15:59:04 -0700270__BIONIC_FORTIFY_INLINE
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700271__printflike(2, 0) int vsprintf(char* dest, const char* _Nonnull format, __va_list ap) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700272 return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
Nick Kralevich9b549c32012-06-12 15:59:04 -0700273}
274
Nick Kralevich621b19d2013-06-25 10:02:35 -0700275#if defined(__clang__)
Nick Kralevich7eb28b52014-03-18 17:03:38 -0700276 #if !defined(snprintf)
277 #define __wrap_snprintf(dest, size, ...) __builtin___snprintf_chk(dest, size, 0, __bos(dest), __VA_ARGS__)
278 #define snprintf(...) __wrap_snprintf(__VA_ARGS__)
279 #endif
Nick Kralevich621b19d2013-06-25 10:02:35 -0700280#else
Nick Kralevichcffdf662012-06-11 15:50:57 -0700281__BIONIC_FORTIFY_INLINE
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700282__printflike(3, 4) int snprintf(char* dest, size_t size, const char* _Nonnull format, ...) {
283 return __builtin___snprintf_chk(dest, size, 0, __bos(dest), format, __builtin_va_arg_pack());
Nick Kralevich9b549c32012-06-12 15:59:04 -0700284}
Nick Kralevich621b19d2013-06-25 10:02:35 -0700285#endif
Nick Kralevich9b549c32012-06-12 15:59:04 -0700286
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700287#if defined(__clang__)
Nick Kralevich7eb28b52014-03-18 17:03:38 -0700288 #if !defined(sprintf)
289 #define __wrap_sprintf(dest, ...) __builtin___sprintf_chk(dest, 0, __bos(dest), __VA_ARGS__)
290 #define sprintf(...) __wrap_sprintf(__VA_ARGS__)
291 #endif
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700292#else
Nick Kralevich9b549c32012-06-12 15:59:04 -0700293__BIONIC_FORTIFY_INLINE
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700294__printflike(2, 3) int sprintf(char* dest, const char* _Nonnull format, ...) {
295 return __builtin___sprintf_chk(dest, 0, __bos(dest), format, __builtin_va_arg_pack());
Nick Kralevichcffdf662012-06-11 15:50:57 -0700296}
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700297#endif
Nick Kralevichcffdf662012-06-11 15:50:57 -0700298
Daniel Micayfed26592015-07-18 13:55:51 -0400299__BIONIC_FORTIFY_INLINE
300size_t fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
301 size_t bos = __bos0(buf);
302
303#if !defined(__clang__)
304 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
305 return __fread_real(buf, size, count, stream);
306 }
307
308 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
309 size_t total;
310 if (__size_mul_overflow(size, count, &total)) {
311 __fread_overflow();
312 }
313
314 if (total > bos) {
315 __fread_too_big_error();
316 }
317
318 return __fread_real(buf, size, count, stream);
319 }
320#endif
321
322 return __fread_chk(buf, size, count, stream, bos);
323}
324
325__BIONIC_FORTIFY_INLINE
326size_t fwrite(const void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
327 size_t bos = __bos0(buf);
328
329#if !defined(__clang__)
330 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
331 return __fwrite_real(buf, size, count, stream);
332 }
333
334 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
335 size_t total;
336 if (__size_mul_overflow(size, count, &total)) {
337 __fwrite_overflow();
338 }
339
340 if (total > bos) {
341 __fwrite_too_big_error();
342 }
343
344 return __fwrite_real(buf, size, count, stream);
345 }
346#endif
347
348 return __fwrite_chk(buf, size, count, stream, bos);
349}
350
Elliott Hughescd0609f2013-12-19 12:21:07 -0800351#if !defined(__clang__)
Nick Kralevich965dbc62012-07-03 11:45:31 -0700352
353__BIONIC_FORTIFY_INLINE
Elliott Hughescd0609f2013-12-19 12:21:07 -0800354char *fgets(char* dest, int size, FILE* stream) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700355 size_t bos = __bos(dest);
Nick Kralevich965dbc62012-07-03 11:45:31 -0700356
357 // Compiler can prove, at compile time, that the passed in size
358 // is always negative. Force a compiler error.
359 if (__builtin_constant_p(size) && (size < 0)) {
360 __fgets_too_small_error();
361 }
362
363 // Compiler doesn't know destination size. Don't call __fgets_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700364 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700365 return __fgets_real(dest, size, stream);
366 }
367
368 // Compiler can prove, at compile time, that the passed in size
369 // is always <= the actual object size. Don't call __fgets_chk
Elliott Hughes41b31792013-01-28 10:36:31 -0800370 if (__builtin_constant_p(size) && (size <= (int) bos)) {
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. Force a compiler error.
Elliott Hughes41b31792013-01-28 10:36:31 -0800376 if (__builtin_constant_p(size) && (size > (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700377 __fgets_too_big_error();
378 }
379
380 return __fgets_chk(dest, size, stream, bos);
381}
382
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700383#endif /* !defined(__clang__) */
384
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700385#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevichcffdf662012-06-11 15:50:57 -0700386
Dan Albert658727e2014-10-07 11:10:36 -0700387__END_DECLS
388
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800389#endif /* _STDIO_H_ */