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