1 /*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)stdio.h 8.5 (Berkeley) 4/29/95 33 * $FreeBSD: src/include/stdio.h,v 1.78 2009/03/25 08:07:52 das Exp $ 34 */ 35 36 #ifndef _STDIO_H_ 37 #define _STDIO_H_ 38 39 #include <sys/cdefs.h> 40 #include <sys/_null.h> 41 #if __BSD_VISIBLE 42 #include <sys/types.h> 43 #else 44 #include <machine/stdint.h> 45 #endif 46 #include <machine/stdarg.h> /* for __va_list */ 47 48 typedef __off_t fpos_t; 49 50 #ifndef _SIZE_T_DECLARED 51 typedef __size_t size_t; 52 #define _SIZE_T_DECLARED 53 #endif 54 55 #if __EXT1_VISIBLE 56 #ifndef _RSIZE_T_DECLARED 57 #define _RSIZE_T_DECLARED 58 typedef size_t rsize_t; 59 #endif 60 #endif 61 62 /* Should be '__POSIX_VISIBLE >= 200809' but stick with fseeko()/ftello() */ 63 #if __XSI_VISIBLE >= 500 || __POSIX_VISIBLE >= 200112 64 #ifndef _OFF_T_DECLARED 65 #define _OFF_T_DECLARED 66 typedef __off_t off_t; 67 #endif 68 #ifndef _SSIZE_T_DECLARED 69 #define _SSIZE_T_DECLARED 70 typedef __ssize_t ssize_t; 71 #endif 72 #endif 73 74 #if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE 75 #ifdef __GNUC__ 76 #ifndef _VA_LIST_DECLARED 77 #define _VA_LIST_DECLARED 78 typedef __va_list va_list; 79 /* __gnuc_va_list is not needed, provided by <machine/stdarg.h> */ 80 #endif 81 #else 82 #include <stdarg.h> 83 #endif 84 #endif 85 86 #define _FSTDIO /* Define for new stdio with functions. */ 87 88 /* 89 * stdio state variables. 90 * 91 * The following always hold: 92 * 93 * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR), 94 * _lbfsize is -_bf._size, else _lbfsize is 0 95 * if _flags&__SRD, _w is 0 96 * if _flags&__SWR, _r is 0 97 * 98 * This ensures that the getc and putc macros (or inline functions) never 99 * try to write or read from a file that is in `read' or `write' mode. 100 * (Moreover, they can, and do, automatically switch from read mode to 101 * write mode, and back, on "r+" and "w+" files.) 102 * 103 * _lbfsize is used only to make the inline line-buffered output stream 104 * code as compact as possible. 105 * 106 * WARNING: Do not change the order of the fields in __FILE_public! 107 */ 108 #ifndef _STDFILE_DECLARED 109 #define _STDFILE_DECLARED 110 typedef struct __FILE FILE; 111 #endif 112 113 struct __FILE_public { 114 unsigned char *_p; /* current position in (some) buffer */ 115 int _flags; /* flags, below; this FILE is free if 0 */ 116 int _fileno; /* fileno, if Unix descriptor, else -1 */ 117 __ssize_t _r; /* read space left for getc() */ 118 __ssize_t _w; /* write space left for putc() */ 119 __ssize_t _lbfsize; /* 0 or -_bf._size, for inline putc */ 120 }; 121 122 #ifndef _STDSTREAM_DECLARED 123 __BEGIN_DECLS 124 extern FILE *__stdinp; 125 extern FILE *__stdoutp; 126 extern FILE *__stderrp; 127 __END_DECLS 128 #define _STDSTREAM_DECLARED 129 #endif 130 131 #define __SLBF 0x0001 /* line buffered */ 132 #define __SNBF 0x0002 /* unbuffered */ 133 #define __SRD 0x0004 /* OK to read */ 134 #define __SWR 0x0008 /* OK to write */ 135 /* RD and WR are never simultaneously asserted */ 136 #define __SRW 0x0010 /* open for reading & writing */ 137 #define __SEOF 0x0020 /* found EOF */ 138 #define __SERR 0x0040 /* found error */ 139 #define __SMBF 0x0080 /* _buf is from malloc */ 140 #define __SAPP 0x0100 /* fdopen()ed in append mode */ 141 #define __SSTR 0x0200 /* this is an sprintf/snprintf string */ 142 #define __SOPT 0x0400 /* do fseek() optimization */ 143 #define __SNPT 0x0800 /* do not do fseek() optimization */ 144 #define __SOFF 0x1000 /* set iff _offset is in fact correct */ 145 #define __SMOD 0x2000 /* true => fgetln modified _p text */ 146 #define __SALC 0x4000 /* allocate string space dynamically */ 147 #define __SIGN 0x8000 /* ignore this file in _fwalk */ 148 149 /* 150 * The following three definitions are for ANSI C, which took them 151 * from System V, which brilliantly took internal interface macros and 152 * made them official arguments to setvbuf(), without renaming them. 153 * Hence, these ugly _IOxxx names are *supposed* to appear in user code. 154 * 155 * Although numbered as their counterparts above, the implementation 156 * does not rely on this. 157 */ 158 #define _IOFBF 0 /* setvbuf should set fully buffered */ 159 #define _IOLBF 1 /* setvbuf should set line buffered */ 160 #define _IONBF 2 /* setvbuf should set unbuffered */ 161 162 #define BUFSIZ 1024 /* size of buffer used by setbuf */ 163 #define EOF (-1) 164 165 /* 166 * FOPEN_MAX is a minimum maximum, and is the number of streams that 167 * stdio can provide without attempting to allocate further resources 168 * (which could fail). Do not use this for anything. 169 */ 170 /* must be == _POSIX_STREAM_MAX <limits.h> */ 171 #ifndef FOPEN_MAX 172 #define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */ 173 #endif 174 #define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */ 175 176 /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */ 177 #if __XSI_VISIBLE 178 #define P_tmpdir "/tmp/" 179 #endif 180 #define L_tmpnam 1024 /* XXX must be == PATH_MAX */ 181 #define TMP_MAX 308915776 182 183 /* Always ensure that these are consistent with <fcntl.h> and <unistd.h>! */ 184 #ifndef SEEK_SET 185 #define SEEK_SET 0 /* set file offset to offset */ 186 #endif 187 #ifndef SEEK_CUR 188 #define SEEK_CUR 1 /* set file offset to current plus offset */ 189 #endif 190 #ifndef SEEK_END 191 #define SEEK_END 2 /* set file offset to EOF plus offset */ 192 #endif 193 194 #define stdin __stdinp 195 #define stdout __stdoutp 196 #define stderr __stderrp 197 198 #if __BSD_VISIBLE 199 /* fparseln(3) */ 200 #define FPARSELN_UNESCESC 0x01 201 #define FPARSELN_UNESCCONT 0x02 202 #define FPARSELN_UNESCCOMM 0x04 203 #define FPARSELN_UNESCREST 0x08 204 #define FPARSELN_UNESCALL 0x0f 205 #endif 206 207 __BEGIN_DECLS 208 #ifdef _XLOCALE_H_ 209 #include <xlocale/_stdio.h> 210 #endif 211 /* 212 * Functions defined in ANSI C standard. 213 */ 214 void clearerr(FILE *); 215 int fclose(FILE *); 216 int feof(FILE *); 217 int ferror(FILE *); 218 int fflush(FILE *); 219 int fgetc(FILE *); 220 int fgetpos(FILE * __restrict, fpos_t * __restrict); 221 char *fgets(char * __restrict, int, FILE * __restrict); 222 FILE *fopen(const char * __restrict, const char * __restrict); 223 int fprintf(FILE * __restrict, const char * __restrict, ...) 224 __printflike(2, 3); 225 int fputc(int, FILE *); 226 int fputs(const char * __restrict, FILE * __restrict); 227 size_t fread(void * __restrict, size_t, size_t, FILE * __restrict); 228 FILE *freopen(const char * __restrict, const char * __restrict, 229 FILE * __restrict); 230 int fscanf(FILE * __restrict, const char * __restrict, ...) 231 __scanflike(2, 3); 232 int fseek(FILE *, long, int); 233 int fsetpos(FILE *, const fpos_t *); 234 long ftell(FILE *); 235 size_t fwrite(const void * __restrict, size_t, size_t, FILE * __restrict); 236 int getc(FILE *); 237 int getchar(void); 238 char *gets(char *); 239 #if __EXT1_VISIBLE 240 char *gets_s(char *, rsize_t); 241 #endif 242 void perror(const char *); 243 int printf(const char * __restrict, ...) __printflike(1, 2); 244 int putc(int, FILE *); 245 int putchar(int); 246 int puts(const char *); 247 int remove(const char *); 248 int rename(const char *, const char *); 249 void rewind(FILE *); 250 int scanf(const char * __restrict, ...) __scanflike(1, 2); 251 void setbuf(FILE * __restrict, char * __restrict); 252 int setvbuf(FILE * __restrict, char * __restrict, int, size_t); 253 int sprintf(char * __restrict, const char * __restrict, ...) 254 __printflike(2, 3); 255 int sscanf(const char * __restrict, const char * __restrict, ...) 256 __scanflike(2, 3); 257 FILE *tmpfile(void); 258 char *tmpnam(char *); 259 int ungetc(int, FILE *); 260 int vfprintf(FILE * __restrict, const char * __restrict, __va_list) 261 __printflike(2, 0); 262 int vprintf(const char * __restrict, __va_list) __printflike(1, 0); 263 int vsprintf(char * __restrict, const char * __restrict, __va_list) 264 __printflike(2, 0); 265 266 #if __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE >= 500 267 int snprintf(char * __restrict, size_t, const char * __restrict, ...) 268 __printflike(3, 4); 269 int vsnprintf(char * __restrict, size_t, const char * __restrict, 270 __va_list) __printflike(3, 0); 271 #endif 272 #if __ISO_C_VISIBLE >= 1999 273 int vfscanf(FILE * __restrict, const char * __restrict, __va_list) 274 __scanflike(2, 0); 275 int vscanf(const char * __restrict, __va_list) __scanflike(1, 0); 276 int vsscanf(const char * __restrict, const char * __restrict, __va_list) 277 __scanflike(2, 0); 278 #endif 279 280 /* 281 * Functions defined in all versions of POSIX 1003.1. 282 */ 283 #if __BSD_VISIBLE || (__POSIX_VISIBLE && __POSIX_VISIBLE < 200112) 284 #define L_cuserid 17 /* size for cuserid(3); MAXLOGNAME, legacy */ 285 #endif 286 287 #if __POSIX_VISIBLE 288 #define L_ctermid 1024 /* size for ctermid(3); PATH_MAX */ 289 290 char *ctermid(char *); 291 FILE *fdopen(int, const char *); 292 int fileno(FILE *); 293 #endif /* __POSIX_VISIBLE */ 294 295 #if __POSIX_VISIBLE >= 199209 296 int pclose(FILE *); 297 FILE *popen(const char *, const char *); 298 #endif 299 300 #if __POSIX_VISIBLE >= 199506 301 int ftrylockfile(FILE *); 302 void flockfile(FILE *); 303 void funlockfile(FILE *); 304 305 /* 306 * These are normally used through macros as defined below, but POSIX 307 * requires functions as well. 308 */ 309 int getc_unlocked(FILE *); 310 int getchar_unlocked(void); 311 int putc_unlocked(int, FILE *); 312 int putchar_unlocked(int); 313 #endif 314 #if __BSD_VISIBLE 315 void clearerr_unlocked(FILE *); 316 int feof_unlocked(FILE *); 317 int ferror_unlocked(FILE *); 318 int fflush_unlocked(FILE *); 319 int fileno_unlocked(FILE *); 320 int fputc_unlocked(int, FILE *); 321 int fputs_unlocked(const char * __restrict, FILE * __restrict); 322 size_t fread_unlocked(void * __restrict, size_t, size_t, FILE * __restrict); 323 size_t fwrite_unlocked(const void * __restrict, size_t, size_t, 324 FILE * __restrict); 325 #endif 326 327 #if __XSI_VISIBLE >= 500 || __POSIX_VISIBLE >= 200112 328 int fseeko(FILE *, off_t, int); 329 off_t ftello(FILE *); 330 #endif 331 332 #if __BSD_VISIBLE || (__XSI_VISIBLE && __XSI_VISIBLE < 600) 333 int getw(FILE *); 334 int putw(int, FILE *); 335 #endif /* BSD or X/Open before issue 6 */ 336 337 #if __XSI_VISIBLE 338 char *tempnam(const char *, const char *); 339 #endif 340 341 #if __POSIX_VISIBLE >= 200809 342 FILE *fmemopen(void *__restrict, size_t, const char *__restrict); 343 ssize_t getdelim(char ** __restrict, size_t * __restrict, int, 344 FILE * __restrict); 345 FILE *open_memstream(char **, size_t *); 346 int renameat(int, const char *, int, const char *); 347 int vdprintf(int, const char * __restrict, __va_list) __printflike(2, 0); 348 int dprintf(int, const char * __restrict, ...) __printflike(2, 3); 349 ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict); 350 #endif /* __POSIX_VISIBLE >= 200809 */ 351 352 /* 353 * Routines that are purely local. 354 */ 355 #if __BSD_VISIBLE 356 int asprintf(char **, const char *, ...) __printflike(2, 3); 357 char *ctermid_r(char *); 358 void fcloseall(void); 359 void *fcookie(FILE *); 360 char *fgetln(FILE *, size_t *); 361 const char *fmtcheck(const char *, const char *) __format_arg(2); 362 char *fparseln(FILE *, size_t *, size_t *, const char[3], int); 363 __ssize_t __fpending(const FILE *); 364 int fpurge(FILE *); 365 void setbuffer(FILE *, char *, int); 366 int setlinebuf(FILE *); 367 int vasprintf(char **, const char *, __va_list) __printflike(2, 0); 368 369 /* 370 * The system error table contains messages for the first sys_nerr 371 * positive errno values. Use strerror() or strerror_r() from <string.h> 372 * instead. 373 */ 374 extern const int sys_nerr; 375 extern const char *const sys_errlist[]; 376 377 /* 378 * Stdio function-access interface. 379 */ 380 FILE *funopen(const void *, int (*)(void *, char *, int), 381 int (*)(void *, const char *, int), 382 fpos_t (*)(void *, fpos_t, int), int (*)(void *)); 383 #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) 384 #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) 385 386 typedef __ssize_t cookie_read_function_t(void *, char *, size_t); 387 typedef __ssize_t cookie_write_function_t(void *, const char *, size_t); 388 typedef int cookie_seek_function_t(void *, __off_t *, int); 389 typedef int cookie_close_function_t(void *); 390 typedef struct { 391 cookie_read_function_t *read; 392 cookie_write_function_t *write; 393 cookie_seek_function_t *seek; 394 cookie_close_function_t *close; 395 } cookie_io_functions_t; 396 FILE *fopencookie(void *, const char *, cookie_io_functions_t); 397 #endif /* __BSD_VISIBLE */ 398 399 /* 400 * Functions internal to the implementation. 401 */ 402 int __srget(FILE *); 403 int __swbuf(int, FILE *); 404 405 /* 406 * The __sfoo functions are here so that we can 407 * define real function versions in the C library. 408 */ 409 static __inline __always_inline int 410 __sgetc(FILE *_fp) 411 { 412 struct __FILE_public *_p = (struct __FILE_public *)_fp; 413 414 if (--_p->_r < 0) 415 return (__srget(_fp)); 416 else 417 return (*_p->_p++); 418 } 419 420 static __inline __always_inline int 421 __sputc(int _c, FILE *_fp) 422 { 423 struct __FILE_public *_p = (struct __FILE_public *)_fp; 424 425 if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && _c != '\n')) 426 return (*_p->_p++ = _c); 427 else 428 return (__swbuf(_c, _fp)); 429 } 430 431 static __inline __always_inline int 432 __sfeof(FILE *_fp) 433 { 434 struct __FILE_public *_p = (struct __FILE_public *)_fp; 435 436 return ((_p->_flags & __SEOF) != 0); 437 } 438 439 static __inline __always_inline int 440 __sferror(FILE *_fp) 441 { 442 struct __FILE_public *_p = (struct __FILE_public *)_fp; 443 444 return ((_p->_flags & __SERR) != 0); 445 } 446 447 static __inline __always_inline void 448 __sclearerr(FILE *_fp) 449 { 450 struct __FILE_public *_p = (struct __FILE_public *)_fp; 451 452 _p->_flags &= ~(__SERR|__SEOF); 453 } 454 455 static __inline __always_inline int 456 __sfileno(FILE *_fp) 457 { 458 struct __FILE_public *_p = (struct __FILE_public *)_fp; 459 460 return (_p->_fileno); 461 } 462 463 #ifndef __LIBC_ISTHREADED_DECLARED 464 #define __LIBC_ISTHREADED_DECLARED 465 extern int __isthreaded; 466 #endif 467 468 #ifndef __cplusplus 469 #define feof(p) (!__isthreaded ? __sfeof(p) : (feof)(p)) 470 #define ferror(p) (!__isthreaded ? __sferror(p) : (ferror)(p)) 471 #define clearerr(p) (!__isthreaded ? __sclearerr(p) : (clearerr)(p)) 472 473 #if __POSIX_VISIBLE 474 #define fileno(p) (!__isthreaded ? __sfileno(p) : (fileno)(p)) 475 #endif 476 477 #define getc(fp) (!__isthreaded ? __sgetc(fp) : (getc)(fp)) 478 #define putc(x, fp) (!__isthreaded ? __sputc(x, fp) : (putc)(x, fp)) 479 480 #define getchar() getc(stdin) 481 #define putchar(x) putc(x, stdout) 482 483 #if __BSD_VISIBLE 484 /* 485 * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12 486 * B.8.2.7 for the rationale behind the *_unlocked() macros. 487 */ 488 #define clearerr_unlocked(p) __sclearerr(p) 489 #define feof_unlocked(p) __sfeof(p) 490 #define ferror_unlocked(p) __sferror(p) 491 #define fileno_unlocked(p) __sfileno(p) 492 #define fputc_unlocked(s, p) __sputc(s, p) 493 #endif 494 #if __POSIX_VISIBLE >= 199506 495 #define getc_unlocked(fp) __sgetc(fp) 496 #define putc_unlocked(x, fp) __sputc(x, fp) 497 498 #define getchar_unlocked() getc_unlocked(stdin) 499 #define putchar_unlocked(x) putc_unlocked(x, stdout) 500 #endif 501 #endif /* __cplusplus */ 502 503 __END_DECLS 504 #endif /* !_STDIO_H_ */ 505