stdio.h revision 7d56ccbfaac2b702e4be0f71038efb7f251ef637
1/* $OpenBSD: stdio.h,v 1.35 2006/01/13 18:10:09 miod Exp $ */ 2/* $NetBSD: stdio.h,v 1.18 1996/04/25 18:29:21 jtc Exp $ */ 3 4/*- 5 * Copyright (c) 1990 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Chris Torek. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)stdio.h 5.17 (Berkeley) 6/3/91 36 */ 37 38#ifndef _STDIO_H_ 39#define _STDIO_H_ 40 41#include <sys/cdefs.h> 42#include <sys/_types.h> 43 44#include <stdarg.h> 45#include <stddef.h> 46 47#if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE 48#include <sys/types.h> /* XXX should be removed */ 49#endif 50 51#ifndef _SIZE_T_DEFINED_ 52#define _SIZE_T_DEFINED_ 53typedef unsigned int size_t; 54#endif 55 56#ifndef _SSIZE_T_DEFINED_ 57#define _SSIZE_T_DEFINED_ 58typedef long int ssize_t; 59#endif 60 61#ifndef _OFF_T_DEFINED_ 62#define _OFF_T_DEFINED_ 63typedef long off_t; 64#endif 65 66#define __need_NULL 67#include <stddef.h> 68 69#define _FSTDIO /* Define for new stdio with functions. */ 70 71typedef off_t fpos_t; /* stdio file position type */ 72 73/* 74 * NB: to fit things in six character monocase externals, the stdio 75 * code uses the prefix `__s' for stdio objects, typically followed 76 * by a three-character attempt at a mnemonic. 77 */ 78 79/* stdio buffers */ 80struct __sbuf { 81 unsigned char *_base; 82 int _size; 83}; 84 85/* 86 * stdio state variables. 87 * 88 * The following always hold: 89 * 90 * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR), 91 * _lbfsize is -_bf._size, else _lbfsize is 0 92 * if _flags&__SRD, _w is 0 93 * if _flags&__SWR, _r is 0 94 * 95 * This ensures that the getc and putc macros (or inline functions) never 96 * try to write or read from a file that is in `read' or `write' mode. 97 * (Moreover, they can, and do, automatically switch from read mode to 98 * write mode, and back, on "r+" and "w+" files.) 99 * 100 * _lbfsize is used only to make the inline line-buffered output stream 101 * code as compact as possible. 102 * 103 * _ub, _up, and _ur are used when ungetc() pushes back more characters 104 * than fit in the current _bf, or when ungetc() pushes back a character 105 * that does not match the previous one in _bf. When this happens, 106 * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff 107 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r. 108 * 109 * NOTE: if you change this structure, you also need to update the 110 * std() initializer in findfp.c. 111 */ 112typedef struct __sFILE { 113 unsigned char *_p; /* current position in (some) buffer */ 114 int _r; /* read space left for getc() */ 115 int _w; /* write space left for putc() */ 116 short _flags; /* flags, below; this FILE is free if 0 */ 117 short _file; /* fileno, if Unix descriptor, else -1 */ 118 struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ 119 int _lbfsize; /* 0 or -_bf._size, for inline putc */ 120 121 /* operations */ 122 void *_cookie; /* cookie passed to io functions */ 123 int (*_close)(void *); 124 int (*_read)(void *, char *, int); 125 fpos_t (*_seek)(void *, fpos_t, int); 126 int (*_write)(void *, const char *, int); 127 128 /* extension data, to avoid further ABI breakage */ 129 struct __sbuf _ext; 130 /* data for long sequences of ungetc() */ 131 unsigned char *_up; /* saved _p when _p is doing ungetc data */ 132 int _ur; /* saved _r when _r is counting ungetc data */ 133 134 /* tricks to meet minimum requirements even when malloc() fails */ 135 unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ 136 unsigned char _nbuf[1]; /* guarantee a getc() buffer */ 137 138 /* separate buffer for fgetln() when line crosses buffer boundary */ 139 struct __sbuf _lb; /* buffer for fgetln() */ 140 141 /* Unix stdio files get aligned to block boundaries on fseek() */ 142 int _blksize; /* stat.st_blksize (may be != _bf._size) */ 143 fpos_t _offset; /* current lseek offset */ 144} FILE; 145 146__BEGIN_DECLS 147extern FILE __sF[]; 148__END_DECLS 149 150#define __SLBF 0x0001 /* line buffered */ 151#define __SNBF 0x0002 /* unbuffered */ 152#define __SRD 0x0004 /* OK to read */ 153#define __SWR 0x0008 /* OK to write */ 154 /* RD and WR are never simultaneously asserted */ 155#define __SRW 0x0010 /* open for reading & writing */ 156#define __SEOF 0x0020 /* found EOF */ 157#define __SERR 0x0040 /* found error */ 158#define __SMBF 0x0080 /* _buf is from malloc */ 159#define __SAPP 0x0100 /* fdopen()ed in append mode */ 160#define __SSTR 0x0200 /* this is an sprintf/snprintf string */ 161#define __SOPT 0x0400 /* do fseek() optimisation */ 162#define __SNPT 0x0800 /* do not do fseek() optimisation */ 163#define __SOFF 0x1000 /* set iff _offset is in fact correct */ 164#define __SMOD 0x2000 /* true => fgetln modified _p text */ 165#define __SALC 0x4000 /* allocate string space dynamically */ 166#define __SIGN 0x8000 /* ignore this file in _fwalk */ 167 168/* 169 * The following three definitions are for ANSI C, which took them 170 * from System V, which brilliantly took internal interface macros and 171 * made them official arguments to setvbuf(), without renaming them. 172 * Hence, these ugly _IOxxx names are *supposed* to appear in user code. 173 * 174 * Although numbered as their counterparts above, the implementation 175 * does not rely on this. 176 */ 177#define _IOFBF 0 /* setvbuf should set fully buffered */ 178#define _IOLBF 1 /* setvbuf should set line buffered */ 179#define _IONBF 2 /* setvbuf should set unbuffered */ 180 181#define BUFSIZ 1024 /* size of buffer used by setbuf */ 182 183#define EOF (-1) 184 185/* 186 * FOPEN_MAX is a minimum maximum, and should be the number of descriptors 187 * that the kernel can provide without allocation of a resource that can 188 * fail without the process sleeping. Do not use this for anything. 189 */ 190#define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */ 191#define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */ 192 193/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */ 194#if __BSD_VISIBLE || __XPG_VISIBLE 195#define P_tmpdir "/tmp/" 196#endif 197#define L_tmpnam 1024 /* XXX must be == PATH_MAX */ 198#define TMP_MAX 308915776 199 200#ifndef SEEK_SET 201#define SEEK_SET 0 /* set file offset to offset */ 202#endif 203#ifndef SEEK_CUR 204#define SEEK_CUR 1 /* set file offset to current plus offset */ 205#endif 206#ifndef SEEK_END 207#define SEEK_END 2 /* set file offset to EOF plus offset */ 208#endif 209 210#define stdin (&__sF[0]) 211#define stdout (&__sF[1]) 212#define stderr (&__sF[2]) 213 214/* 215 * Functions defined in ANSI C standard. 216 */ 217__BEGIN_DECLS 218void clearerr(FILE *); 219int fclose(FILE *); 220int feof(FILE *); 221int ferror(FILE *); 222int fflush(FILE *); 223int fgetc(FILE *); 224int fgetpos(FILE *, fpos_t *); 225char *fgets(char *, int, FILE *); 226FILE *fopen(const char *, const char *); 227int fprintf(FILE *, const char *, ...) 228 __attribute__((__format__ (printf, 2, 3))) 229 __attribute__((__nonnull__ (2))); 230int fputc(int, FILE *); 231int fputs(const char *, FILE *); 232size_t fread(void *, size_t, size_t, FILE *); 233FILE *freopen(const char *, const char *, FILE *); 234int fscanf(FILE *, const char *, ...) 235 __attribute__ ((__format__ (scanf, 2, 3))) 236 __attribute__ ((__nonnull__ (2))); 237int fseek(FILE *, long, int); 238int fseeko(FILE *, off_t, int); 239int fsetpos(FILE *, const fpos_t *); 240long ftell(FILE *); 241off_t ftello(FILE *); 242size_t fwrite(const void *, size_t, size_t, FILE *); 243int getc(FILE *); 244int getchar(void); 245ssize_t getdelim(char ** __restrict, size_t * __restrict, int, 246 FILE * __restrict); 247ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict); 248char *gets(char *); 249#if __BSD_VISIBLE && !defined(__SYS_ERRLIST) 250#define __SYS_ERRLIST 251 252extern int sys_nerr; /* perror(3) external variables */ 253extern char *sys_errlist[]; 254#endif 255void perror(const char *); 256int printf(const char *, ...) 257 __attribute__((__format__ (printf, 1, 2))) 258 __attribute__((__nonnull__ (1))); 259int putc(int, FILE *); 260int putchar(int); 261int puts(const char *); 262int remove(const char *); 263int rename(const char *, const char *); 264void rewind(FILE *); 265int scanf(const char *, ...) 266 __attribute__ ((__format__ (scanf, 1, 2))) 267 __attribute__ ((__nonnull__ (1))); 268void setbuf(FILE *, char *); 269int setvbuf(FILE *, char *, int, size_t); 270int sprintf(char *, const char *, ...) 271 __attribute__((__format__ (printf, 2, 3))) 272 __attribute__((__nonnull__ (2))); 273int sscanf(const char *, const char *, ...) 274 __attribute__ ((__format__ (scanf, 2, 3))) 275 __attribute__ ((__nonnull__ (2))); 276FILE *tmpfile(void); 277char *tmpnam(char *); 278int ungetc(int, FILE *); 279int vfprintf(FILE *, const char *, __va_list) 280 __attribute__((__format__ (printf, 2, 0))) 281 __attribute__((__nonnull__ (2))); 282int vprintf(const char *, __va_list) 283 __attribute__((__format__ (printf, 1, 0))) 284 __attribute__((__nonnull__ (1))); 285int vsprintf(char *, const char *, __va_list) 286 __attribute__((__format__ (printf, 2, 0))) 287 __attribute__((__nonnull__ (2))); 288 289#if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE 290int snprintf(char *, size_t, const char *, ...) 291 __attribute__((__format__ (printf, 3, 4))) 292 __attribute__((__nonnull__ (3))); 293int vfscanf(FILE *, const char *, __va_list) 294 __attribute__((__format__ (scanf, 2, 0))) 295 __attribute__((__nonnull__ (2))); 296int vscanf(const char *, __va_list) 297 __attribute__((__format__ (scanf, 1, 0))) 298 __attribute__((__nonnull__ (1))); 299int vsnprintf(char *, size_t, const char *, __va_list) 300 __attribute__((__format__ (printf, 3, 0))) 301 __attribute__((__nonnull__ (3))); 302int vsscanf(const char *, const char *, __va_list) 303 __attribute__((__format__ (scanf, 2, 0))) 304 __attribute__((__nonnull__ (2))); 305#endif /* __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE */ 306 307__END_DECLS 308 309 310/* 311 * Functions defined in POSIX 1003.1. 312 */ 313#if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE 314#define L_ctermid 1024 /* size for ctermid(); PATH_MAX */ 315#define L_cuserid 9 /* size for cuserid(); UT_NAMESIZE + 1 */ 316 317__BEGIN_DECLS 318#if 0 /* MISSING FROM BIONIC */ 319char *ctermid(char *); 320char *cuserid(char *); 321#endif /* MISSING */ 322FILE *fdopen(int, const char *); 323int fileno(FILE *); 324 325#if (__POSIX_VISIBLE >= 199209) 326int pclose(FILE *); 327FILE *popen(const char *, const char *); 328#endif 329 330#if __POSIX_VISIBLE >= 199506 331void flockfile(FILE *); 332int ftrylockfile(FILE *); 333void funlockfile(FILE *); 334 335/* 336 * These are normally used through macros as defined below, but POSIX 337 * requires functions as well. 338 */ 339int getc_unlocked(FILE *); 340int getchar_unlocked(void); 341int putc_unlocked(int, FILE *); 342int putchar_unlocked(int); 343#endif /* __POSIX_VISIBLE >= 199506 */ 344 345#if __XPG_VISIBLE 346char *tempnam(const char *, const char *); 347#endif 348__END_DECLS 349 350#endif /* __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE */ 351 352/* 353 * Routines that are purely local. 354 */ 355#if __BSD_VISIBLE 356__BEGIN_DECLS 357int asprintf(char **, const char *, ...) 358 __attribute__((__format__ (printf, 2, 3))) 359 __attribute__((__nonnull__ (2))); 360char *fgetln(FILE *, size_t *); 361int fpurge(FILE *); 362int getw(FILE *); 363int putw(int, FILE *); 364void setbuffer(FILE *, char *, int); 365int setlinebuf(FILE *); 366int vasprintf(char **, const char *, __va_list) 367 __attribute__((__format__ (printf, 2, 0))) 368 __attribute__((__nonnull__ (2))); 369__END_DECLS 370 371/* 372 * Stdio function-access interface. 373 */ 374__BEGIN_DECLS 375FILE *funopen(const void *, 376 int (*)(void *, char *, int), 377 int (*)(void *, const char *, int), 378 fpos_t (*)(void *, fpos_t, int), 379 int (*)(void *)); 380__END_DECLS 381#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) 382#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) 383#endif /* __BSD_VISIBLE */ 384 385/* 386 * Functions internal to the implementation. 387 */ 388__BEGIN_DECLS 389int __srget(FILE *); 390int __swbuf(int, FILE *); 391__END_DECLS 392 393/* 394 * The __sfoo macros are here so that we can 395 * define function versions in the C library. 396 */ 397#define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++)) 398#if defined(__GNUC__) 399static __inline int __sputc(int _c, FILE *_p) { 400 if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n')) 401 return (*_p->_p++ = _c); 402 else 403 return (__swbuf(_c, _p)); 404} 405#else 406/* 407 * This has been tuned to generate reasonable code on the vax using pcc. 408 */ 409#define __sputc(c, p) \ 410 (--(p)->_w < 0 ? \ 411 (p)->_w >= (p)->_lbfsize ? \ 412 (*(p)->_p = (c)), *(p)->_p != '\n' ? \ 413 (int)*(p)->_p++ : \ 414 __swbuf('\n', p) : \ 415 __swbuf((int)(c), p) : \ 416 (*(p)->_p = (c), (int)*(p)->_p++)) 417#endif 418 419#define __sfeof(p) (((p)->_flags & __SEOF) != 0) 420#define __sferror(p) (((p)->_flags & __SERR) != 0) 421#define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF))) 422#define __sfileno(p) ((p)->_file) 423 424extern int __isthreaded; 425 426#define feof(p) (!__isthreaded ? __sfeof(p) : (feof)(p)) 427#define ferror(p) (!__isthreaded ? __sferror(p) : (ferror)(p)) 428#define clearerr(p) (!__isthreaded ? __sclearerr(p) : (clearerr)(p)) 429 430#if __POSIX_VISIBLE 431#define fileno(p) (!__isthreaded ? __sfileno(p) : (fileno)(p)) 432#endif 433 434#define getc(fp) (!__isthreaded ? __sgetc(fp) : (getc)(fp)) 435 436#if __BSD_VISIBLE 437/* 438 * The macro implementations of putc and putc_unlocked are not 439 * fully POSIX compliant; they do not set errno on failure 440 */ 441#define putc(x, fp) (!__isthreaded ? __sputc(x, fp) : (putc)(x, fp)) 442#endif /* __BSD_VISIBLE */ 443 444#ifndef lint 445#if __POSIX_VISIBLE >= 199506 446#define getc_unlocked(fp) __sgetc(fp) 447/* 448 * The macro implementations of putc and putc_unlocked are not 449 * fully POSIX compliant; they do not set errno on failure 450 */ 451#if __BSD_VISIBLE 452#define putc_unlocked(x, fp) __sputc(x, fp) 453#endif /* __BSD_VISIBLE */ 454#endif /* __POSIX_VISIBLE >= 199506 */ 455#endif /* lint */ 456 457#define getchar() getc(stdin) 458#define putchar(x) putc(x, stdout) 459#define getchar_unlocked() getc_unlocked(stdin) 460#define putchar_unlocked(c) putc_unlocked(c, stdout) 461 462#ifdef _GNU_SOURCE 463/* 464 * glibc defines dprintf(int, const char*, ...), which is poorly named 465 * and likely to conflict with locally defined debugging printfs 466 * fdprintf is a better name, and some programs that use fdprintf use a 467 * #define fdprintf dprintf for compatibility 468 */ 469__BEGIN_DECLS 470int fdprintf(int, const char*, ...) 471 __attribute__((__format__ (printf, 2, 3))) 472 __attribute__((__nonnull__ (2))); 473int vfdprintf(int, const char*, __va_list) 474 __attribute__((__format__ (printf, 2, 0))) 475 __attribute__((__nonnull__ (2))); 476__END_DECLS 477#endif /* _GNU_SOURCE */ 478 479#if defined(__BIONIC_FORTIFY_INLINE) 480 481__BIONIC_FORTIFY_INLINE 482__attribute__((__format__ (printf, 3, 0))) 483__attribute__((__nonnull__ (3))) 484int vsnprintf(char *dest, size_t size, const char *format, __va_list ap) 485{ 486 return __builtin___vsnprintf_chk(dest, size, 0, 487 __builtin_object_size(dest, 0), format, ap); 488} 489 490__BIONIC_FORTIFY_INLINE 491__attribute__((__format__ (printf, 2, 0))) 492__attribute__((__nonnull__ (2))) 493int vsprintf(char *dest, const char *format, __va_list ap) 494{ 495 return __builtin___vsprintf_chk(dest, 0, 496 __builtin_object_size(dest, 0), format, ap); 497} 498 499__BIONIC_FORTIFY_INLINE 500__attribute__((__format__ (printf, 3, 4))) 501__attribute__((__nonnull__ (3))) 502int snprintf(char *str, size_t size, const char *format, ...) 503{ 504 return __builtin___snprintf_chk(str, size, 0, 505 __builtin_object_size(str, 0), format, __builtin_va_arg_pack()); 506} 507 508__BIONIC_FORTIFY_INLINE 509__attribute__((__format__ (printf, 2, 3))) 510__attribute__((__nonnull__ (2))) 511int sprintf(char *dest, const char *format, ...) 512{ 513 return __builtin___sprintf_chk(dest, 0, 514 __builtin_object_size(dest, 0), format, __builtin_va_arg_pack()); 515} 516 517extern char *__fgets_real(char *, int, FILE *) 518 __asm__(__USER_LABEL_PREFIX__ "fgets"); 519extern void __fgets_too_big_error() 520 __attribute__((__error__("fgets called with size bigger than buffer"))); 521extern void __fgets_too_small_error() 522 __attribute__((__error__("fgets called with size less than zero"))); 523extern char *__fgets_chk(char *, int, FILE *, size_t); 524 525__BIONIC_FORTIFY_INLINE 526char *fgets(char *dest, int size, FILE *stream) 527{ 528 size_t bos = __builtin_object_size(dest, 0); 529 530 // Compiler can prove, at compile time, that the passed in size 531 // is always negative. Force a compiler error. 532 if (__builtin_constant_p(size) && (size < 0)) { 533 __fgets_too_small_error(); 534 } 535 536 // Compiler doesn't know destination size. Don't call __fgets_chk 537 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { 538 return __fgets_real(dest, size, stream); 539 } 540 541 // Compiler can prove, at compile time, that the passed in size 542 // is always <= the actual object size. Don't call __fgets_chk 543 if (__builtin_constant_p(size) && (size <= bos)) { 544 return __fgets_real(dest, size, stream); 545 } 546 547 // Compiler can prove, at compile time, that the passed in size 548 // is always > the actual object size. Force a compiler error. 549 if (__builtin_constant_p(size) && (size > bos)) { 550 __fgets_too_big_error(); 551 } 552 553 return __fgets_chk(dest, size, stream, bos); 554} 555 556#endif /* defined(__BIONIC_FORTIFY_INLINE) */ 557 558#endif /* _STDIO_H_ */ 559