blob: 1df4b54f2cb205e2ef13f929f42852935b7aec1f [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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052typedef off_t fpos_t; /* stdio file position type */
53
Elliott Hughesf0141df2015-10-12 12:44:23 -070054struct __sFILE;
55typedef struct __sFILE FILE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056
Elliott Hughes168667c2014-11-14 14:42:59 -080057extern FILE* stdin;
58extern FILE* stdout;
59extern FILE* stderr;
60/* C99 and earlier plus current C++ standards say these must be macros. */
61#define stdin stdin
62#define stdout stdout
63#define stderr stderr
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
65#define __SLBF 0x0001 /* line buffered */
66#define __SNBF 0x0002 /* unbuffered */
67#define __SRD 0x0004 /* OK to read */
68#define __SWR 0x0008 /* OK to write */
69 /* RD and WR are never simultaneously asserted */
70#define __SRW 0x0010 /* open for reading & writing */
71#define __SEOF 0x0020 /* found EOF */
72#define __SERR 0x0040 /* found error */
73#define __SMBF 0x0080 /* _buf is from malloc */
74#define __SAPP 0x0100 /* fdopen()ed in append mode */
75#define __SSTR 0x0200 /* this is an sprintf/snprintf string */
Elliott Hughesd04c1832013-05-14 16:08:43 -070076#define __SOPT 0x0400 /* do fseek() optimization */
77#define __SNPT 0x0800 /* do not do fseek() optimization */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078#define __SOFF 0x1000 /* set iff _offset is in fact correct */
79#define __SMOD 0x2000 /* true => fgetln modified _p text */
80#define __SALC 0x4000 /* allocate string space dynamically */
Kenny Rootf5823402011-02-12 07:13:44 -080081#define __SIGN 0x8000 /* ignore this file in _fwalk */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082
83/*
84 * The following three definitions are for ANSI C, which took them
85 * from System V, which brilliantly took internal interface macros and
86 * made them official arguments to setvbuf(), without renaming them.
87 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
88 *
89 * Although numbered as their counterparts above, the implementation
90 * does not rely on this.
91 */
92#define _IOFBF 0 /* setvbuf should set fully buffered */
93#define _IOLBF 1 /* setvbuf should set line buffered */
94#define _IONBF 2 /* setvbuf should set unbuffered */
95
96#define BUFSIZ 1024 /* size of buffer used by setbuf */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097#define EOF (-1)
98
99/*
Elliott Hughesd04c1832013-05-14 16:08:43 -0700100 * FOPEN_MAX is a minimum maximum, and is the number of streams that
101 * stdio can provide without attempting to allocate further resources
102 * (which could fail). Do not use this for anything.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103 */
Elliott Hughesd04c1832013-05-14 16:08:43 -0700104
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105#define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */
106#define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */
107
108/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
109#if __BSD_VISIBLE || __XPG_VISIBLE
110#define P_tmpdir "/tmp/"
111#endif
112#define L_tmpnam 1024 /* XXX must be == PATH_MAX */
113#define TMP_MAX 308915776
114
Elliott Hughes1ed337d2015-02-02 14:02:09 -0800115#define SEEK_SET 0
116#define SEEK_CUR 1
117#define SEEK_END 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119/*
120 * Functions defined in ANSI C standard.
121 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122void clearerr(FILE *);
123int fclose(FILE *);
124int feof(FILE *);
125int ferror(FILE *);
126int fflush(FILE *);
127int fgetc(FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700128char *fgets(char * __restrict, int, FILE * __restrict);
129FILE *fopen(const char * __restrict , const char * __restrict);
130int fprintf(FILE * __restrict , const char * __restrict, ...)
131 __printflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132int fputc(int, FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700133int fputs(const char * __restrict, FILE * __restrict);
134size_t fread(void * __restrict, size_t, size_t, FILE * __restrict);
135FILE *freopen(const char * __restrict, const char * __restrict,
136 FILE * __restrict);
137int fscanf(FILE * __restrict, const char * __restrict, ...)
138 __scanflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139int fseek(FILE *, long, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800140long ftell(FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700141size_t fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142int getc(FILE *);
143int getchar(void);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300144ssize_t getdelim(char ** __restrict, size_t * __restrict, int,
145 FILE * __restrict);
146ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict);
Elliott Hughesc13fb752013-12-17 20:43:30 -0800147
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148void perror(const char *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700149int printf(const char * __restrict, ...)
150 __printflike(1, 2);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800151int putc(int, FILE *);
152int putchar(int);
153int puts(const char *);
154int remove(const char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155void rewind(FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700156int scanf(const char * __restrict, ...)
157 __scanflike(1, 2);
158void setbuf(FILE * __restrict, char * __restrict);
159int setvbuf(FILE * __restrict, char * __restrict, int, size_t);
160int sscanf(const char * __restrict, const char * __restrict, ...)
161 __scanflike(2, 3);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162FILE *tmpfile(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163int ungetc(int, FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700164int vfprintf(FILE * __restrict, const char * __restrict, __va_list)
165 __printflike(2, 0);
166int vprintf(const char * __restrict, __va_list)
167 __printflike(1, 0);
168
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700169int dprintf(int, const char * __restrict, ...) __printflike(2, 3);
170int vdprintf(int, const char * __restrict, __va_list) __printflike(2, 0);
171
Elliott Hughesd04c1832013-05-14 16:08:43 -0700172#ifndef __AUDIT__
Dan Albert96350462014-06-17 23:31:21 +0000173#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800174char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead")));
Dan Albert96350462014-06-17 23:31:21 +0000175#endif
Elliott Hughesc13fb752013-12-17 20:43:30 -0800176int sprintf(char* __restrict, const char* __restrict, ...)
177 __printflike(2, 3) __warnattr("sprintf is often misused; please use snprintf");
Elliott Hughesc13fb752013-12-17 20:43:30 -0800178int vsprintf(char* __restrict, const char* __restrict, __va_list)
179 __printflike(2, 0) __warnattr("vsprintf is often misused; please use vsnprintf");
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800180char* tmpnam(char*) __attribute__((deprecated("tmpnam is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughesc13fb752013-12-17 20:43:30 -0800181#if __XPG_VISIBLE
182char* tempnam(const char*, const char*)
Yabin Cui9b4f77f2015-02-23 16:42:07 -0800183 __attribute__((deprecated("tempnam is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughesc13fb752013-12-17 20:43:30 -0800184#endif
Elliott Hughesd04c1832013-05-14 16:08:43 -0700185#endif
186
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -0700187extern int rename(const char*, const char*);
188extern int renameat(int, const char*, int, const char*);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700189
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800190#if defined(__USE_FILE_OFFSET64)
191/* Not possible. */
192int fgetpos(FILE * __restrict, fpos_t * __restrict)
193 __attribute__((__error__("not available with _FILE_OFFSET_BITS=64")));
194int fsetpos(FILE *, const fpos_t *)
195 __attribute__((__error__("not available with _FILE_OFFSET_BITS=64")));
196int fseeko(FILE *, off_t, int)
197 __attribute__((__error__("not available with _FILE_OFFSET_BITS=64")));
198off_t ftello(FILE *)
199 __attribute__((__error__("not available with _FILE_OFFSET_BITS=64")));
200#else
Elliott Hughesd04c1832013-05-14 16:08:43 -0700201int fgetpos(FILE * __restrict, fpos_t * __restrict);
202int fsetpos(FILE *, const fpos_t *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700203int fseeko(FILE *, off_t, int);
204off_t ftello(FILE *);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800205#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206
207#if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700208int snprintf(char * __restrict, size_t, const char * __restrict, ...)
209 __printflike(3, 4);
210int vfscanf(FILE * __restrict, const char * __restrict, __va_list)
211 __scanflike(2, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212int vscanf(const char *, __va_list)
Elliott Hughesd04c1832013-05-14 16:08:43 -0700213 __scanflike(1, 0);
214int vsnprintf(char * __restrict, size_t, const char * __restrict, __va_list)
215 __printflike(3, 0);
216int vsscanf(const char * __restrict, const char * __restrict, __va_list)
217 __scanflike(2, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218#endif /* __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE */
219
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220/*
221 * Functions defined in POSIX 1003.1.
222 */
223#if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE
224#define L_ctermid 1024 /* size for ctermid(); PATH_MAX */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226FILE *fdopen(int, const char *);
227int fileno(FILE *);
228
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700229#if (__POSIX_VISIBLE >= 199209)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230int pclose(FILE *);
231FILE *popen(const char *, const char *);
232#endif
233
234#if __POSIX_VISIBLE >= 199506
235void flockfile(FILE *);
236int ftrylockfile(FILE *);
237void funlockfile(FILE *);
238
239/*
240 * These are normally used through macros as defined below, but POSIX
241 * requires functions as well.
242 */
243int getc_unlocked(FILE *);
244int getchar_unlocked(void);
245int putc_unlocked(int, FILE *);
246int putchar_unlocked(int);
247#endif /* __POSIX_VISIBLE >= 199506 */
248
Elliott Hughes6b841db2014-08-20 16:10:49 -0700249#if __POSIX_VISIBLE >= 200809
250FILE* fmemopen(void*, size_t, const char*);
251FILE* open_memstream(char**, size_t*);
252#endif /* __POSIX_VISIBLE >= 200809 */
253
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800254#endif /* __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE */
255
256/*
257 * Routines that are purely local.
258 */
259#if __BSD_VISIBLE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700260int asprintf(char ** __restrict, const char * __restrict, ...)
261 __printflike(2, 3);
262char *fgetln(FILE * __restrict, size_t * __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800263int fpurge(FILE *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264void setbuffer(FILE *, char *, int);
265int setlinebuf(FILE *);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700266int vasprintf(char ** __restrict, const char * __restrict,
267 __va_list)
268 __printflike(2, 0);
Elliott Hughes2b021e12014-08-19 17:00:33 -0700269
270void clearerr_unlocked(FILE*);
271int feof_unlocked(FILE*);
272int ferror_unlocked(FILE*);
273
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274/*
275 * Stdio function-access interface.
276 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800277FILE *funopen(const void *,
278 int (*)(void *, char *, int),
279 int (*)(void *, const char *, int),
280 fpos_t (*)(void *, fpos_t, int),
281 int (*)(void *));
Dan Albert658727e2014-10-07 11:10:36 -0700282
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800283#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
284#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
285#endif /* __BSD_VISIBLE */
286
Dan Albert658727e2014-10-07 11:10:36 -0700287extern char* __fgets_chk(char*, int, FILE*, size_t);
288extern char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
289__errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
290__errordecl(__fgets_too_small_error, "fgets called with size less than zero");
Nick Kralevichcffdf662012-06-11 15:50:57 -0700291
Daniel Micayfed26592015-07-18 13:55:51 -0400292extern size_t __fread_chk(void * __restrict, size_t, size_t, FILE * __restrict, size_t);
293extern size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
294__errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
295__errordecl(__fread_overflow, "fread called with overflowing size * count");
296
297extern size_t __fwrite_chk(const void * __restrict, size_t, size_t, FILE * __restrict, size_t);
298extern size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
299__errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
300__errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
301
Dan Albert658727e2014-10-07 11:10:36 -0700302#if defined(__BIONIC_FORTIFY)
Elliott Hughesce45fea2012-10-22 16:10:27 -0700303
Nick Kralevichcffdf662012-06-11 15:50:57 -0700304__BIONIC_FORTIFY_INLINE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700305__printflike(3, 0)
Nick Kralevich9b549c32012-06-12 15:59:04 -0700306int vsnprintf(char *dest, size_t size, const char *format, __va_list ap)
Nick Kralevichcffdf662012-06-11 15:50:57 -0700307{
Nick Kralevich9020fd52013-04-30 11:31:35 -0700308 return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
Nick Kralevichcffdf662012-06-11 15:50:57 -0700309}
310
Nick Kralevich9b549c32012-06-12 15:59:04 -0700311__BIONIC_FORTIFY_INLINE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700312__printflike(2, 0)
Nick Kralevich9b549c32012-06-12 15:59:04 -0700313int vsprintf(char *dest, const char *format, __va_list ap)
314{
Nick Kralevich9020fd52013-04-30 11:31:35 -0700315 return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
Nick Kralevich9b549c32012-06-12 15:59:04 -0700316}
317
Nick Kralevich621b19d2013-06-25 10:02:35 -0700318#if defined(__clang__)
Nick Kralevich7eb28b52014-03-18 17:03:38 -0700319 #if !defined(snprintf)
320 #define __wrap_snprintf(dest, size, ...) __builtin___snprintf_chk(dest, size, 0, __bos(dest), __VA_ARGS__)
321 #define snprintf(...) __wrap_snprintf(__VA_ARGS__)
322 #endif
Nick Kralevich621b19d2013-06-25 10:02:35 -0700323#else
Nick Kralevichcffdf662012-06-11 15:50:57 -0700324__BIONIC_FORTIFY_INLINE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700325__printflike(3, 4)
Nick Kralevich621b19d2013-06-25 10:02:35 -0700326int snprintf(char *dest, size_t size, const char *format, ...)
Nick Kralevichcffdf662012-06-11 15:50:57 -0700327{
Nick Kralevich621b19d2013-06-25 10:02:35 -0700328 return __builtin___snprintf_chk(dest, size, 0,
329 __bos(dest), format, __builtin_va_arg_pack());
Nick Kralevich9b549c32012-06-12 15:59:04 -0700330}
Nick Kralevich621b19d2013-06-25 10:02:35 -0700331#endif
Nick Kralevich9b549c32012-06-12 15:59:04 -0700332
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700333#if defined(__clang__)
Nick Kralevich7eb28b52014-03-18 17:03:38 -0700334 #if !defined(sprintf)
335 #define __wrap_sprintf(dest, ...) __builtin___sprintf_chk(dest, 0, __bos(dest), __VA_ARGS__)
336 #define sprintf(...) __wrap_sprintf(__VA_ARGS__)
337 #endif
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700338#else
Nick Kralevich9b549c32012-06-12 15:59:04 -0700339__BIONIC_FORTIFY_INLINE
Elliott Hughesd04c1832013-05-14 16:08:43 -0700340__printflike(2, 3)
Nick Kralevich9b549c32012-06-12 15:59:04 -0700341int sprintf(char *dest, const char *format, ...)
342{
343 return __builtin___sprintf_chk(dest, 0,
Nick Kralevich78d6d982013-04-29 16:29:37 -0700344 __bos(dest), format, __builtin_va_arg_pack());
Nick Kralevichcffdf662012-06-11 15:50:57 -0700345}
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700346#endif
Nick Kralevichcffdf662012-06-11 15:50:57 -0700347
Daniel Micayfed26592015-07-18 13:55:51 -0400348__BIONIC_FORTIFY_INLINE
349size_t fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
350 size_t bos = __bos0(buf);
351
352#if !defined(__clang__)
353 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
354 return __fread_real(buf, size, count, stream);
355 }
356
357 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
358 size_t total;
359 if (__size_mul_overflow(size, count, &total)) {
360 __fread_overflow();
361 }
362
363 if (total > bos) {
364 __fread_too_big_error();
365 }
366
367 return __fread_real(buf, size, count, stream);
368 }
369#endif
370
371 return __fread_chk(buf, size, count, stream, bos);
372}
373
374__BIONIC_FORTIFY_INLINE
375size_t fwrite(const void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
376 size_t bos = __bos0(buf);
377
378#if !defined(__clang__)
379 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
380 return __fwrite_real(buf, size, count, stream);
381 }
382
383 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
384 size_t total;
385 if (__size_mul_overflow(size, count, &total)) {
386 __fwrite_overflow();
387 }
388
389 if (total > bos) {
390 __fwrite_too_big_error();
391 }
392
393 return __fwrite_real(buf, size, count, stream);
394 }
395#endif
396
397 return __fwrite_chk(buf, size, count, stream, bos);
398}
399
Elliott Hughescd0609f2013-12-19 12:21:07 -0800400#if !defined(__clang__)
Nick Kralevich965dbc62012-07-03 11:45:31 -0700401
402__BIONIC_FORTIFY_INLINE
Elliott Hughescd0609f2013-12-19 12:21:07 -0800403char *fgets(char* dest, int size, FILE* stream) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700404 size_t bos = __bos(dest);
Nick Kralevich965dbc62012-07-03 11:45:31 -0700405
406 // Compiler can prove, at compile time, that the passed in size
407 // is always negative. Force a compiler error.
408 if (__builtin_constant_p(size) && (size < 0)) {
409 __fgets_too_small_error();
410 }
411
412 // Compiler doesn't know destination size. Don't call __fgets_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700413 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700414 return __fgets_real(dest, size, stream);
415 }
416
417 // Compiler can prove, at compile time, that the passed in size
418 // is always <= the actual object size. Don't call __fgets_chk
Elliott Hughes41b31792013-01-28 10:36:31 -0800419 if (__builtin_constant_p(size) && (size <= (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700420 return __fgets_real(dest, size, stream);
421 }
422
423 // Compiler can prove, at compile time, that the passed in size
424 // is always > the actual object size. Force a compiler error.
Elliott Hughes41b31792013-01-28 10:36:31 -0800425 if (__builtin_constant_p(size) && (size > (int) bos)) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700426 __fgets_too_big_error();
427 }
428
429 return __fgets_chk(dest, size, stream, bos);
430}
431
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700432#endif /* !defined(__clang__) */
433
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700434#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevichcffdf662012-06-11 15:50:57 -0700435
Dan Albert658727e2014-10-07 11:10:36 -0700436__END_DECLS
437
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800438#endif /* _STDIO_H_ */