blob: b103990d660a55e521131971a79040847b340e57 [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 *);
George Burgess IV7cc779f2017-02-09 00:00:31 -0800124char *fgets(char * __restrict, int, FILE * __restrict) __overloadable
125 __RENAME_CLANG(fgets);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700126int fprintf(FILE * __restrict , const char * __restrict _Nonnull, ...) __printflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127int fputc(int, FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700128int fputs(const char * __restrict, FILE * __restrict);
George Burgess IV7cc779f2017-02-09 00:00:31 -0800129size_t fread(void * __restrict, size_t, size_t, FILE * __restrict)
130 __overloadable __RENAME_CLANG(fread);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700131int fscanf(FILE * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3);
George Burgess IV7cc779f2017-02-09 00:00:31 -0800132size_t fwrite(const void * __restrict, size_t, size_t, FILE * __restrict)
133 __overloadable __RENAME_CLANG(fwrite);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134int getc(FILE *);
135int getchar(void);
Josh Gao46b44162016-05-27 11:14:16 -0700136ssize_t getdelim(char** __restrict, size_t* __restrict, int, FILE* __restrict) __INTRODUCED_IN(18);
137ssize_t getline(char** __restrict, size_t* __restrict, FILE* __restrict) __INTRODUCED_IN(18);
Elliott Hughesc13fb752013-12-17 20:43:30 -0800138
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139void perror(const char *);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700140int printf(const char * __restrict _Nonnull, ...) __printflike(1, 2);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141int putc(int, FILE *);
142int putchar(int);
143int puts(const char *);
144int remove(const char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145void rewind(FILE *);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700146int scanf(const char * __restrict _Nonnull, ...) __scanflike(1, 2);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700147void setbuf(FILE * __restrict, char * __restrict);
148int setvbuf(FILE * __restrict, char * __restrict, int, size_t);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700149int sscanf(const char * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150int ungetc(int, FILE *);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700151int vfprintf(FILE * __restrict, const char * __restrict _Nonnull, __va_list) __printflike(2, 0);
152int vprintf(const char * __restrict _Nonnull, __va_list) __printflike(1, 0);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700153
Dan Albert5f7135e2017-07-26 14:09:45 -0700154#if __ANDROID_API__ >= 21
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700155int dprintf(int, const char* __restrict _Nonnull, ...) __printflike(2, 3) __INTRODUCED_IN(21);
156int vdprintf(int, const char* __restrict _Nonnull, __va_list) __printflike(2, 0) __INTRODUCED_IN(21);
Dan Albert5f7135e2017-07-26 14:09:45 -0700157#else
158/*
159 * Old versions of Android called these fdprintf and vfdprintf out of fears that the glibc names
160 * would collide with user debug printfs.
161 *
162 * Allow users to just use dprintf and vfdprintf on any version by renaming those calls to their
163 * legacy equivalents if needed.
164 */
165int dprintf(int, const char* __restrict _Nonnull, ...) __printflike(2, 3) __RENAME(fdprintf);
166int vdprintf(int, const char* __restrict _Nonnull, __va_list) __printflike(2, 0) __RENAME(vfdprintf);
167#endif
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700168
Elliott Hughesf6495c72016-07-25 09:20:57 -0700169#if (defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L) || \
170 (defined(__cplusplus) && __cplusplus <= 201103L)
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800171char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead")));
Dan Albert96350462014-06-17 23:31:21 +0000172#endif
George Burgess IV7cc779f2017-02-09 00:00:31 -0800173int sprintf(char* __restrict, const char* __restrict _Nonnull, ...)
174 __printflike(2, 3) __warnattr_strict("sprintf is often misused; please use snprintf")
175 __overloadable __RENAME_CLANG(sprintf);
176int vsprintf(char* __restrict, const char* __restrict _Nonnull, __va_list)
177 __overloadable __printflike(2, 0) __RENAME_CLANG(vsprintf)
178 __warnattr_strict("vsprintf is often misused; please use vsnprintf");
179char* tmpnam(char*)
180 __warnattr("tempnam is unsafe, use mkstemp or tmpfile instead");
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700181#define P_tmpdir "/tmp/" /* deprecated */
Elliott Hughesc13fb752013-12-17 20:43:30 -0800182char* tempnam(const char*, const char*)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800183 __warnattr("tempnam is unsafe, use mkstemp or tmpfile instead");
Elliott Hughesd04c1832013-05-14 16:08:43 -0700184
Elliott Hughes9677fab2016-01-25 15:50:59 -0800185int rename(const char*, const char*);
186int renameat(int, const char*, int, const char*);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700187
Elliott Hughes9677fab2016-01-25 15:50:59 -0800188int fseek(FILE*, long, int);
189long ftell(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800190
Elliott Hughes00fedf52017-07-05 15:23:50 -0700191#if defined(__USE_FILE_OFFSET64)
192int fgetpos(FILE*, fpos_t*) __RENAME(fgetpos64) __INTRODUCED_IN(24);
193int fsetpos(FILE*, const fpos_t*) __RENAME(fsetpos64) __INTRODUCED_IN(24);
194int fseeko(FILE*, off_t, int) __RENAME(fseeko64) __INTRODUCED_IN(24);
195off_t ftello(FILE*) __RENAME(ftello64) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800196# if defined(__USE_BSD)
197FILE* funopen(const void*,
198 int (*)(void*, char*, int),
199 int (*)(void*, const char*, int),
200 fpos_t (*)(void*, fpos_t, int),
Elliott Hughes00fedf52017-07-05 15:23:50 -0700201 int (*)(void*)) __RENAME(funopen64) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800202# endif
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800203#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800204int fgetpos(FILE*, fpos_t*);
205int fsetpos(FILE*, const fpos_t*);
206int fseeko(FILE*, off_t, int);
207off_t ftello(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800208# if defined(__USE_BSD)
209FILE* funopen(const void*,
210 int (*)(void*, char*, int),
211 int (*)(void*, const char*, int),
212 fpos_t (*)(void*, fpos_t, int),
213 int (*)(void*));
214# endif
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800215#endif
Josh Gao14adff12016-04-29 12:00:55 -0700216int fgetpos64(FILE*, fpos64_t*) __INTRODUCED_IN(24);
217int fsetpos64(FILE*, const fpos64_t*) __INTRODUCED_IN(24);
218int fseeko64(FILE*, off64_t, int) __INTRODUCED_IN(24);
219off64_t ftello64(FILE*) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800220#if defined(__USE_BSD)
Josh Gao14adff12016-04-29 12:00:55 -0700221FILE* funopen64(const void*, int (*)(void*, char*, int), int (*)(void*, const char*, int),
222 fpos64_t (*)(void*, fpos64_t, int), int (*)(void*)) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800223#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224
Elliott Hughesf226ee52016-02-03 11:24:28 -0800225FILE* fopen(const char* __restrict, const char* __restrict);
Josh Gao14adff12016-04-29 12:00:55 -0700226FILE* fopen64(const char* __restrict, const char* __restrict) __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800227FILE* freopen(const char* __restrict, const char* __restrict, FILE* __restrict);
Josh Gao14adff12016-04-29 12:00:55 -0700228FILE* freopen64(const char* __restrict, const char* __restrict, FILE* __restrict)
229 __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800230FILE* tmpfile(void);
Josh Gao14adff12016-04-29 12:00:55 -0700231FILE* tmpfile64(void) __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800232
George Burgess IV7cc779f2017-02-09 00:00:31 -0800233int snprintf(char* __restrict, size_t, const char* __restrict _Nonnull, ...)
234 __printflike(3, 4) __overloadable __RENAME_CLANG(snprintf);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700235int vfscanf(FILE* __restrict, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0);
236int vscanf(const char* _Nonnull , __va_list) __scanflike(1, 0);
George Burgess IV7cc779f2017-02-09 00:00:31 -0800237int vsnprintf(char* __restrict, size_t, const char* __restrict _Nonnull, __va_list)
238 __printflike(3, 0) __overloadable __RENAME_CLANG(vsnprintf);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700239int vsscanf(const char* __restrict _Nonnull, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700241#define L_ctermid 1024 /* size for ctermid() */
Josh Gao2e8e5e62017-04-20 12:58:31 -0700242char* ctermid(char*) __INTRODUCED_IN(26);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800243
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700244FILE* fdopen(int, const char*);
245int fileno(FILE*);
246int pclose(FILE*);
247FILE* popen(const char*, const char*);
248void flockfile(FILE*);
249int ftrylockfile(FILE*);
250void funlockfile(FILE*);
251int getc_unlocked(FILE*);
252int getchar_unlocked(void);
253int putc_unlocked(int, FILE*);
254int putchar_unlocked(int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255
Josh Gao14adff12016-04-29 12:00:55 -0700256FILE* fmemopen(void*, size_t, const char*) __INTRODUCED_IN(23);
257FILE* open_memstream(char**, size_t*) __INTRODUCED_IN(23);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800258
Elliott Hughes9c8d7112016-06-13 13:23:42 -0700259#if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700260int asprintf(char** __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3);
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700261char* fgetln(FILE* __restrict, size_t* __restrict);
262int fpurge(FILE*);
263void setbuffer(FILE*, char*, int);
264int setlinebuf(FILE*);
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700265int vasprintf(char** __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0);
Josh Gao14adff12016-04-29 12:00:55 -0700266void clearerr_unlocked(FILE*) __INTRODUCED_IN(23);
267int feof_unlocked(FILE*) __INTRODUCED_IN(23);
268int ferror_unlocked(FILE*) __INTRODUCED_IN(23);
269int fileno_unlocked(FILE*) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800270#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
271#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700272#endif /* __USE_BSD */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273
Elliott Hughes5470c182016-07-22 11:36:17 -0700274char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17);
Elliott Hughes5470c182016-07-22 11:36:17 -0700275size_t __fread_chk(void* __restrict, size_t, size_t, FILE* __restrict, size_t)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800276 __INTRODUCED_IN(24);
Elliott Hughes5470c182016-07-22 11:36:17 -0700277size_t __fwrite_chk(const void* __restrict, size_t, size_t, FILE* __restrict, size_t)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800278 __INTRODUCED_IN(24);
Daniel Micayfed26592015-07-18 13:55:51 -0400279
Elliott Hughes53cf3482016-08-09 13:06:41 -0700280#if defined(__BIONIC_FORTIFY) && !defined(__BIONIC_NO_STDIO_FORTIFY)
Elliott Hughesce45fea2012-10-22 16:10:27 -0700281
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800282#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
George Burgess IV7cc779f2017-02-09 00:00:31 -0800283__BIONIC_FORTIFY_INLINE __printflike(3, 0)
284int vsnprintf(char *const __pass_object_size dest, size_t size,
285 const char *_Nonnull format, __va_list ap) __overloadable {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700286 return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
Nick Kralevichcffdf662012-06-11 15:50:57 -0700287}
288
George Burgess IV7cc779f2017-02-09 00:00:31 -0800289__BIONIC_FORTIFY_INLINE __printflike(2, 0)
290int vsprintf(char *const __pass_object_size dest, const char *_Nonnull format,
291 __va_list ap) __overloadable {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700292 return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
Nick Kralevich9b549c32012-06-12 15:59:04 -0700293}
George Burgess IV7cc779f2017-02-09 00:00:31 -0800294#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
Nick Kralevich9b549c32012-06-12 15:59:04 -0700295
Nick Kralevich621b19d2013-06-25 10:02:35 -0700296#if defined(__clang__)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800297#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
298/*
299 * Simple case: `format` can't have format specifiers, so we can just compare
300 * its length to the length of `dest`
301 */
302__BIONIC_ERROR_FUNCTION_VISIBILITY
303int snprintf(char *__restrict dest, size_t size, const char *__restrict format)
304 __overloadable
305 __enable_if(__bos(dest) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
306 __bos(dest) < __builtin_strlen(format),
307 "format string will always overflow destination buffer")
308 __errorattr("format string will always overflow destination buffer");
Nick Kralevich9b549c32012-06-12 15:59:04 -0700309
310__BIONIC_FORTIFY_INLINE
George Burgess IV7cc779f2017-02-09 00:00:31 -0800311__printflike(3, 4)
312int snprintf(char *__restrict const __pass_object_size dest,
313 size_t size, const char *__restrict format, ...) __overloadable {
314 va_list va;
315 va_start(va, format);
316 int result = __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, va);
317 va_end(va);
318 return result;
Nick Kralevichcffdf662012-06-11 15:50:57 -0700319}
George Burgess IV7cc779f2017-02-09 00:00:31 -0800320
321__BIONIC_ERROR_FUNCTION_VISIBILITY
322int sprintf(char *__restrict dest, const char *__restrict format) __overloadable
323 __enable_if(__bos(dest) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
324 __bos(dest) < __builtin_strlen(format),
325 "format string will always overflow destination buffer")
326 __errorattr("format string will always overflow destination buffer");
327
328__BIONIC_FORTIFY_INLINE
329__printflike(2, 3)
330int sprintf(char *__restrict const __pass_object_size dest,
331 const char *__restrict format, ...) __overloadable {
332 va_list va;
333 va_start(va, format);
334 int result = __builtin___vsprintf_chk(dest, 0, __bos(dest), format, va);
335 va_end(va);
336 return result;
337}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800338#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
Nick Kralevichcffdf662012-06-11 15:50:57 -0700339
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800340#if __ANDROID_API__ >= __ANDROID_API_N__
Daniel Micayfed26592015-07-18 13:55:51 -0400341__BIONIC_FORTIFY_INLINE
George Burgess IV7cc779f2017-02-09 00:00:31 -0800342size_t fread(void *__restrict buf, size_t size, size_t count,
343 FILE *__restrict stream) __overloadable
344 __enable_if(__unsafe_check_mul_overflow(size, count), "size * count overflows")
345 __errorattr("size * count overflows");
346
347__BIONIC_FORTIFY_INLINE
348size_t fread(void *__restrict buf, size_t size, size_t count,
349 FILE *__restrict stream) __overloadable
350 __enable_if(!__unsafe_check_mul_overflow(size, count), "no overflow")
351 __enable_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
352 size * count > __bos(buf), "size * count is too large")
353 __errorattr("size * count is too large");
354
355__BIONIC_FORTIFY_INLINE
George Burgess IV156d5a82017-02-10 13:56:22 -0800356size_t fread(void *__restrict const __pass_object_size0 buf, size_t size,
George Burgess IV7cc779f2017-02-09 00:00:31 -0800357 size_t count, FILE *__restrict stream) __overloadable {
Daniel Micayfed26592015-07-18 13:55:51 -0400358 size_t bos = __bos0(buf);
359
George Burgess IV7cc779f2017-02-09 00:00:31 -0800360 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
361 return __call_bypassing_fortify(fread)(buf, size, count, stream);
362 }
363
364 return __fread_chk(buf, size, count, stream, bos);
365}
366
367size_t fwrite(const void * __restrict buf, size_t size,
368 size_t count, FILE * __restrict stream) __overloadable
369 __enable_if(__unsafe_check_mul_overflow(size, count),
370 "size * count overflows")
371 __errorattr("size * count overflows");
372
373size_t fwrite(const void * __restrict buf, size_t size,
374 size_t count, FILE * __restrict stream) __overloadable
375 __enable_if(!__unsafe_check_mul_overflow(size, count), "no overflow")
376 __enable_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
377 size * count > __bos(buf), "size * count is too large")
378 __errorattr("size * count is too large");
379
380__BIONIC_FORTIFY_INLINE
George Burgess IV156d5a82017-02-10 13:56:22 -0800381size_t fwrite(const void * __restrict const __pass_object_size0 buf,
382 size_t size, size_t count, FILE * __restrict stream)
383 __overloadable {
George Burgess IV7cc779f2017-02-09 00:00:31 -0800384 size_t bos = __bos0(buf);
385
386 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
387 return __call_bypassing_fortify(fwrite)(buf, size, count, stream);
388 }
389
390 return __fwrite_chk(buf, size, count, stream, bos);
391}
392#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
393
Dan Albert9c2094f2017-02-14 19:28:18 -0800394#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
George Burgess IV7cc779f2017-02-09 00:00:31 -0800395__BIONIC_ERROR_FUNCTION_VISIBILITY
396char *fgets(char* __restrict dest, int size, FILE* stream) __overloadable
397 __enable_if(size < 0, "size is negative")
398 __errorattr("size is negative");
399
400__BIONIC_ERROR_FUNCTION_VISIBILITY
401char *fgets(char* dest, int size, FILE* stream) __overloadable
402 __enable_if(size >= 0 && size > __bos(dest),
403 "size is larger than the destination buffer")
404 __errorattr("size is larger than the destination buffer");
405
406__BIONIC_FORTIFY_INLINE
407char *fgets(char* __restrict const __pass_object_size dest,
408 int size, FILE* stream) __overloadable {
409 size_t bos = __bos(dest);
410
411 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
412 return __call_bypassing_fortify(fgets)(dest, size, stream);
413 }
414
415 return __fgets_chk(dest, size, stream, bos);
416}
Dan Albert9c2094f2017-02-14 19:28:18 -0800417#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
George Burgess IV7cc779f2017-02-09 00:00:31 -0800418
419#else /* defined(__clang__) */
420
421size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
422__errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
423__errordecl(__fread_overflow, "fread called with overflowing size * count");
424
425char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
426__errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
427__errordecl(__fgets_too_small_error, "fgets called with size less than zero");
428
429size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
430__errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
431__errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
432
433
434#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
435__BIONIC_FORTIFY_INLINE __printflike(3, 4)
436int snprintf(char *__restrict dest, size_t size, const char* _Nonnull format, ...)
437{
438 return __builtin___snprintf_chk(dest, size, 0, __bos(dest), format,
439 __builtin_va_arg_pack());
440}
441
442__BIONIC_FORTIFY_INLINE __printflike(2, 3)
443int sprintf(char *__restrict dest, const char* _Nonnull format, ...) {
444 return __builtin___sprintf_chk(dest, 0, __bos(dest), format,
445 __builtin_va_arg_pack());
446}
447#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
448
449#if __ANDROID_API__ >= __ANDROID_API_N__
450__BIONIC_FORTIFY_INLINE
451size_t fread(void *__restrict buf, size_t size, size_t count, FILE * __restrict stream) {
452 size_t bos = __bos0(buf);
453
Daniel Micayfed26592015-07-18 13:55:51 -0400454 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
455 return __fread_real(buf, size, count, stream);
456 }
457
458 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
459 size_t total;
460 if (__size_mul_overflow(size, count, &total)) {
461 __fread_overflow();
462 }
463
464 if (total > bos) {
465 __fread_too_big_error();
466 }
467
468 return __fread_real(buf, size, count, stream);
469 }
Daniel Micayfed26592015-07-18 13:55:51 -0400470
471 return __fread_chk(buf, size, count, stream, bos);
472}
473
474__BIONIC_FORTIFY_INLINE
475size_t fwrite(const void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
476 size_t bos = __bos0(buf);
477
Daniel Micayfed26592015-07-18 13:55:51 -0400478 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
479 return __fwrite_real(buf, size, count, stream);
480 }
481
482 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
483 size_t total;
484 if (__size_mul_overflow(size, count, &total)) {
485 __fwrite_overflow();
486 }
487
488 if (total > bos) {
489 __fwrite_too_big_error();
490 }
491
492 return __fwrite_real(buf, size, count, stream);
493 }
Daniel Micayfed26592015-07-18 13:55:51 -0400494
495 return __fwrite_chk(buf, size, count, stream, bos);
496}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800497#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
Daniel Micayfed26592015-07-18 13:55:51 -0400498
Dan Albert9c2094f2017-02-14 19:28:18 -0800499#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
Nick Kralevich965dbc62012-07-03 11:45:31 -0700500__BIONIC_FORTIFY_INLINE
Elliott Hughescd0609f2013-12-19 12:21:07 -0800501char *fgets(char* dest, int size, FILE* stream) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700502 size_t bos = __bos(dest);
Nick Kralevich965dbc62012-07-03 11:45:31 -0700503
504 // Compiler can prove, at compile time, that the passed in size
505 // is always negative. Force a compiler error.
506 if (__builtin_constant_p(size) && (size < 0)) {
507 __fgets_too_small_error();
508 }
509
510 // Compiler doesn't know destination size. Don't call __fgets_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700511 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700512 return __fgets_real(dest, size, stream);
513 }
514
515 // Compiler can prove, at compile time, that the passed in size
516 // is always <= the actual object size. Don't call __fgets_chk
Elliott Hughes41b31792013-01-28 10:36:31 -0800517 if (__builtin_constant_p(size) && (size <= (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700518 return __fgets_real(dest, size, stream);
519 }
520
521 // Compiler can prove, at compile time, that the passed in size
522 // is always > the actual object size. Force a compiler error.
Elliott Hughes41b31792013-01-28 10:36:31 -0800523 if (__builtin_constant_p(size) && (size > (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700524 __fgets_too_big_error();
525 }
526
527 return __fgets_chk(dest, size, stream, bos);
528}
Dan Albert9c2094f2017-02-14 19:28:18 -0800529#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
Nick Kralevich965dbc62012-07-03 11:45:31 -0700530
George Burgess IV7cc779f2017-02-09 00:00:31 -0800531#endif /* defined(__clang__) */
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700532#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevichcffdf662012-06-11 15:50:57 -0700533
Elliott Hughes5470c182016-07-22 11:36:17 -0700534#if defined(__clang__)
535#pragma clang diagnostic pop
536#endif
537
Dan Albert658727e2014-10-07 11:10:36 -0700538__END_DECLS
539
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800540#endif /* _STDIO_H_ */