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