blob: ef660d8e885b752da62eba9d08e3a03ab33ed12d [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
Josh Gao14adff12016-04-29 12:00:55 -070058extern FILE* stdin __INTRODUCED_IN(23);
59extern FILE* stdout __INTRODUCED_IN(23);
60extern FILE* stderr __INTRODUCED_IN(23);
Elliott Hughes168667c2014-11-14 14:42:59 -080061/* 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 Project1dc9e472009-03-03 19:28:35 -080065
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066/*
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 Project1dc9e472009-03-03 19:28:35 -080080#define EOF (-1)
81
82/*
Elliott Hughesd04c1832013-05-14 16:08:43 -070083 * 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 Project1dc9e472009-03-03 19:28:35 -080086 */
Elliott Hughesd04c1832013-05-14 16:08:43 -070087
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088#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 Project1dc9e472009-03-03 19:28:35 -080091#define L_tmpnam 1024 /* XXX must be == PATH_MAX */
92#define TMP_MAX 308915776
93
Elliott Hughes1ed337d2015-02-02 14:02:09 -080094#define SEEK_SET 0
95#define SEEK_CUR 1
96#define SEEK_END 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098/*
99 * Functions defined in ANSI C standard.
100 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101void clearerr(FILE *);
102int fclose(FILE *);
103int feof(FILE *);
104int ferror(FILE *);
105int fflush(FILE *);
106int fgetc(FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700107char *fgets(char * __restrict, int, FILE * __restrict);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700108int fprintf(FILE * __restrict , const char * __restrict, ...)
109 __printflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110int fputc(int, FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700111int fputs(const char * __restrict, FILE * __restrict);
112size_t fread(void * __restrict, size_t, size_t, FILE * __restrict);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700113int fscanf(FILE * __restrict, const char * __restrict, ...)
114 __scanflike(2, 3);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700115size_t fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116int getc(FILE *);
117int getchar(void);
Josh Gao46b44162016-05-27 11:14:16 -0700118ssize_t getdelim(char** __restrict, size_t* __restrict, int, FILE* __restrict) __INTRODUCED_IN(18);
119ssize_t getline(char** __restrict, size_t* __restrict, FILE* __restrict) __INTRODUCED_IN(18);
Elliott Hughesc13fb752013-12-17 20:43:30 -0800120
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121void perror(const char *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700122int printf(const char * __restrict, ...)
123 __printflike(1, 2);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124int putc(int, FILE *);
125int putchar(int);
126int puts(const char *);
127int remove(const char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128void rewind(FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700129int scanf(const char * __restrict, ...)
130 __scanflike(1, 2);
131void setbuf(FILE * __restrict, char * __restrict);
132int setvbuf(FILE * __restrict, char * __restrict, int, size_t);
133int sscanf(const char * __restrict, const char * __restrict, ...)
134 __scanflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135int ungetc(int, FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700136int vfprintf(FILE * __restrict, const char * __restrict, __va_list)
137 __printflike(2, 0);
138int vprintf(const char * __restrict, __va_list)
139 __printflike(1, 0);
140
Josh Gao14adff12016-04-29 12:00:55 -0700141int dprintf(int, const char* __restrict, ...) __printflike(2, 3) __INTRODUCED_IN(21);
142int vdprintf(int, const char* __restrict, __va_list) __printflike(2, 0) __INTRODUCED_IN(21);
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700143
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700144#if __STDC_VERSION__ < 201112L
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800145char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead")));
Dan Albert96350462014-06-17 23:31:21 +0000146#endif
Elliott Hughesc13fb752013-12-17 20:43:30 -0800147int sprintf(char* __restrict, const char* __restrict, ...)
148 __printflike(2, 3) __warnattr("sprintf is often misused; please use snprintf");
Elliott Hughesc13fb752013-12-17 20:43:30 -0800149int vsprintf(char* __restrict, const char* __restrict, __va_list)
150 __printflike(2, 0) __warnattr("vsprintf is often misused; please use vsnprintf");
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800151char* tmpnam(char*) __attribute__((deprecated("tmpnam is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700152#if defined(__USE_BSD) || defined(__USE_GNU)
153#define P_tmpdir "/tmp/" /* deprecated */
Elliott Hughesc13fb752013-12-17 20:43:30 -0800154char* tempnam(const char*, const char*)
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800155 __attribute__((deprecated("tempnam is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughesc13fb752013-12-17 20:43:30 -0800156#endif
Elliott Hughesd04c1832013-05-14 16:08:43 -0700157
Elliott Hughes9677fab2016-01-25 15:50:59 -0800158int rename(const char*, const char*);
159int renameat(int, const char*, int, const char*);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700160
Elliott Hughes9677fab2016-01-25 15:50:59 -0800161int fseek(FILE*, long, int);
162long ftell(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800163
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800164#if defined(__USE_FILE_OFFSET64)
Elliott Hughes9677fab2016-01-25 15:50:59 -0800165int fgetpos(FILE*, fpos_t*) __RENAME(fgetpos64);
166int fsetpos(FILE*, const fpos_t*) __RENAME(fsetpos64);
167int fseeko(FILE*, off_t, int) __RENAME(fseeko64);
168off_t ftello(FILE*) __RENAME(ftello64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800169# if defined(__USE_BSD)
170FILE* 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 Hughes68dc20d2015-02-06 22:28:49 -0800176#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800177int fgetpos(FILE*, fpos_t*);
178int fsetpos(FILE*, const fpos_t*);
179int fseeko(FILE*, off_t, int);
180off_t ftello(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800181# if defined(__USE_BSD)
182FILE* 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 Hughes68dc20d2015-02-06 22:28:49 -0800188#endif
Josh Gao14adff12016-04-29 12:00:55 -0700189int fgetpos64(FILE*, fpos64_t*) __INTRODUCED_IN(24);
190int fsetpos64(FILE*, const fpos64_t*) __INTRODUCED_IN(24);
191int fseeko64(FILE*, off64_t, int) __INTRODUCED_IN(24);
192off64_t ftello64(FILE*) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800193#if defined(__USE_BSD)
Josh Gao14adff12016-04-29 12:00:55 -0700194FILE* 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 Hughes03e65eb2016-01-26 14:13:04 -0800196#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800197
Elliott Hughesf226ee52016-02-03 11:24:28 -0800198FILE* fopen(const char* __restrict, const char* __restrict);
Josh Gao14adff12016-04-29 12:00:55 -0700199FILE* fopen64(const char* __restrict, const char* __restrict) __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800200FILE* freopen(const char* __restrict, const char* __restrict, FILE* __restrict);
Josh Gao14adff12016-04-29 12:00:55 -0700201FILE* freopen64(const char* __restrict, const char* __restrict, FILE* __restrict)
202 __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800203FILE* tmpfile(void);
Josh Gao14adff12016-04-29 12:00:55 -0700204FILE* tmpfile64(void) __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800205
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700206int snprintf(char* __restrict, size_t, const char* __restrict, ...) __printflike(3, 4);
207int vfscanf(FILE* __restrict, const char* __restrict, __va_list) __scanflike(2, 0);
208int vscanf(const char*, __va_list) __scanflike(1, 0);
209int vsnprintf(char* __restrict, size_t, const char* __restrict, __va_list) __printflike(3, 0);
210int vsscanf(const char* __restrict, const char* __restrict, __va_list) __scanflike(2, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800211
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700212#define L_ctermid 1024 /* size for ctermid() */
Josh Gao95fa26e2016-06-10 16:33:05 -0700213char* ctermid(char*) __INTRODUCED_IN_FUTURE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700215FILE* fdopen(int, const char*);
216int fileno(FILE*);
217int pclose(FILE*);
218FILE* popen(const char*, const char*);
219void flockfile(FILE*);
220int ftrylockfile(FILE*);
221void funlockfile(FILE*);
222int getc_unlocked(FILE*);
223int getchar_unlocked(void);
224int putc_unlocked(int, FILE*);
225int putchar_unlocked(int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226
Josh Gao14adff12016-04-29 12:00:55 -0700227FILE* fmemopen(void*, size_t, const char*) __INTRODUCED_IN(23);
228FILE* open_memstream(char**, size_t*) __INTRODUCED_IN(23);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229
Elliott Hughes9c8d7112016-06-13 13:23:42 -0700230#if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700231int asprintf(char** __restrict, const char* __restrict, ...) __printflike(2, 3);
232char* fgetln(FILE* __restrict, size_t* __restrict);
233int fpurge(FILE*);
234void setbuffer(FILE*, char*, int);
235int setlinebuf(FILE*);
236int vasprintf(char** __restrict, const char* __restrict, __va_list) __printflike(2, 0);
Josh Gao14adff12016-04-29 12:00:55 -0700237void clearerr_unlocked(FILE*) __INTRODUCED_IN(23);
238int feof_unlocked(FILE*) __INTRODUCED_IN(23);
239int ferror_unlocked(FILE*) __INTRODUCED_IN(23);
240int fileno_unlocked(FILE*) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800241#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
242#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700243#endif /* __USE_BSD */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244
Josh Gao46b44162016-05-27 11:14:16 -0700245extern char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17);
Dan Albert658727e2014-10-07 11:10:36 -0700246extern char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
247__errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
248__errordecl(__fgets_too_small_error, "fgets called with size less than zero");
Nick Kralevichcffdf662012-06-11 15:50:57 -0700249
Josh Gao14adff12016-04-29 12:00:55 -0700250extern size_t __fread_chk(void* __restrict, size_t, size_t, FILE* __restrict, size_t)
251 __INTRODUCED_IN(24);
Daniel Micayfed26592015-07-18 13:55:51 -0400252extern size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
253__errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
254__errordecl(__fread_overflow, "fread called with overflowing size * count");
255
Josh Gao14adff12016-04-29 12:00:55 -0700256extern size_t __fwrite_chk(const void* __restrict, size_t, size_t, FILE* __restrict, size_t)
257 __INTRODUCED_IN(24);
Daniel Micayfed26592015-07-18 13:55:51 -0400258extern size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
259__errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
260__errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
261
Dan Albert658727e2014-10-07 11:10:36 -0700262#if defined(__BIONIC_FORTIFY)
Elliott Hughesce45fea2012-10-22 16:10:27 -0700263
Nick Kralevichcffdf662012-06-11 15:50:57 -0700264__BIONIC_FORTIFY_INLINE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700265__printflike(3, 0)
Nick Kralevich9b549c32012-06-12 15:59:04 -0700266int vsnprintf(char *dest, size_t size, const char *format, __va_list ap)
Nick Kralevichcffdf662012-06-11 15:50:57 -0700267{
Nick Kralevich9020fd52013-04-30 11:31:35 -0700268 return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
Nick Kralevichcffdf662012-06-11 15:50:57 -0700269}
270
Nick Kralevich9b549c32012-06-12 15:59:04 -0700271__BIONIC_FORTIFY_INLINE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700272__printflike(2, 0)
Nick Kralevich9b549c32012-06-12 15:59:04 -0700273int vsprintf(char *dest, const char *format, __va_list ap)
274{
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 Hughesd04c1832013-05-14 16:08:43 -0700285__printflike(3, 4)
Nick Kralevich621b19d2013-06-25 10:02:35 -0700286int snprintf(char *dest, size_t size, const char *format, ...)
Nick Kralevichcffdf662012-06-11 15:50:57 -0700287{
Nick Kralevich621b19d2013-06-25 10:02:35 -0700288 return __builtin___snprintf_chk(dest, size, 0,
289 __bos(dest), format, __builtin_va_arg_pack());
Nick Kralevich9b549c32012-06-12 15:59:04 -0700290}
Nick Kralevich621b19d2013-06-25 10:02:35 -0700291#endif
Nick Kralevich9b549c32012-06-12 15:59:04 -0700292
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700293#if defined(__clang__)
Nick Kralevich7eb28b52014-03-18 17:03:38 -0700294 #if !defined(sprintf)
295 #define __wrap_sprintf(dest, ...) __builtin___sprintf_chk(dest, 0, __bos(dest), __VA_ARGS__)
296 #define sprintf(...) __wrap_sprintf(__VA_ARGS__)
297 #endif
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700298#else
Nick Kralevich9b549c32012-06-12 15:59:04 -0700299__BIONIC_FORTIFY_INLINE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700300__printflike(2, 3)
Nick Kralevich9b549c32012-06-12 15:59:04 -0700301int sprintf(char *dest, const char *format, ...)
302{
303 return __builtin___sprintf_chk(dest, 0,
Nick Kralevich78d6d982013-04-29 16:29:37 -0700304 __bos(dest), format, __builtin_va_arg_pack());
Nick Kralevichcffdf662012-06-11 15:50:57 -0700305}
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700306#endif
Nick Kralevichcffdf662012-06-11 15:50:57 -0700307
Daniel Micayfed26592015-07-18 13:55:51 -0400308__BIONIC_FORTIFY_INLINE
309size_t fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
310 size_t bos = __bos0(buf);
311
312#if !defined(__clang__)
313 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
314 return __fread_real(buf, size, count, stream);
315 }
316
317 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
318 size_t total;
319 if (__size_mul_overflow(size, count, &total)) {
320 __fread_overflow();
321 }
322
323 if (total > bos) {
324 __fread_too_big_error();
325 }
326
327 return __fread_real(buf, size, count, stream);
328 }
329#endif
330
331 return __fread_chk(buf, size, count, stream, bos);
332}
333
334__BIONIC_FORTIFY_INLINE
335size_t fwrite(const void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
336 size_t bos = __bos0(buf);
337
338#if !defined(__clang__)
339 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
340 return __fwrite_real(buf, size, count, stream);
341 }
342
343 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
344 size_t total;
345 if (__size_mul_overflow(size, count, &total)) {
346 __fwrite_overflow();
347 }
348
349 if (total > bos) {
350 __fwrite_too_big_error();
351 }
352
353 return __fwrite_real(buf, size, count, stream);
354 }
355#endif
356
357 return __fwrite_chk(buf, size, count, stream, bos);
358}
359
Elliott Hughescd0609f2013-12-19 12:21:07 -0800360#if !defined(__clang__)
Nick Kralevich965dbc62012-07-03 11:45:31 -0700361
362__BIONIC_FORTIFY_INLINE
Elliott Hughescd0609f2013-12-19 12:21:07 -0800363char *fgets(char* dest, int size, FILE* stream) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700364 size_t bos = __bos(dest);
Nick Kralevich965dbc62012-07-03 11:45:31 -0700365
366 // Compiler can prove, at compile time, that the passed in size
367 // is always negative. Force a compiler error.
368 if (__builtin_constant_p(size) && (size < 0)) {
369 __fgets_too_small_error();
370 }
371
372 // Compiler doesn't know destination size. Don't call __fgets_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700373 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700374 return __fgets_real(dest, size, stream);
375 }
376
377 // Compiler can prove, at compile time, that the passed in size
378 // is always <= the actual object size. Don't call __fgets_chk
Elliott Hughes41b31792013-01-28 10:36:31 -0800379 if (__builtin_constant_p(size) && (size <= (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700380 return __fgets_real(dest, size, stream);
381 }
382
383 // Compiler can prove, at compile time, that the passed in size
384 // is always > the actual object size. Force a compiler error.
Elliott Hughes41b31792013-01-28 10:36:31 -0800385 if (__builtin_constant_p(size) && (size > (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700386 __fgets_too_big_error();
387 }
388
389 return __fgets_chk(dest, size, stream, bos);
390}
391
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700392#endif /* !defined(__clang__) */
393
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700394#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevichcffdf662012-06-11 15:50:57 -0700395
Dan Albert658727e2014-10-07 11:10:36 -0700396__END_DECLS
397
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398#endif /* _STDIO_H_ */