blob: d7c881cb439e2cb8282043dec082e7caa54694ee [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>
42#include <sys/_types.h>
43
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
47#if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE
48#include <sys/types.h> /* XXX should be removed */
49#endif
50
Elliott Hughes0cc0d252012-10-01 15:12:40 -070051#ifndef _SIZE_T_DEFINED_
52#define _SIZE_T_DEFINED_
53typedef unsigned int size_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054#endif
55
Elliott Hughes422b6ef2012-09-28 10:15:52 -070056#ifndef _SSIZE_T_DEFINED_
57#define _SSIZE_T_DEFINED_
58typedef long int ssize_t;
59#endif
60
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061#ifndef _OFF_T_DEFINED_
62#define _OFF_T_DEFINED_
63typedef long off_t;
64#endif
65
66#ifndef NULL
67#ifdef __GNUG__
68#define NULL __null
69#else
70#define NULL 0L
71#endif
72#endif
73
74#define _FSTDIO /* Define for new stdio with functions. */
75
76typedef off_t fpos_t; /* stdio file position type */
77
78/*
79 * NB: to fit things in six character monocase externals, the stdio
80 * code uses the prefix `__s' for stdio objects, typically followed
81 * by a three-character attempt at a mnemonic.
82 */
83
84/* stdio buffers */
85struct __sbuf {
86 unsigned char *_base;
87 int _size;
88};
89
90/*
91 * stdio state variables.
92 *
93 * The following always hold:
94 *
95 * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
96 * _lbfsize is -_bf._size, else _lbfsize is 0
97 * if _flags&__SRD, _w is 0
98 * if _flags&__SWR, _r is 0
99 *
100 * This ensures that the getc and putc macros (or inline functions) never
101 * try to write or read from a file that is in `read' or `write' mode.
102 * (Moreover, they can, and do, automatically switch from read mode to
103 * write mode, and back, on "r+" and "w+" files.)
104 *
105 * _lbfsize is used only to make the inline line-buffered output stream
106 * code as compact as possible.
107 *
108 * _ub, _up, and _ur are used when ungetc() pushes back more characters
109 * than fit in the current _bf, or when ungetc() pushes back a character
110 * that does not match the previous one in _bf. When this happens,
111 * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
112 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
Andy McFaddenc1202512009-11-25 14:16:53 -0800113 *
114 * NOTE: if you change this structure, you also need to update the
115 * std() initializer in findfp.c.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116 */
117typedef struct __sFILE {
118 unsigned char *_p; /* current position in (some) buffer */
119 int _r; /* read space left for getc() */
120 int _w; /* write space left for putc() */
121 short _flags; /* flags, below; this FILE is free if 0 */
122 short _file; /* fileno, if Unix descriptor, else -1 */
123 struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */
124 int _lbfsize; /* 0 or -_bf._size, for inline putc */
125
126 /* operations */
127 void *_cookie; /* cookie passed to io functions */
128 int (*_close)(void *);
129 int (*_read)(void *, char *, int);
130 fpos_t (*_seek)(void *, fpos_t, int);
131 int (*_write)(void *, const char *, int);
132
133 /* extension data, to avoid further ABI breakage */
134 struct __sbuf _ext;
135 /* data for long sequences of ungetc() */
136 unsigned char *_up; /* saved _p when _p is doing ungetc data */
137 int _ur; /* saved _r when _r is counting ungetc data */
138
139 /* tricks to meet minimum requirements even when malloc() fails */
140 unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
141 unsigned char _nbuf[1]; /* guarantee a getc() buffer */
142
143 /* separate buffer for fgetln() when line crosses buffer boundary */
144 struct __sbuf _lb; /* buffer for fgetln() */
145
146 /* Unix stdio files get aligned to block boundaries on fseek() */
147 int _blksize; /* stat.st_blksize (may be != _bf._size) */
148 fpos_t _offset; /* current lseek offset */
149} FILE;
150
151__BEGIN_DECLS
152extern FILE __sF[];
153__END_DECLS
154
155#define __SLBF 0x0001 /* line buffered */
156#define __SNBF 0x0002 /* unbuffered */
157#define __SRD 0x0004 /* OK to read */
158#define __SWR 0x0008 /* OK to write */
159 /* RD and WR are never simultaneously asserted */
160#define __SRW 0x0010 /* open for reading & writing */
161#define __SEOF 0x0020 /* found EOF */
162#define __SERR 0x0040 /* found error */
163#define __SMBF 0x0080 /* _buf is from malloc */
164#define __SAPP 0x0100 /* fdopen()ed in append mode */
165#define __SSTR 0x0200 /* this is an sprintf/snprintf string */
166#define __SOPT 0x0400 /* do fseek() optimisation */
167#define __SNPT 0x0800 /* do not do fseek() optimisation */
168#define __SOFF 0x1000 /* set iff _offset is in fact correct */
169#define __SMOD 0x2000 /* true => fgetln modified _p text */
170#define __SALC 0x4000 /* allocate string space dynamically */
Kenny Rootf5823402011-02-12 07:13:44 -0800171#define __SIGN 0x8000 /* ignore this file in _fwalk */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172
173/*
174 * The following three definitions are for ANSI C, which took them
175 * from System V, which brilliantly took internal interface macros and
176 * made them official arguments to setvbuf(), without renaming them.
177 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
178 *
179 * Although numbered as their counterparts above, the implementation
180 * does not rely on this.
181 */
182#define _IOFBF 0 /* setvbuf should set fully buffered */
183#define _IOLBF 1 /* setvbuf should set line buffered */
184#define _IONBF 2 /* setvbuf should set unbuffered */
185
186#define BUFSIZ 1024 /* size of buffer used by setbuf */
187
188#define EOF (-1)
189
190/*
191 * FOPEN_MAX is a minimum maximum, and should be the number of descriptors
192 * that the kernel can provide without allocation of a resource that can
193 * fail without the process sleeping. Do not use this for anything.
194 */
195#define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */
196#define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */
197
198/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
199#if __BSD_VISIBLE || __XPG_VISIBLE
200#define P_tmpdir "/tmp/"
201#endif
202#define L_tmpnam 1024 /* XXX must be == PATH_MAX */
203#define TMP_MAX 308915776
204
205#ifndef SEEK_SET
206#define SEEK_SET 0 /* set file offset to offset */
207#endif
208#ifndef SEEK_CUR
209#define SEEK_CUR 1 /* set file offset to current plus offset */
210#endif
211#ifndef SEEK_END
212#define SEEK_END 2 /* set file offset to EOF plus offset */
213#endif
214
215#define stdin (&__sF[0])
216#define stdout (&__sF[1])
217#define stderr (&__sF[2])
218
219/*
220 * Functions defined in ANSI C standard.
221 */
222__BEGIN_DECLS
223void clearerr(FILE *);
224int fclose(FILE *);
225int feof(FILE *);
226int ferror(FILE *);
227int fflush(FILE *);
228int fgetc(FILE *);
229int fgetpos(FILE *, fpos_t *);
230char *fgets(char *, int, FILE *);
231FILE *fopen(const char *, const char *);
Nick Kralevich9b549c32012-06-12 15:59:04 -0700232int fprintf(FILE *, const char *, ...)
233 __attribute__((__format__ (printf, 2, 3)))
234 __attribute__((__nonnull__ (2)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800235int fputc(int, FILE *);
236int fputs(const char *, FILE *);
237size_t fread(void *, size_t, size_t, FILE *);
238FILE *freopen(const char *, const char *, FILE *);
Nick Kralevich9b549c32012-06-12 15:59:04 -0700239int fscanf(FILE *, const char *, ...)
240 __attribute__ ((__format__ (scanf, 2, 3)))
241 __attribute__ ((__nonnull__ (2)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800242int fseek(FILE *, long, int);
243int fseeko(FILE *, off_t, int);
244int fsetpos(FILE *, const fpos_t *);
245long ftell(FILE *);
246off_t ftello(FILE *);
247size_t fwrite(const void *, size_t, size_t, FILE *);
248int getc(FILE *);
249int getchar(void);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300250ssize_t getdelim(char ** __restrict, size_t * __restrict, int,
251 FILE * __restrict);
252ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800253char *gets(char *);
254#if __BSD_VISIBLE && !defined(__SYS_ERRLIST)
255#define __SYS_ERRLIST
256
257extern int sys_nerr; /* perror(3) external variables */
258extern char *sys_errlist[];
259#endif
260void perror(const char *);
Nick Kralevich9b549c32012-06-12 15:59:04 -0700261int printf(const char *, ...)
262 __attribute__((__format__ (printf, 1, 2)))
263 __attribute__((__nonnull__ (1)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264int putc(int, FILE *);
265int putchar(int);
266int puts(const char *);
267int remove(const char *);
268int rename(const char *, const char *);
269void rewind(FILE *);
Nick Kralevich9b549c32012-06-12 15:59:04 -0700270int scanf(const char *, ...)
271 __attribute__ ((__format__ (scanf, 1, 2)))
272 __attribute__ ((__nonnull__ (1)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273void setbuf(FILE *, char *);
274int setvbuf(FILE *, char *, int, size_t);
Nick Kralevich9b549c32012-06-12 15:59:04 -0700275int sprintf(char *, const char *, ...)
276 __attribute__((__format__ (printf, 2, 3)))
277 __attribute__((__nonnull__ (2)));
278int sscanf(const char *, const char *, ...)
279 __attribute__ ((__format__ (scanf, 2, 3)))
280 __attribute__ ((__nonnull__ (2)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800281FILE *tmpfile(void);
282char *tmpnam(char *);
283int ungetc(int, FILE *);
Nick Kralevich9b549c32012-06-12 15:59:04 -0700284int vfprintf(FILE *, const char *, __va_list)
285 __attribute__((__format__ (printf, 2, 0)))
286 __attribute__((__nonnull__ (2)));
287int vprintf(const char *, __va_list)
288 __attribute__((__format__ (printf, 1, 0)))
289 __attribute__((__nonnull__ (1)));
290int vsprintf(char *, const char *, __va_list)
291 __attribute__((__format__ (printf, 2, 0)))
292 __attribute__((__nonnull__ (2)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800293
294#if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE
295int snprintf(char *, size_t, const char *, ...)
296 __attribute__((__format__ (printf, 3, 4)))
297 __attribute__((__nonnull__ (3)));
298int vfscanf(FILE *, const char *, __va_list)
299 __attribute__((__format__ (scanf, 2, 0)))
300 __attribute__((__nonnull__ (2)));
301int vscanf(const char *, __va_list)
302 __attribute__((__format__ (scanf, 1, 0)))
303 __attribute__((__nonnull__ (1)));
304int vsnprintf(char *, size_t, const char *, __va_list)
305 __attribute__((__format__ (printf, 3, 0)))
306 __attribute__((__nonnull__ (3)));
307int vsscanf(const char *, const char *, __va_list)
308 __attribute__((__format__ (scanf, 2, 0)))
309 __attribute__((__nonnull__ (2)));
310#endif /* __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE */
311
312__END_DECLS
313
314
315/*
316 * Functions defined in POSIX 1003.1.
317 */
318#if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE
319#define L_ctermid 1024 /* size for ctermid(); PATH_MAX */
320#define L_cuserid 9 /* size for cuserid(); UT_NAMESIZE + 1 */
321
322__BEGIN_DECLS
David 'Digit' Turnerbb5581a2010-10-09 17:56:55 +0200323#if 0 /* MISSING FROM BIONIC */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800324char *ctermid(char *);
325char *cuserid(char *);
David 'Digit' Turnerbb5581a2010-10-09 17:56:55 +0200326#endif /* MISSING */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800327FILE *fdopen(int, const char *);
328int fileno(FILE *);
329
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700330#if (__POSIX_VISIBLE >= 199209)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800331int pclose(FILE *);
332FILE *popen(const char *, const char *);
333#endif
334
335#if __POSIX_VISIBLE >= 199506
336void flockfile(FILE *);
337int ftrylockfile(FILE *);
338void funlockfile(FILE *);
339
340/*
341 * These are normally used through macros as defined below, but POSIX
342 * requires functions as well.
343 */
344int getc_unlocked(FILE *);
345int getchar_unlocked(void);
346int putc_unlocked(int, FILE *);
347int putchar_unlocked(int);
348#endif /* __POSIX_VISIBLE >= 199506 */
349
350#if __XPG_VISIBLE
351char *tempnam(const char *, const char *);
352#endif
353__END_DECLS
354
355#endif /* __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE */
356
357/*
358 * Routines that are purely local.
359 */
360#if __BSD_VISIBLE
361__BEGIN_DECLS
362int asprintf(char **, const char *, ...)
363 __attribute__((__format__ (printf, 2, 3)))
364 __attribute__((__nonnull__ (2)));
365char *fgetln(FILE *, size_t *);
366int fpurge(FILE *);
367int getw(FILE *);
368int putw(int, FILE *);
369void setbuffer(FILE *, char *, int);
370int setlinebuf(FILE *);
371int vasprintf(char **, const char *, __va_list)
372 __attribute__((__format__ (printf, 2, 0)))
373 __attribute__((__nonnull__ (2)));
374__END_DECLS
375
376/*
377 * Stdio function-access interface.
378 */
379__BEGIN_DECLS
380FILE *funopen(const void *,
381 int (*)(void *, char *, int),
382 int (*)(void *, const char *, int),
383 fpos_t (*)(void *, fpos_t, int),
384 int (*)(void *));
385__END_DECLS
386#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
387#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
388#endif /* __BSD_VISIBLE */
389
390/*
391 * Functions internal to the implementation.
392 */
393__BEGIN_DECLS
394int __srget(FILE *);
395int __swbuf(int, FILE *);
396__END_DECLS
397
398/*
399 * The __sfoo macros are here so that we can
400 * define function versions in the C library.
401 */
402#define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
403#if defined(__GNUC__)
404static __inline int __sputc(int _c, FILE *_p) {
405 if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
406 return (*_p->_p++ = _c);
407 else
408 return (__swbuf(_c, _p));
409}
410#else
411/*
412 * This has been tuned to generate reasonable code on the vax using pcc.
413 */
414#define __sputc(c, p) \
415 (--(p)->_w < 0 ? \
416 (p)->_w >= (p)->_lbfsize ? \
417 (*(p)->_p = (c)), *(p)->_p != '\n' ? \
418 (int)*(p)->_p++ : \
419 __swbuf('\n', p) : \
420 __swbuf((int)(c), p) : \
421 (*(p)->_p = (c), (int)*(p)->_p++))
422#endif
423
424#define __sfeof(p) (((p)->_flags & __SEOF) != 0)
425#define __sferror(p) (((p)->_flags & __SERR) != 0)
426#define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
427#define __sfileno(p) ((p)->_file)
428
Kenny Rootf5823402011-02-12 07:13:44 -0800429extern int __isthreaded;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800430
Kenny Rootf5823402011-02-12 07:13:44 -0800431#define feof(p) (!__isthreaded ? __sfeof(p) : (feof)(p))
432#define ferror(p) (!__isthreaded ? __sferror(p) : (ferror)(p))
433#define clearerr(p) (!__isthreaded ? __sclearerr(p) : (clearerr)(p))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800434
435#if __POSIX_VISIBLE
Kenny Rootf5823402011-02-12 07:13:44 -0800436#define fileno(p) (!__isthreaded ? __sfileno(p) : (fileno)(p))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800437#endif
438
Kenny Rootf5823402011-02-12 07:13:44 -0800439#define getc(fp) (!__isthreaded ? __sgetc(fp) : (getc)(fp))
440
441#if __BSD_VISIBLE
442/*
443 * The macro implementations of putc and putc_unlocked are not
444 * fully POSIX compliant; they do not set errno on failure
445 */
446#define putc(x, fp) (!__isthreaded ? __sputc(x, fp) : (putc)(x, fp))
447#endif /* __BSD_VISIBLE */
448
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800449#ifndef lint
Kenny Rootf5823402011-02-12 07:13:44 -0800450#if __POSIX_VISIBLE >= 199506
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800451#define getc_unlocked(fp) __sgetc(fp)
452/*
453 * The macro implementations of putc and putc_unlocked are not
454 * fully POSIX compliant; they do not set errno on failure
455 */
456#if __BSD_VISIBLE
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800457#define putc_unlocked(x, fp) __sputc(x, fp)
458#endif /* __BSD_VISIBLE */
Kenny Rootf5823402011-02-12 07:13:44 -0800459#endif /* __POSIX_VISIBLE >= 199506 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800460#endif /* lint */
461
462#define getchar() getc(stdin)
463#define putchar(x) putc(x, stdout)
Kenny Rootf5823402011-02-12 07:13:44 -0800464#define getchar_unlocked() getc_unlocked(stdin)
465#define putchar_unlocked(c) putc_unlocked(c, stdout)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800466
Colin Crossfc10b242010-01-13 17:48:34 -0800467#ifdef _GNU_SOURCE
468/*
469 * glibc defines dprintf(int, const char*, ...), which is poorly named
470 * and likely to conflict with locally defined debugging printfs
471 * fdprintf is a better name, and some programs that use fdprintf use a
472 * #define fdprintf dprintf for compatibility
473 */
Glenn Kastenf39a3fe2012-02-09 10:15:45 -0800474__BEGIN_DECLS
Nick Kralevich9b549c32012-06-12 15:59:04 -0700475int fdprintf(int, const char*, ...)
476 __attribute__((__format__ (printf, 2, 3)))
477 __attribute__((__nonnull__ (2)));
478int vfdprintf(int, const char*, __va_list)
479 __attribute__((__format__ (printf, 2, 0)))
480 __attribute__((__nonnull__ (2)));
Glenn Kastenf39a3fe2012-02-09 10:15:45 -0800481__END_DECLS
Colin Crossfc10b242010-01-13 17:48:34 -0800482#endif /* _GNU_SOURCE */
483
Nick Kralevichcffdf662012-06-11 15:50:57 -0700484#if defined(__BIONIC_FORTIFY_INLINE)
485
486__BIONIC_FORTIFY_INLINE
487__attribute__((__format__ (printf, 3, 0)))
488__attribute__((__nonnull__ (3)))
Nick Kralevich9b549c32012-06-12 15:59:04 -0700489int vsnprintf(char *dest, size_t size, const char *format, __va_list ap)
Nick Kralevichcffdf662012-06-11 15:50:57 -0700490{
Nick Kralevich9b549c32012-06-12 15:59:04 -0700491 return __builtin___vsnprintf_chk(dest, size, 0,
492 __builtin_object_size(dest, 0), format, ap);
Nick Kralevichcffdf662012-06-11 15:50:57 -0700493}
494
Nick Kralevich9b549c32012-06-12 15:59:04 -0700495__BIONIC_FORTIFY_INLINE
496__attribute__((__format__ (printf, 2, 0)))
497__attribute__((__nonnull__ (2)))
498int vsprintf(char *dest, const char *format, __va_list ap)
499{
500 return __builtin___vsprintf_chk(dest, 0,
501 __builtin_object_size(dest, 0), format, ap);
502}
503
Nick Kralevichcffdf662012-06-11 15:50:57 -0700504__BIONIC_FORTIFY_INLINE
505__attribute__((__format__ (printf, 3, 4)))
506__attribute__((__nonnull__ (3)))
507int snprintf(char *str, size_t size, const char *format, ...)
508{
Nick Kralevich9b549c32012-06-12 15:59:04 -0700509 return __builtin___snprintf_chk(str, size, 0,
510 __builtin_object_size(str, 0), format, __builtin_va_arg_pack());
511}
512
513__BIONIC_FORTIFY_INLINE
514__attribute__((__format__ (printf, 2, 3)))
515__attribute__((__nonnull__ (2)))
516int sprintf(char *dest, const char *format, ...)
517{
518 return __builtin___sprintf_chk(dest, 0,
519 __builtin_object_size(dest, 0), format, __builtin_va_arg_pack());
Nick Kralevichcffdf662012-06-11 15:50:57 -0700520}
521
Nick Kralevich965dbc62012-07-03 11:45:31 -0700522extern char *__fgets_real(char *, int, FILE *)
523 __asm__(__USER_LABEL_PREFIX__ "fgets");
524extern void __fgets_too_big_error()
525 __attribute__((__error__("fgets called with size bigger than buffer")));
526extern void __fgets_too_small_error()
527 __attribute__((__error__("fgets called with size less than zero")));
528extern char *__fgets_chk(char *, int, FILE *, size_t);
529
530__BIONIC_FORTIFY_INLINE
531char *fgets(char *dest, int size, FILE *stream)
532{
533 size_t bos = __builtin_object_size(dest, 0);
534
535 // Compiler can prove, at compile time, that the passed in size
536 // is always negative. Force a compiler error.
537 if (__builtin_constant_p(size) && (size < 0)) {
538 __fgets_too_small_error();
539 }
540
541 // Compiler doesn't know destination size. Don't call __fgets_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700542 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevich965dbc62012-07-03 11:45:31 -0700543 return __fgets_real(dest, size, stream);
544 }
545
546 // Compiler can prove, at compile time, that the passed in size
547 // is always <= the actual object size. Don't call __fgets_chk
548 if (__builtin_constant_p(size) && (size <= bos)) {
549 return __fgets_real(dest, size, stream);
550 }
551
552 // Compiler can prove, at compile time, that the passed in size
553 // is always > the actual object size. Force a compiler error.
554 if (__builtin_constant_p(size) && (size > bos)) {
555 __fgets_too_big_error();
556 }
557
558 return __fgets_chk(dest, size, stream, bos);
559}
560
Nick Kralevichcffdf662012-06-11 15:50:57 -0700561#endif /* defined(__BIONIC_FORTIFY_INLINE) */
562
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800563#endif /* _STDIO_H_ */