blob: 9125e4c32cc3b73dfcee75bba71ac92f70baa5c0 [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
Elliott Hughes5bc78c82016-11-16 11:35:43 -080052#if __ANDROID_API__ < __ANDROID_API_N__
Dan Albert3037ea42016-10-06 15:46:45 -070053#include <bits/struct_file.h>
54#endif
55
Dan Albert658727e2014-10-07 11:10:36 -070056__BEGIN_DECLS
57
Elliott Hughes5470c182016-07-22 11:36:17 -070058#if defined(__clang__)
59#pragma clang diagnostic push
60#pragma clang diagnostic ignored "-Wnullability-completeness"
61#endif
62
Elliott Hughes9677fab2016-01-25 15:50:59 -080063typedef off_t fpos_t;
64typedef off64_t fpos64_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065
Elliott Hughesf0141df2015-10-12 12:44:23 -070066struct __sFILE;
67typedef struct __sFILE FILE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068
Elliott Hughes5bc78c82016-11-16 11:35:43 -080069#if __ANDROID_API__ >= __ANDROID_API_M__
Josh Gao14adff12016-04-29 12:00:55 -070070extern FILE* stdin __INTRODUCED_IN(23);
71extern FILE* stdout __INTRODUCED_IN(23);
72extern FILE* stderr __INTRODUCED_IN(23);
Dan Albert32c79c22016-07-15 11:32:23 -070073
Elliott Hughes168667c2014-11-14 14:42:59 -080074/* C99 and earlier plus current C++ standards say these must be macros. */
75#define stdin stdin
76#define stdout stdout
77#define stderr stderr
Dan Albert32c79c22016-07-15 11:32:23 -070078#else
79/* Before M the actual symbols for stdin and friends had different names. */
Dan Albert3037ea42016-10-06 15:46:45 -070080extern FILE __sF[] __REMOVED_IN(23);
Dan Albert32c79c22016-07-15 11:32:23 -070081
Dan Albert3037ea42016-10-06 15:46:45 -070082#define stdin (&__sF[0])
83#define stdout (&__sF[1])
84#define stderr (&__sF[2])
Dan Albert32c79c22016-07-15 11:32:23 -070085#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087/*
88 * The following three definitions are for ANSI C, which took them
89 * from System V, which brilliantly took internal interface macros and
90 * made them official arguments to setvbuf(), without renaming them.
91 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
92 *
93 * Although numbered as their counterparts above, the implementation
94 * does not rely on this.
95 */
96#define _IOFBF 0 /* setvbuf should set fully buffered */
97#define _IOLBF 1 /* setvbuf should set line buffered */
98#define _IONBF 2 /* setvbuf should set unbuffered */
99
100#define BUFSIZ 1024 /* size of buffer used by setbuf */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101#define EOF (-1)
102
103/*
Elliott Hughesd04c1832013-05-14 16:08:43 -0700104 * FOPEN_MAX is a minimum maximum, and is the number of streams that
105 * stdio can provide without attempting to allocate further resources
106 * (which could fail). Do not use this for anything.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107 */
Elliott Hughesd04c1832013-05-14 16:08:43 -0700108
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109#define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */
110#define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */
111
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112#define L_tmpnam 1024 /* XXX must be == PATH_MAX */
113#define TMP_MAX 308915776
114
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115/*
116 * Functions defined in ANSI C standard.
117 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118void clearerr(FILE *);
119int fclose(FILE *);
120int feof(FILE *);
121int ferror(FILE *);
122int fflush(FILE *);
123int fgetc(FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700124char *fgets(char * __restrict, int, FILE * __restrict);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700125int fprintf(FILE * __restrict , const char * __restrict _Nonnull, ...) __printflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126int fputc(int, FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700127int fputs(const char * __restrict, FILE * __restrict);
128size_t fread(void * __restrict, size_t, size_t, FILE * __restrict);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700129int fscanf(FILE * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700130size_t fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131int getc(FILE *);
132int getchar(void);
Josh Gao46b44162016-05-27 11:14:16 -0700133ssize_t getdelim(char** __restrict, size_t* __restrict, int, FILE* __restrict) __INTRODUCED_IN(18);
134ssize_t getline(char** __restrict, size_t* __restrict, FILE* __restrict) __INTRODUCED_IN(18);
Elliott Hughesc13fb752013-12-17 20:43:30 -0800135
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136void perror(const char *);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700137int printf(const char * __restrict _Nonnull, ...) __printflike(1, 2);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138int putc(int, FILE *);
139int putchar(int);
140int puts(const char *);
141int remove(const char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142void rewind(FILE *);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700143int scanf(const char * __restrict _Nonnull, ...) __scanflike(1, 2);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700144void setbuf(FILE * __restrict, char * __restrict);
145int setvbuf(FILE * __restrict, char * __restrict, int, size_t);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700146int sscanf(const char * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800147int ungetc(int, FILE *);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700148int vfprintf(FILE * __restrict, const char * __restrict _Nonnull, __va_list) __printflike(2, 0);
149int vprintf(const char * __restrict _Nonnull, __va_list) __printflike(1, 0);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700150
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700151int dprintf(int, const char* __restrict _Nonnull, ...) __printflike(2, 3) __INTRODUCED_IN(21);
152int vdprintf(int, const char* __restrict _Nonnull, __va_list) __printflike(2, 0) __INTRODUCED_IN(21);
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700153
Elliott Hughesf6495c72016-07-25 09:20:57 -0700154#if (defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L) || \
155 (defined(__cplusplus) && __cplusplus <= 201103L)
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800156char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead")));
Dan Albert96350462014-06-17 23:31:21 +0000157#endif
Elliott Hughes5470c182016-07-22 11:36:17 -0700158int sprintf(char* __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3);
159int vsprintf(char* __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0);
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800160char* tmpnam(char*) __attribute__((deprecated("tmpnam is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700161#define P_tmpdir "/tmp/" /* deprecated */
Elliott Hughesc13fb752013-12-17 20:43:30 -0800162char* tempnam(const char*, const char*)
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800163 __attribute__((deprecated("tempnam is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughesd04c1832013-05-14 16:08:43 -0700164
Elliott Hughes9677fab2016-01-25 15:50:59 -0800165int rename(const char*, const char*);
166int renameat(int, const char*, int, const char*);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700167
Elliott Hughes9677fab2016-01-25 15:50:59 -0800168int fseek(FILE*, long, int);
169long ftell(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800170
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800171#if defined(__USE_FILE_OFFSET64)
Elliott Hughes9677fab2016-01-25 15:50:59 -0800172int fgetpos(FILE*, fpos_t*) __RENAME(fgetpos64);
173int fsetpos(FILE*, const fpos_t*) __RENAME(fsetpos64);
174int fseeko(FILE*, off_t, int) __RENAME(fseeko64);
175off_t ftello(FILE*) __RENAME(ftello64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800176# if defined(__USE_BSD)
177FILE* funopen(const void*,
178 int (*)(void*, char*, int),
179 int (*)(void*, const char*, int),
180 fpos_t (*)(void*, fpos_t, int),
181 int (*)(void*)) __RENAME(funopen64);
182# endif
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800183#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800184int fgetpos(FILE*, fpos_t*);
185int fsetpos(FILE*, const fpos_t*);
186int fseeko(FILE*, off_t, int);
187off_t ftello(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800188# if defined(__USE_BSD)
189FILE* funopen(const void*,
190 int (*)(void*, char*, int),
191 int (*)(void*, const char*, int),
192 fpos_t (*)(void*, fpos_t, int),
193 int (*)(void*));
194# endif
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800195#endif
Josh Gao14adff12016-04-29 12:00:55 -0700196int fgetpos64(FILE*, fpos64_t*) __INTRODUCED_IN(24);
197int fsetpos64(FILE*, const fpos64_t*) __INTRODUCED_IN(24);
198int fseeko64(FILE*, off64_t, int) __INTRODUCED_IN(24);
199off64_t ftello64(FILE*) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800200#if defined(__USE_BSD)
Josh Gao14adff12016-04-29 12:00:55 -0700201FILE* funopen64(const void*, int (*)(void*, char*, int), int (*)(void*, const char*, int),
202 fpos64_t (*)(void*, fpos64_t, int), int (*)(void*)) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800203#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204
Elliott Hughesf226ee52016-02-03 11:24:28 -0800205FILE* fopen(const char* __restrict, const char* __restrict);
Josh Gao14adff12016-04-29 12:00:55 -0700206FILE* fopen64(const char* __restrict, const char* __restrict) __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800207FILE* freopen(const char* __restrict, const char* __restrict, FILE* __restrict);
Josh Gao14adff12016-04-29 12:00:55 -0700208FILE* freopen64(const char* __restrict, const char* __restrict, FILE* __restrict)
209 __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800210FILE* tmpfile(void);
Josh Gao14adff12016-04-29 12:00:55 -0700211FILE* tmpfile64(void) __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800212
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700213int snprintf(char* __restrict, size_t, const char* __restrict _Nonnull, ...) __printflike(3, 4);
214int vfscanf(FILE* __restrict, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0);
215int vscanf(const char* _Nonnull , __va_list) __scanflike(1, 0);
216int vsnprintf(char* __restrict, size_t, const char* __restrict _Nonnull, __va_list) __printflike(3, 0);
217int vsscanf(const char* __restrict _Nonnull, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700219#define L_ctermid 1024 /* size for ctermid() */
Josh Gao95fa26e2016-06-10 16:33:05 -0700220char* ctermid(char*) __INTRODUCED_IN_FUTURE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700222FILE* fdopen(int, const char*);
223int fileno(FILE*);
224int pclose(FILE*);
225FILE* popen(const char*, const char*);
226void flockfile(FILE*);
227int ftrylockfile(FILE*);
228void funlockfile(FILE*);
229int getc_unlocked(FILE*);
230int getchar_unlocked(void);
231int putc_unlocked(int, FILE*);
232int putchar_unlocked(int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233
Josh Gao14adff12016-04-29 12:00:55 -0700234FILE* fmemopen(void*, size_t, const char*) __INTRODUCED_IN(23);
235FILE* open_memstream(char**, size_t*) __INTRODUCED_IN(23);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800236
Elliott Hughes9c8d7112016-06-13 13:23:42 -0700237#if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700238int asprintf(char** __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3);
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700239char* fgetln(FILE* __restrict, size_t* __restrict);
240int fpurge(FILE*);
241void setbuffer(FILE*, char*, int);
242int setlinebuf(FILE*);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700243int vasprintf(char** __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0);
Josh Gao14adff12016-04-29 12:00:55 -0700244void clearerr_unlocked(FILE*) __INTRODUCED_IN(23);
245int feof_unlocked(FILE*) __INTRODUCED_IN(23);
246int ferror_unlocked(FILE*) __INTRODUCED_IN(23);
247int fileno_unlocked(FILE*) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800248#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
249#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700250#endif /* __USE_BSD */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800251
Elliott Hughes5470c182016-07-22 11:36:17 -0700252char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17);
253char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
Dan Albert658727e2014-10-07 11:10:36 -0700254__errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
255__errordecl(__fgets_too_small_error, "fgets called with size less than zero");
Nick Kralevichcffdf662012-06-11 15:50:57 -0700256
Elliott Hughes5470c182016-07-22 11:36:17 -0700257size_t __fread_chk(void* __restrict, size_t, size_t, FILE* __restrict, size_t)
Josh Gao14adff12016-04-29 12:00:55 -0700258 __INTRODUCED_IN(24);
Elliott Hughes5470c182016-07-22 11:36:17 -0700259size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
Daniel Micayfed26592015-07-18 13:55:51 -0400260__errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
261__errordecl(__fread_overflow, "fread called with overflowing size * count");
262
Elliott Hughes5470c182016-07-22 11:36:17 -0700263size_t __fwrite_chk(const void* __restrict, size_t, size_t, FILE* __restrict, size_t)
Josh Gao14adff12016-04-29 12:00:55 -0700264 __INTRODUCED_IN(24);
Elliott Hughes5470c182016-07-22 11:36:17 -0700265size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
Daniel Micayfed26592015-07-18 13:55:51 -0400266__errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
267__errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
268
Elliott Hughes53cf3482016-08-09 13:06:41 -0700269#if defined(__BIONIC_FORTIFY) && !defined(__BIONIC_NO_STDIO_FORTIFY)
Elliott Hughesce45fea2012-10-22 16:10:27 -0700270
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800271#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
Nick Kralevichcffdf662012-06-11 15:50:57 -0700272__BIONIC_FORTIFY_INLINE
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700273__printflike(3, 0) int vsnprintf(char* dest, size_t size, const char* _Nonnull format, __va_list ap) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700274 return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
Nick Kralevichcffdf662012-06-11 15:50:57 -0700275}
276
Nick Kralevich9b549c32012-06-12 15:59:04 -0700277__BIONIC_FORTIFY_INLINE
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700278__printflike(2, 0) int vsprintf(char* dest, const char* _Nonnull format, __va_list ap) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700279 return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
Nick Kralevich9b549c32012-06-12 15:59:04 -0700280}
281
Nick Kralevich621b19d2013-06-25 10:02:35 -0700282#if defined(__clang__)
Nick Kralevich7eb28b52014-03-18 17:03:38 -0700283 #if !defined(snprintf)
284 #define __wrap_snprintf(dest, size, ...) __builtin___snprintf_chk(dest, size, 0, __bos(dest), __VA_ARGS__)
285 #define snprintf(...) __wrap_snprintf(__VA_ARGS__)
286 #endif
Nick Kralevich621b19d2013-06-25 10:02:35 -0700287#else
Nick Kralevichcffdf662012-06-11 15:50:57 -0700288__BIONIC_FORTIFY_INLINE
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700289__printflike(3, 4) int snprintf(char* dest, size_t size, const char* _Nonnull format, ...) {
290 return __builtin___snprintf_chk(dest, size, 0, __bos(dest), format, __builtin_va_arg_pack());
Nick Kralevich9b549c32012-06-12 15:59:04 -0700291}
Nick Kralevich621b19d2013-06-25 10:02:35 -0700292#endif
Nick Kralevich9b549c32012-06-12 15:59:04 -0700293
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700294#if defined(__clang__)
Nick Kralevich7eb28b52014-03-18 17:03:38 -0700295 #if !defined(sprintf)
296 #define __wrap_sprintf(dest, ...) __builtin___sprintf_chk(dest, 0, __bos(dest), __VA_ARGS__)
297 #define sprintf(...) __wrap_sprintf(__VA_ARGS__)
298 #endif
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700299#else
Nick Kralevich9b549c32012-06-12 15:59:04 -0700300__BIONIC_FORTIFY_INLINE
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700301__printflike(2, 3) int sprintf(char* dest, const char* _Nonnull format, ...) {
302 return __builtin___sprintf_chk(dest, 0, __bos(dest), format, __builtin_va_arg_pack());
Nick Kralevichcffdf662012-06-11 15:50:57 -0700303}
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700304#endif
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800305#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
Nick Kralevichcffdf662012-06-11 15:50:57 -0700306
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800307#if __ANDROID_API__ >= __ANDROID_API_N__
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}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800359#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
Daniel Micayfed26592015-07-18 13:55:51 -0400360
Elliott Hughescd0609f2013-12-19 12:21:07 -0800361#if !defined(__clang__)
Nick Kralevich965dbc62012-07-03 11:45:31 -0700362
363__BIONIC_FORTIFY_INLINE
Elliott Hughescd0609f2013-12-19 12:21:07 -0800364char *fgets(char* dest, int size, FILE* stream) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700365 size_t bos = __bos(dest);
Nick Kralevich965dbc62012-07-03 11:45:31 -0700366
367 // Compiler can prove, at compile time, that the passed in size
368 // is always negative. Force a compiler error.
369 if (__builtin_constant_p(size) && (size < 0)) {
370 __fgets_too_small_error();
371 }
372
373 // Compiler doesn't know destination size. Don't call __fgets_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700374 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700375 return __fgets_real(dest, size, stream);
376 }
377
378 // Compiler can prove, at compile time, that the passed in size
379 // is always <= the actual object size. Don't call __fgets_chk
Elliott Hughes41b31792013-01-28 10:36:31 -0800380 if (__builtin_constant_p(size) && (size <= (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700381 return __fgets_real(dest, size, stream);
382 }
383
384 // Compiler can prove, at compile time, that the passed in size
385 // is always > the actual object size. Force a compiler error.
Elliott Hughes41b31792013-01-28 10:36:31 -0800386 if (__builtin_constant_p(size) && (size > (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700387 __fgets_too_big_error();
388 }
389
390 return __fgets_chk(dest, size, stream, bos);
391}
392
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700393#endif /* !defined(__clang__) */
394
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700395#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevichcffdf662012-06-11 15:50:57 -0700396
Elliott Hughes5470c182016-07-22 11:36:17 -0700397#if defined(__clang__)
398#pragma clang diagnostic pop
399#endif
400
Dan Albert658727e2014-10-07 11:10:36 -0700401__END_DECLS
402
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800403#endif /* _STDIO_H_ */