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