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