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