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