blob: b61809133bead8e04a7b5c6c4d8a9f4a14628ec7 [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
Elliott Hughes168667c2014-11-14 14:42:59 -080058extern FILE* stdin;
59extern FILE* stdout;
60extern FILE* stderr;
61/* 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
91/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
92#if __BSD_VISIBLE || __XPG_VISIBLE
93#define P_tmpdir "/tmp/"
94#endif
95#define L_tmpnam 1024 /* XXX must be == PATH_MAX */
96#define TMP_MAX 308915776
97
Elliott Hughes1ed337d2015-02-02 14:02:09 -080098#define SEEK_SET 0
99#define SEEK_CUR 1
100#define SEEK_END 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800102/*
103 * Functions defined in ANSI C standard.
104 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105void clearerr(FILE *);
106int fclose(FILE *);
107int feof(FILE *);
108int ferror(FILE *);
109int fflush(FILE *);
110int fgetc(FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700111char *fgets(char * __restrict, int, FILE * __restrict);
112FILE *fopen(const char * __restrict , const char * __restrict);
113int fprintf(FILE * __restrict , const char * __restrict, ...)
114 __printflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115int fputc(int, FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700116int fputs(const char * __restrict, FILE * __restrict);
117size_t fread(void * __restrict, size_t, size_t, FILE * __restrict);
118FILE *freopen(const char * __restrict, const char * __restrict,
119 FILE * __restrict);
120int fscanf(FILE * __restrict, const char * __restrict, ...)
121 __scanflike(2, 3);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700122size_t fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123int getc(FILE *);
124int getchar(void);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300125ssize_t getdelim(char ** __restrict, size_t * __restrict, int,
126 FILE * __restrict);
127ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict);
Elliott Hughesc13fb752013-12-17 20:43:30 -0800128
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129void perror(const char *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700130int printf(const char * __restrict, ...)
131 __printflike(1, 2);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132int putc(int, FILE *);
133int putchar(int);
134int puts(const char *);
135int remove(const char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136void rewind(FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700137int scanf(const char * __restrict, ...)
138 __scanflike(1, 2);
139void setbuf(FILE * __restrict, char * __restrict);
140int setvbuf(FILE * __restrict, char * __restrict, int, size_t);
141int sscanf(const char * __restrict, const char * __restrict, ...)
142 __scanflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143FILE *tmpfile(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144int ungetc(int, FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700145int vfprintf(FILE * __restrict, const char * __restrict, __va_list)
146 __printflike(2, 0);
147int vprintf(const char * __restrict, __va_list)
148 __printflike(1, 0);
149
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700150int dprintf(int, const char * __restrict, ...) __printflike(2, 3);
151int vdprintf(int, const char * __restrict, __va_list) __printflike(2, 0);
152
Elliott Hughesd04c1832013-05-14 16:08:43 -0700153#ifndef __AUDIT__
Dan Albert96350462014-06-17 23:31:21 +0000154#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800155char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead")));
Dan Albert96350462014-06-17 23:31:21 +0000156#endif
Elliott Hughesc13fb752013-12-17 20:43:30 -0800157int sprintf(char* __restrict, const char* __restrict, ...)
158 __printflike(2, 3) __warnattr("sprintf is often misused; please use snprintf");
Elliott Hughesc13fb752013-12-17 20:43:30 -0800159int vsprintf(char* __restrict, const char* __restrict, __va_list)
160 __printflike(2, 0) __warnattr("vsprintf is often misused; please use vsnprintf");
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800161char* tmpnam(char*) __attribute__((deprecated("tmpnam is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughesc13fb752013-12-17 20:43:30 -0800162#if __XPG_VISIBLE
163char* tempnam(const char*, const char*)
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800164 __attribute__((deprecated("tempnam is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughesc13fb752013-12-17 20:43:30 -0800165#endif
Elliott Hughesd04c1832013-05-14 16:08:43 -0700166#endif
167
Elliott Hughes9677fab2016-01-25 15:50:59 -0800168int rename(const char*, const char*);
169int renameat(int, const char*, int, const char*);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700170
Elliott Hughes9677fab2016-01-25 15:50:59 -0800171int fseek(FILE*, long, int);
172long ftell(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800173
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800174#if defined(__USE_FILE_OFFSET64)
Elliott Hughes9677fab2016-01-25 15:50:59 -0800175int fgetpos(FILE*, fpos_t*) __RENAME(fgetpos64);
176int fsetpos(FILE*, const fpos_t*) __RENAME(fsetpos64);
177int fseeko(FILE*, off_t, int) __RENAME(fseeko64);
178off_t ftello(FILE*) __RENAME(ftello64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800179# if defined(__USE_BSD)
180FILE* funopen(const void*,
181 int (*)(void*, char*, int),
182 int (*)(void*, const char*, int),
183 fpos_t (*)(void*, fpos_t, int),
184 int (*)(void*)) __RENAME(funopen64);
185# endif
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800186#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800187int fgetpos(FILE*, fpos_t*);
188int fsetpos(FILE*, const fpos_t*);
189int fseeko(FILE*, off_t, int);
190off_t ftello(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800191# if defined(__USE_BSD)
192FILE* funopen(const void*,
193 int (*)(void*, char*, int),
194 int (*)(void*, const char*, int),
195 fpos_t (*)(void*, fpos_t, int),
196 int (*)(void*));
197# endif
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800198#endif
Elliott Hughes9677fab2016-01-25 15:50:59 -0800199int fgetpos64(FILE*, fpos64_t*);
200int fsetpos64(FILE*, const fpos64_t*);
201int fseeko64(FILE*, off64_t, int);
202off64_t ftello64(FILE*);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800203#if defined(__USE_BSD)
204FILE* funopen64(const void*,
205 int (*)(void*, char*, int),
206 int (*)(void*, const char*, int),
207 fpos64_t (*)(void*, fpos64_t, int),
208 int (*)(void*));
209#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800210
211#if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700212int snprintf(char * __restrict, size_t, const char * __restrict, ...)
213 __printflike(3, 4);
214int vfscanf(FILE * __restrict, const char * __restrict, __va_list)
215 __scanflike(2, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216int vscanf(const char *, __va_list)
Elliott Hughesd04c1832013-05-14 16:08:43 -0700217 __scanflike(1, 0);
218int vsnprintf(char * __restrict, size_t, const char * __restrict, __va_list)
219 __printflike(3, 0);
220int vsscanf(const char * __restrict, const char * __restrict, __va_list)
221 __scanflike(2, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222#endif /* __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE */
223
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224/*
225 * Functions defined in POSIX 1003.1.
226 */
227#if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE
228#define L_ctermid 1024 /* size for ctermid(); PATH_MAX */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230FILE *fdopen(int, const char *);
231int fileno(FILE *);
232
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700233#if (__POSIX_VISIBLE >= 199209)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800234int pclose(FILE *);
235FILE *popen(const char *, const char *);
236#endif
237
238#if __POSIX_VISIBLE >= 199506
239void flockfile(FILE *);
240int ftrylockfile(FILE *);
241void funlockfile(FILE *);
242
243/*
244 * These are normally used through macros as defined below, but POSIX
245 * requires functions as well.
246 */
247int getc_unlocked(FILE *);
248int getchar_unlocked(void);
249int putc_unlocked(int, FILE *);
250int putchar_unlocked(int);
251#endif /* __POSIX_VISIBLE >= 199506 */
252
Elliott Hughes6b841db2014-08-20 16:10:49 -0700253#if __POSIX_VISIBLE >= 200809
254FILE* fmemopen(void*, size_t, const char*);
255FILE* open_memstream(char**, size_t*);
256#endif /* __POSIX_VISIBLE >= 200809 */
257
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800258#endif /* __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE */
259
260/*
261 * Routines that are purely local.
262 */
263#if __BSD_VISIBLE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700264int asprintf(char ** __restrict, const char * __restrict, ...)
265 __printflike(2, 3);
266char *fgetln(FILE * __restrict, size_t * __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800267int fpurge(FILE *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800268void setbuffer(FILE *, char *, int);
269int setlinebuf(FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700270int vasprintf(char ** __restrict, const char * __restrict,
271 __va_list)
272 __printflike(2, 0);
Elliott Hughes2b021e12014-08-19 17:00:33 -0700273
274void clearerr_unlocked(FILE*);
275int feof_unlocked(FILE*);
276int ferror_unlocked(FILE*);
Yabin Cuifb994f42015-11-06 16:13:47 -0800277int fileno_unlocked(FILE*);
Elliott Hughes2b021e12014-08-19 17:00:33 -0700278
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800279#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
280#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800281#endif /* __BSD_VISIBLE */
282
Dan Albert658727e2014-10-07 11:10:36 -0700283extern char* __fgets_chk(char*, int, FILE*, size_t);
284extern char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
285__errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
286__errordecl(__fgets_too_small_error, "fgets called with size less than zero");
Nick Kralevichcffdf662012-06-11 15:50:57 -0700287
Daniel Micayfed26592015-07-18 13:55:51 -0400288extern size_t __fread_chk(void * __restrict, size_t, size_t, FILE * __restrict, size_t);
289extern size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
290__errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
291__errordecl(__fread_overflow, "fread called with overflowing size * count");
292
293extern size_t __fwrite_chk(const void * __restrict, size_t, size_t, FILE * __restrict, size_t);
294extern size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
295__errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
296__errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
297
Dan Albert658727e2014-10-07 11:10:36 -0700298#if defined(__BIONIC_FORTIFY)
Elliott Hughesce45fea2012-10-22 16:10:27 -0700299
Nick Kralevichcffdf662012-06-11 15:50:57 -0700300__BIONIC_FORTIFY_INLINE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700301__printflike(3, 0)
Nick Kralevich9b549c32012-06-12 15:59:04 -0700302int vsnprintf(char *dest, size_t size, const char *format, __va_list ap)
Nick Kralevichcffdf662012-06-11 15:50:57 -0700303{
Nick Kralevich9020fd52013-04-30 11:31:35 -0700304 return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
Nick Kralevichcffdf662012-06-11 15:50:57 -0700305}
306
Nick Kralevich9b549c32012-06-12 15:59:04 -0700307__BIONIC_FORTIFY_INLINE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700308__printflike(2, 0)
Nick Kralevich9b549c32012-06-12 15:59:04 -0700309int vsprintf(char *dest, const char *format, __va_list ap)
310{
Nick Kralevich9020fd52013-04-30 11:31:35 -0700311 return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
Nick Kralevich9b549c32012-06-12 15:59:04 -0700312}
313
Nick Kralevich621b19d2013-06-25 10:02:35 -0700314#if defined(__clang__)
Nick Kralevich7eb28b52014-03-18 17:03:38 -0700315 #if !defined(snprintf)
316 #define __wrap_snprintf(dest, size, ...) __builtin___snprintf_chk(dest, size, 0, __bos(dest), __VA_ARGS__)
317 #define snprintf(...) __wrap_snprintf(__VA_ARGS__)
318 #endif
Nick Kralevich621b19d2013-06-25 10:02:35 -0700319#else
Nick Kralevichcffdf662012-06-11 15:50:57 -0700320__BIONIC_FORTIFY_INLINE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700321__printflike(3, 4)
Nick Kralevich621b19d2013-06-25 10:02:35 -0700322int snprintf(char *dest, size_t size, const char *format, ...)
Nick Kralevichcffdf662012-06-11 15:50:57 -0700323{
Nick Kralevich621b19d2013-06-25 10:02:35 -0700324 return __builtin___snprintf_chk(dest, size, 0,
325 __bos(dest), format, __builtin_va_arg_pack());
Nick Kralevich9b549c32012-06-12 15:59:04 -0700326}
Nick Kralevich621b19d2013-06-25 10:02:35 -0700327#endif
Nick Kralevich9b549c32012-06-12 15:59:04 -0700328
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700329#if defined(__clang__)
Nick Kralevich7eb28b52014-03-18 17:03:38 -0700330 #if !defined(sprintf)
331 #define __wrap_sprintf(dest, ...) __builtin___sprintf_chk(dest, 0, __bos(dest), __VA_ARGS__)
332 #define sprintf(...) __wrap_sprintf(__VA_ARGS__)
333 #endif
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700334#else
Nick Kralevich9b549c32012-06-12 15:59:04 -0700335__BIONIC_FORTIFY_INLINE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700336__printflike(2, 3)
Nick Kralevich9b549c32012-06-12 15:59:04 -0700337int sprintf(char *dest, const char *format, ...)
338{
339 return __builtin___sprintf_chk(dest, 0,
Nick Kralevich78d6d982013-04-29 16:29:37 -0700340 __bos(dest), format, __builtin_va_arg_pack());
Nick Kralevichcffdf662012-06-11 15:50:57 -0700341}
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700342#endif
Nick Kralevichcffdf662012-06-11 15:50:57 -0700343
Daniel Micayfed26592015-07-18 13:55:51 -0400344__BIONIC_FORTIFY_INLINE
345size_t fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
346 size_t bos = __bos0(buf);
347
348#if !defined(__clang__)
349 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
350 return __fread_real(buf, size, count, stream);
351 }
352
353 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
354 size_t total;
355 if (__size_mul_overflow(size, count, &total)) {
356 __fread_overflow();
357 }
358
359 if (total > bos) {
360 __fread_too_big_error();
361 }
362
363 return __fread_real(buf, size, count, stream);
364 }
365#endif
366
367 return __fread_chk(buf, size, count, stream, bos);
368}
369
370__BIONIC_FORTIFY_INLINE
371size_t fwrite(const void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
372 size_t bos = __bos0(buf);
373
374#if !defined(__clang__)
375 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
376 return __fwrite_real(buf, size, count, stream);
377 }
378
379 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
380 size_t total;
381 if (__size_mul_overflow(size, count, &total)) {
382 __fwrite_overflow();
383 }
384
385 if (total > bos) {
386 __fwrite_too_big_error();
387 }
388
389 return __fwrite_real(buf, size, count, stream);
390 }
391#endif
392
393 return __fwrite_chk(buf, size, count, stream, bos);
394}
395
Elliott Hughescd0609f2013-12-19 12:21:07 -0800396#if !defined(__clang__)
Nick Kralevich965dbc62012-07-03 11:45:31 -0700397
398__BIONIC_FORTIFY_INLINE
Elliott Hughescd0609f2013-12-19 12:21:07 -0800399char *fgets(char* dest, int size, FILE* stream) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700400 size_t bos = __bos(dest);
Nick Kralevich965dbc62012-07-03 11:45:31 -0700401
402 // Compiler can prove, at compile time, that the passed in size
403 // is always negative. Force a compiler error.
404 if (__builtin_constant_p(size) && (size < 0)) {
405 __fgets_too_small_error();
406 }
407
408 // Compiler doesn't know destination size. Don't call __fgets_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700409 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700410 return __fgets_real(dest, size, stream);
411 }
412
413 // Compiler can prove, at compile time, that the passed in size
414 // is always <= the actual object size. Don't call __fgets_chk
Elliott Hughes41b31792013-01-28 10:36:31 -0800415 if (__builtin_constant_p(size) && (size <= (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700416 return __fgets_real(dest, size, stream);
417 }
418
419 // Compiler can prove, at compile time, that the passed in size
420 // is always > the actual object size. Force a compiler error.
Elliott Hughes41b31792013-01-28 10:36:31 -0800421 if (__builtin_constant_p(size) && (size > (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700422 __fgets_too_big_error();
423 }
424
425 return __fgets_chk(dest, size, stream, bos);
426}
427
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700428#endif /* !defined(__clang__) */
429
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700430#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevichcffdf662012-06-11 15:50:57 -0700431
Dan Albert658727e2014-10-07 11:10:36 -0700432__END_DECLS
433
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800434#endif /* _STDIO_H_ */