1*bcac9d59Sguenther /* $OpenBSD: stdio.h,v 1.56 2024/08/07 05:15:28 guenther Exp $ */ 2757841a6Sderaadt /* $NetBSD: stdio.h,v 1.18 1996/04/25 18:29:21 jtc Exp $ */ 3df930be7Sderaadt 4df930be7Sderaadt /*- 5df930be7Sderaadt * Copyright (c) 1990 The Regents of the University of California. 6df930be7Sderaadt * All rights reserved. 7df930be7Sderaadt * 8df930be7Sderaadt * This code is derived from software contributed to Berkeley by 9df930be7Sderaadt * Chris Torek. 10df930be7Sderaadt * 11df930be7Sderaadt * Redistribution and use in source and binary forms, with or without 12df930be7Sderaadt * modification, are permitted provided that the following conditions 13df930be7Sderaadt * are met: 14df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright 15df930be7Sderaadt * notice, this list of conditions and the following disclaimer. 16df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright 17df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the 18df930be7Sderaadt * documentation and/or other materials provided with the distribution. 19e33d3bd3Smillert * 3. Neither the name of the University nor the names of its contributors 20df930be7Sderaadt * may be used to endorse or promote products derived from this software 21df930be7Sderaadt * without specific prior written permission. 22df930be7Sderaadt * 23df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33df930be7Sderaadt * SUCH DAMAGE. 34df930be7Sderaadt * 35df930be7Sderaadt * @(#)stdio.h 5.17 (Berkeley) 6/3/91 36df930be7Sderaadt */ 37df930be7Sderaadt 38df930be7Sderaadt #ifndef _STDIO_H_ 39df930be7Sderaadt #define _STDIO_H_ 40df930be7Sderaadt 4152a0e603Smillert #include <sys/cdefs.h> 426ecde746Smillert #include <sys/_null.h> 43c916d948Smillert #include <sys/_types.h> 4452a0e603Smillert 4552a0e603Smillert #if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE 46c916d948Smillert #include <sys/types.h> /* XXX should be removed */ 47df930be7Sderaadt #endif 48df930be7Sderaadt 49c916d948Smillert #ifndef _SIZE_T_DEFINED_ 50c916d948Smillert #define _SIZE_T_DEFINED_ 51c916d948Smillert typedef __size_t size_t; 52df930be7Sderaadt #endif 53df930be7Sderaadt 54c916d948Smillert #ifndef _OFF_T_DEFINED_ 55c916d948Smillert #define _OFF_T_DEFINED_ 56c916d948Smillert typedef __off_t off_t; 5704204a07Smillert #endif 5804204a07Smillert 59df930be7Sderaadt #define _FSTDIO /* Define for new stdio with functions. */ 60df930be7Sderaadt 6104204a07Smillert typedef off_t fpos_t; /* stdio file position type */ 6204204a07Smillert 63df930be7Sderaadt /* 64df930be7Sderaadt * NB: to fit things in six character monocase externals, the stdio 65df930be7Sderaadt * code uses the prefix `__s' for stdio objects, typically followed 66df930be7Sderaadt * by a three-character attempt at a mnemonic. 67df930be7Sderaadt */ 68df930be7Sderaadt 69df930be7Sderaadt /* stdio buffers */ 70df930be7Sderaadt struct __sbuf { 71df930be7Sderaadt unsigned char *_base; 72df930be7Sderaadt int _size; 73df930be7Sderaadt }; 74df930be7Sderaadt 75df930be7Sderaadt /* 76df930be7Sderaadt * stdio state variables. 77df930be7Sderaadt * 78df930be7Sderaadt * The following always hold: 79df930be7Sderaadt * 80df930be7Sderaadt * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR), 81df930be7Sderaadt * _lbfsize is -_bf._size, else _lbfsize is 0 82df930be7Sderaadt * if _flags&__SRD, _w is 0 83df930be7Sderaadt * if _flags&__SWR, _r is 0 84df930be7Sderaadt * 85df930be7Sderaadt * This ensures that the getc and putc macros (or inline functions) never 86df930be7Sderaadt * try to write or read from a file that is in `read' or `write' mode. 87df930be7Sderaadt * (Moreover, they can, and do, automatically switch from read mode to 88df930be7Sderaadt * write mode, and back, on "r+" and "w+" files.) 89df930be7Sderaadt * 90df930be7Sderaadt * _lbfsize is used only to make the inline line-buffered output stream 91df930be7Sderaadt * code as compact as possible. 92df930be7Sderaadt * 93df930be7Sderaadt * _ub, _up, and _ur are used when ungetc() pushes back more characters 94df930be7Sderaadt * than fit in the current _bf, or when ungetc() pushes back a character 95df930be7Sderaadt * that does not match the previous one in _bf. When this happens, 96df930be7Sderaadt * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff 97df930be7Sderaadt * _ub._base!=NULL) and _up and _ur save the current values of _p and _r. 98df930be7Sderaadt */ 99df930be7Sderaadt typedef struct __sFILE { 100df930be7Sderaadt unsigned char *_p; /* current position in (some) buffer */ 101df930be7Sderaadt int _r; /* read space left for getc() */ 102df930be7Sderaadt int _w; /* write space left for putc() */ 103df930be7Sderaadt short _flags; /* flags, below; this FILE is free if 0 */ 104df930be7Sderaadt short _file; /* fileno, if Unix descriptor, else -1 */ 105df930be7Sderaadt struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ 106df930be7Sderaadt int _lbfsize; /* 0 or -_bf._size, for inline putc */ 107df930be7Sderaadt 108df930be7Sderaadt /* operations */ 109df930be7Sderaadt void *_cookie; /* cookie passed to io functions */ 110c72b5b24Smillert int (*_close)(void *); 111c72b5b24Smillert int (*_read)(void *, char *, int); 112c72b5b24Smillert fpos_t (*_seek)(void *, fpos_t, int); 113c72b5b24Smillert int (*_write)(void *, const char *, int); 114df930be7Sderaadt 115685aea3cSespie /* extension data, to avoid further ABI breakage */ 116685aea3cSespie struct __sbuf _ext; 117685aea3cSespie /* data for long sequences of ungetc() */ 118df930be7Sderaadt unsigned char *_up; /* saved _p when _p is doing ungetc data */ 119df930be7Sderaadt int _ur; /* saved _r when _r is counting ungetc data */ 120df930be7Sderaadt 121df930be7Sderaadt /* tricks to meet minimum requirements even when malloc() fails */ 122df930be7Sderaadt unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ 123df930be7Sderaadt unsigned char _nbuf[1]; /* guarantee a getc() buffer */ 124df930be7Sderaadt 125df930be7Sderaadt /* separate buffer for fgetln() when line crosses buffer boundary */ 126df930be7Sderaadt struct __sbuf _lb; /* buffer for fgetln() */ 127df930be7Sderaadt 128df930be7Sderaadt /* Unix stdio files get aligned to block boundaries on fseek() */ 129df930be7Sderaadt int _blksize; /* stat.st_blksize (may be != _bf._size) */ 130df930be7Sderaadt fpos_t _offset; /* current lseek offset */ 131df930be7Sderaadt } FILE; 132df930be7Sderaadt 133df930be7Sderaadt __BEGIN_DECLS 134df930be7Sderaadt extern FILE __sF[]; 135df930be7Sderaadt __END_DECLS 136df930be7Sderaadt 137df930be7Sderaadt #define __SLBF 0x0001 /* line buffered */ 138df930be7Sderaadt #define __SNBF 0x0002 /* unbuffered */ 139df930be7Sderaadt #define __SRD 0x0004 /* OK to read */ 140df930be7Sderaadt #define __SWR 0x0008 /* OK to write */ 141df930be7Sderaadt /* RD and WR are never simultaneously asserted */ 142df930be7Sderaadt #define __SRW 0x0010 /* open for reading & writing */ 143df930be7Sderaadt #define __SEOF 0x0020 /* found EOF */ 144df930be7Sderaadt #define __SERR 0x0040 /* found error */ 145df930be7Sderaadt #define __SMBF 0x0080 /* _buf is from malloc */ 146df930be7Sderaadt #define __SAPP 0x0100 /* fdopen()ed in append mode */ 147df930be7Sderaadt #define __SSTR 0x0200 /* this is an sprintf/snprintf string */ 148df930be7Sderaadt #define __SOPT 0x0400 /* do fseek() optimisation */ 149df930be7Sderaadt #define __SNPT 0x0800 /* do not do fseek() optimisation */ 150df930be7Sderaadt #define __SOFF 0x1000 /* set iff _offset is in fact correct */ 151df930be7Sderaadt #define __SMOD 0x2000 /* true => fgetln modified _p text */ 15278f54e55Smillert #define __SALC 0x4000 /* allocate string space dynamically */ 153c5acf43aSkurt #define __SIGN 0x8000 /* ignore this file in _fwalk */ 154df930be7Sderaadt 155df930be7Sderaadt /* 156df930be7Sderaadt * The following three definitions are for ANSI C, which took them 157df930be7Sderaadt * from System V, which brilliantly took internal interface macros and 158df930be7Sderaadt * made them official arguments to setvbuf(), without renaming them. 159df930be7Sderaadt * Hence, these ugly _IOxxx names are *supposed* to appear in user code. 160df930be7Sderaadt * 161df930be7Sderaadt * Although numbered as their counterparts above, the implementation 162df930be7Sderaadt * does not rely on this. 163df930be7Sderaadt */ 164df930be7Sderaadt #define _IOFBF 0 /* setvbuf should set fully buffered */ 165df930be7Sderaadt #define _IOLBF 1 /* setvbuf should set line buffered */ 166df930be7Sderaadt #define _IONBF 2 /* setvbuf should set unbuffered */ 167df930be7Sderaadt 168df930be7Sderaadt #define BUFSIZ 1024 /* size of buffer used by setbuf */ 1695d8e0bc2Smillert 170da2317caSmillert #define EOF (-1) 171df930be7Sderaadt 172df930be7Sderaadt /* 173df930be7Sderaadt * FOPEN_MAX is a minimum maximum, and should be the number of descriptors 174df930be7Sderaadt * that the kernel can provide without allocation of a resource that can 175df930be7Sderaadt * fail without the process sleeping. Do not use this for anything. 176df930be7Sderaadt */ 177df930be7Sderaadt #define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */ 178df930be7Sderaadt #define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */ 179df930be7Sderaadt 180df930be7Sderaadt /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */ 18152a0e603Smillert #if __BSD_VISIBLE || __XPG_VISIBLE 1825eac0c95Smillert #define P_tmpdir "/tmp/" 183df930be7Sderaadt #endif 184df930be7Sderaadt #define L_tmpnam 1024 /* XXX must be == PATH_MAX */ 1857a4e5fc4Stedu #define TMP_MAX 0x7fffffff /* more, but don't overflow int */ 186df930be7Sderaadt 187df930be7Sderaadt #ifndef SEEK_SET 188df930be7Sderaadt #define SEEK_SET 0 /* set file offset to offset */ 189df930be7Sderaadt #endif 190df930be7Sderaadt #ifndef SEEK_CUR 191df930be7Sderaadt #define SEEK_CUR 1 /* set file offset to current plus offset */ 192df930be7Sderaadt #endif 193df930be7Sderaadt #ifndef SEEK_END 194df930be7Sderaadt #define SEEK_END 2 /* set file offset to EOF plus offset */ 195df930be7Sderaadt #endif 196df930be7Sderaadt 197df930be7Sderaadt #define stdin (&__sF[0]) 198df930be7Sderaadt #define stdout (&__sF[1]) 199df930be7Sderaadt #define stderr (&__sF[2]) 200df930be7Sderaadt 201df930be7Sderaadt /* 202df930be7Sderaadt * Functions defined in ANSI C standard. 203df930be7Sderaadt */ 204df930be7Sderaadt __BEGIN_DECLS 205c72b5b24Smillert void clearerr(FILE *); 206f2c0f4acSbrad #if __POSIX_VISIBLE >= 200809 207464c6a2fSnaddy int dprintf(int, const char * __restrict, ...) 208464c6a2fSnaddy __attribute__((__format__ (printf, 2, 3))) 209464c6a2fSnaddy __attribute__((__nonnull__ (2))); 210f2c0f4acSbrad #endif 211c72b5b24Smillert int fclose(FILE *); 212c72b5b24Smillert int feof(FILE *); 213c72b5b24Smillert int ferror(FILE *); 214c72b5b24Smillert int fflush(FILE *); 215c72b5b24Smillert int fgetc(FILE *); 216c72b5b24Smillert int fgetpos(FILE *, fpos_t *); 21789b95c1cSavsm char *fgets(char *, int, FILE *) 21889b95c1cSavsm __attribute__((__bounded__ (__string__,1,2))); 219c72b5b24Smillert FILE *fopen(const char *, const char *); 220*bcac9d59Sguenther int fprintf(FILE *, const char * __restrict, ...); 221c72b5b24Smillert int fputc(int, FILE *); 222c72b5b24Smillert int fputs(const char *, FILE *); 22389b95c1cSavsm size_t fread(void *, size_t, size_t, FILE *) 22489b95c1cSavsm __attribute__((__bounded__ (__size__,1,3,2))); 225c72b5b24Smillert FILE *freopen(const char *, const char *, FILE *); 226c72b5b24Smillert int fscanf(FILE *, const char *, ...); 227c72b5b24Smillert int fseek(FILE *, long, int); 228c72b5b24Smillert int fseeko(FILE *, off_t, int); 229c72b5b24Smillert int fsetpos(FILE *, const fpos_t *); 230c72b5b24Smillert long ftell(FILE *); 231c72b5b24Smillert off_t ftello(FILE *); 23289b95c1cSavsm size_t fwrite(const void *, size_t, size_t, FILE *) 23389b95c1cSavsm __attribute__((__bounded__ (__size__,1,3,2))); 234c72b5b24Smillert int getc(FILE *); 235c72b5b24Smillert int getchar(void); 236dc893751Sfgsch #if __POSIX_VISIBLE >= 200809 237dc893751Sfgsch ssize_t getdelim(char ** __restrict, size_t * __restrict, int, 238dc893751Sfgsch FILE * __restrict); 239dc893751Sfgsch ssize_t getline(char ** __restrict, size_t * __restrict, 240dc893751Sfgsch FILE * __restrict); 241dc893751Sfgsch #endif 24252a0e603Smillert #if __BSD_VISIBLE && !defined(__SYS_ERRLIST) 243af3e3a54Sdownsj #define __SYS_ERRLIST 244af3e3a54Sdownsj 245af3e3a54Sdownsj extern int sys_nerr; /* perror(3) external variables */ 2461d96fa34Sderaadt extern char *sys_errlist[]; 247af3e3a54Sdownsj #endif 248c72b5b24Smillert void perror(const char *); 249*bcac9d59Sguenther int printf(const char * __restrict, ...); 250c72b5b24Smillert int putc(int, FILE *); 251c72b5b24Smillert int putchar(int); 252c72b5b24Smillert int puts(const char *); 253c72b5b24Smillert int remove(const char *); 254c72b5b24Smillert int rename(const char *, const char *); 255f02fac78Smatthew #if __POSIX_VISIBLE >= 200809 256f02fac78Smatthew int renameat(int, const char *, int, const char *); 257f02fac78Smatthew #endif 258c72b5b24Smillert void rewind(FILE *); 259c72b5b24Smillert int scanf(const char *, ...); 260c72b5b24Smillert void setbuf(FILE *, char *); 261c72b5b24Smillert int setvbuf(FILE *, char *, int, size_t); 262*bcac9d59Sguenther int sprintf(char * __restrict, const char * __restrict, ...); 263c72b5b24Smillert int sscanf(const char *, const char *, ...); 264c72b5b24Smillert FILE *tmpfile(void); 265c72b5b24Smillert char *tmpnam(char *); 266c72b5b24Smillert int ungetc(int, FILE *); 267*bcac9d59Sguenther int vfprintf(FILE *, const char * __restrict, __va_list); 268*bcac9d59Sguenther int vprintf(const char * __restrict, __va_list); 269*bcac9d59Sguenther int vsprintf(char * __restrict, const char * __restrict, __va_list); 270f2c0f4acSbrad #if __POSIX_VISIBLE >= 200809 271464c6a2fSnaddy int vdprintf(int, const char * __restrict, __va_list) 272464c6a2fSnaddy __attribute__((__format__ (printf, 2, 0))) 273464c6a2fSnaddy __attribute__((__nonnull__ (2))); 274f2c0f4acSbrad #endif 27552a0e603Smillert 276abd93d17Smillert #if __ISO_C_VISIBLE >= 1999 || __XPG_VISIBLE >= 500 || __BSD_VISIBLE 277*bcac9d59Sguenther int snprintf(char * __restrict, size_t, const char * __restrict, ...) 27852a0e603Smillert __attribute__((__format__ (printf, 3, 4))) 27952a0e603Smillert __attribute__((__nonnull__ (3))) 28052a0e603Smillert __attribute__((__bounded__ (__string__,1,2))); 281*bcac9d59Sguenther int vsnprintf(char * __restrict, size_t, const char * __restrict, 282*bcac9d59Sguenther __va_list) 283abd93d17Smillert __attribute__((__format__ (printf, 3, 0))) 284abd93d17Smillert __attribute__((__nonnull__ (3))) 285abd93d17Smillert __attribute__((__bounded__(__string__,1,2))); 286abd93d17Smillert #endif /* __ISO_C_VISIBLE >= 1999 || __XPG_VISIBLE >= 500 || __BSD_VISIBLE */ 287abd93d17Smillert 288abd93d17Smillert #if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE 289c916d948Smillert int vfscanf(FILE *, const char *, __va_list) 29052a0e603Smillert __attribute__((__format__ (scanf, 2, 0))) 29152a0e603Smillert __attribute__((__nonnull__ (2))); 292c916d948Smillert int vscanf(const char *, __va_list) 29352a0e603Smillert __attribute__((__format__ (scanf, 1, 0))) 29452a0e603Smillert __attribute__((__nonnull__ (1))); 295c916d948Smillert int vsscanf(const char *, const char *, __va_list) 29652a0e603Smillert __attribute__((__format__ (scanf, 2, 0))) 29752a0e603Smillert __attribute__((__nonnull__ (2))); 29852a0e603Smillert #endif /* __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE */ 29952a0e603Smillert 300df930be7Sderaadt __END_DECLS 301df930be7Sderaadt 30252a0e603Smillert 303df930be7Sderaadt /* 304df930be7Sderaadt * Functions defined in POSIX 1003.1. 305df930be7Sderaadt */ 30652a0e603Smillert #if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE 307df930be7Sderaadt #define L_ctermid 1024 /* size for ctermid(); PATH_MAX */ 308df930be7Sderaadt 309df930be7Sderaadt __BEGIN_DECLS 310c72b5b24Smillert char *ctermid(char *); 311c72b5b24Smillert FILE *fdopen(int, const char *); 312c72b5b24Smillert int fileno(FILE *); 31352a0e603Smillert 31452a0e603Smillert #if __POSIX_VISIBLE >= 199209 31552a0e603Smillert int pclose(FILE *); 31652a0e603Smillert FILE *popen(const char *, const char *); 31752a0e603Smillert #endif 31852a0e603Smillert 31952a0e603Smillert #if __POSIX_VISIBLE >= 199506 320c72b5b24Smillert void flockfile(FILE *); 321c72b5b24Smillert int ftrylockfile(FILE *); 322c72b5b24Smillert void funlockfile(FILE *); 32352a0e603Smillert 32452a0e603Smillert /* 32552a0e603Smillert * These are normally used through macros as defined below, but POSIX 32652a0e603Smillert * requires functions as well. 32752a0e603Smillert */ 328c72b5b24Smillert int getc_unlocked(FILE *); 329c72b5b24Smillert int getchar_unlocked(void); 33052a0e603Smillert int putc_unlocked(int, FILE *); 331c72b5b24Smillert int putchar_unlocked(int); 33252a0e603Smillert #endif /* __POSIX_VISIBLE >= 199506 */ 33352a0e603Smillert 334236998c6Seric #if __POSIX_VISIBLE >= 200809 335236998c6Seric FILE *fmemopen(void *, size_t, const char *); 336236998c6Seric FILE *open_memstream(char **, size_t *); 337236998c6Seric #endif /* __POSIX_VISIBLE >= 200809 */ 338236998c6Seric 33952a0e603Smillert #if __XPG_VISIBLE 34052a0e603Smillert char *tempnam(const char *, const char *); 34152a0e603Smillert #endif 342*bcac9d59Sguenther 343*bcac9d59Sguenther #if __POSIX_VISIBLE >= 202405 || __BSD_VISIBLE 344*bcac9d59Sguenther int asprintf(char ** __restrict, const char * __restrict, ...) 345*bcac9d59Sguenther __attribute__((__format__ (printf, 2, 3))) 346*bcac9d59Sguenther __attribute__((__nonnull__ (2))); 347*bcac9d59Sguenther int vasprintf(char ** __restrict, const char * __restrict, __va_list) 348*bcac9d59Sguenther __attribute__((__format__ (printf, 2, 0))) 349*bcac9d59Sguenther __attribute__((__nonnull__ (2))); 350*bcac9d59Sguenther #endif /* __POSIX_VISIBLE >= 202405 || __BSD_VISIBLE */ 351df930be7Sderaadt __END_DECLS 35292efb735Sd 35352a0e603Smillert #endif /* __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE */ 354df930be7Sderaadt 355df930be7Sderaadt /* 356df930be7Sderaadt * Routines that are purely local. 357df930be7Sderaadt */ 35852a0e603Smillert #if __BSD_VISIBLE 359df930be7Sderaadt __BEGIN_DECLS 360c72b5b24Smillert char *fgetln(FILE *, size_t *); 361c72b5b24Smillert int fpurge(FILE *); 362c72b5b24Smillert int getw(FILE *); 363c72b5b24Smillert int putw(int, FILE *); 364c72b5b24Smillert void setbuffer(FILE *, char *, int); 365c72b5b24Smillert int setlinebuf(FILE *); 366df930be7Sderaadt __END_DECLS 367df930be7Sderaadt 368df930be7Sderaadt /* 369df930be7Sderaadt * Stdio function-access interface. 370df930be7Sderaadt */ 371df930be7Sderaadt __BEGIN_DECLS 372f3c3a9c6Smillert FILE *funopen(const void *, 373df930be7Sderaadt int (*)(void *, char *, int), 374df930be7Sderaadt int (*)(void *, const char *, int), 3756a970984Smillert off_t (*)(void *, off_t, int), 376f3c3a9c6Smillert int (*)(void *)); 377df930be7Sderaadt __END_DECLS 378df930be7Sderaadt #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) 379df930be7Sderaadt #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) 38052a0e603Smillert #endif /* __BSD_VISIBLE */ 381df930be7Sderaadt 382df930be7Sderaadt /* 383df930be7Sderaadt * Functions internal to the implementation. 384df930be7Sderaadt */ 385df930be7Sderaadt __BEGIN_DECLS 386c72b5b24Smillert int __srget(FILE *); 387c72b5b24Smillert int __swbuf(int, FILE *); 388df930be7Sderaadt __END_DECLS 389df930be7Sderaadt 390df930be7Sderaadt /* 391df930be7Sderaadt * The __sfoo macros are here so that we can 392df930be7Sderaadt * define function versions in the C library. 393df930be7Sderaadt */ 394df930be7Sderaadt #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++)) 395df930be7Sderaadt static __inline int __sputc(int _c, FILE *_p) { 396df930be7Sderaadt if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n')) 397df930be7Sderaadt return (*_p->_p++ = _c); 398df930be7Sderaadt else 399df930be7Sderaadt return (__swbuf(_c, _p)); 400df930be7Sderaadt } 401df930be7Sderaadt 402df930be7Sderaadt #define __sfeof(p) (((p)->_flags & __SEOF) != 0) 403df930be7Sderaadt #define __sferror(p) (((p)->_flags & __SERR) != 0) 404df930be7Sderaadt #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF))) 405df930be7Sderaadt #define __sfileno(p) ((p)->_file) 406df930be7Sderaadt 407c5acf43aSkurt extern int __isthreaded; 40892efb735Sd 409c5acf43aSkurt #define feof(p) (!__isthreaded ? __sfeof(p) : (feof)(p)) 410c5acf43aSkurt #define ferror(p) (!__isthreaded ? __sferror(p) : (ferror)(p)) 411c5acf43aSkurt #define clearerr(p) (!__isthreaded ? __sclearerr(p) : (clearerr)(p)) 412df930be7Sderaadt 4137ff09c5cSguenther #if __POSIX_VISIBLE 414c5acf43aSkurt #define fileno(p) (!__isthreaded ? __sfileno(p) : (fileno)(p)) 4157ff09c5cSguenther #endif 4169962e7fdSguenther 417c5acf43aSkurt #define getc(fp) (!__isthreaded ? __sgetc(fp) : (getc)(fp)) 418c5acf43aSkurt 419c5acf43aSkurt #if __BSD_VISIBLE 420c5acf43aSkurt /* 421c5acf43aSkurt * The macro implementations of putc and putc_unlocked are not 422c5acf43aSkurt * fully POSIX compliant; they do not set errno on failure 423c5acf43aSkurt */ 424c5acf43aSkurt #define putc(x, fp) (!__isthreaded ? __sputc(x, fp) : (putc)(x, fp)) 425c5acf43aSkurt #endif /* __BSD_VISIBLE */ 426c5acf43aSkurt 427c5acf43aSkurt #if __POSIX_VISIBLE >= 199506 42892efb735Sd #define getc_unlocked(fp) __sgetc(fp) 429ca472fa2Stholo /* 43092efb735Sd * The macro implementations of putc and putc_unlocked are not 43192efb735Sd * fully POSIX compliant; they do not set errno on failure 432ca472fa2Stholo */ 43352a0e603Smillert #if __BSD_VISIBLE 43492efb735Sd #define putc_unlocked(x, fp) __sputc(x, fp) 43552a0e603Smillert #endif /* __BSD_VISIBLE */ 436c5acf43aSkurt #endif /* __POSIX_VISIBLE >= 199506 */ 437df930be7Sderaadt 438df930be7Sderaadt #define getchar() getc(stdin) 439df930be7Sderaadt #define putchar(x) putc(x, stdout) 44092efb735Sd #define getchar_unlocked() getc_unlocked(stdin) 44192efb735Sd #define putchar_unlocked(c) putc_unlocked(c, stdout) 44292efb735Sd 443df930be7Sderaadt #endif /* _STDIO_H_ */ 444