18c1325ffSbostic /*- 28c1325ffSbostic * Copyright (c) 1990 The Regents of the University of California. 38c1325ffSbostic * All rights reserved. 46db880efSdist * 58c1325ffSbostic * This code is derived from software contributed to Berkeley by 68c1325ffSbostic * Chris Torek. 78c1325ffSbostic * 88c1325ffSbostic * %sccs.include.redist.c% 98c1325ffSbostic * 10*d812d6a1Sbostic * @(#)stdio.h 5.24 (Berkeley) 12/02/92 116db880efSdist */ 126db880efSdist 138c1325ffSbostic #ifndef _STDIO_H_ 148c1325ffSbostic #define _STDIO_H_ 158c1325ffSbostic 16e058e875Storek #if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__) 17a28a5819Sbostic #include <sys/types.h> 18e058e875Storek #endif 19e058e875Storek 207bde068dSbostic #include <sys/cdefs.h> 218c1325ffSbostic 222b9924ebSbostic #include <machine/ansi.h> 234e5efec7Sbostic #ifdef _BSD_SIZE_T_ 244e5efec7Sbostic typedef _BSD_SIZE_T_ size_t; 254e5efec7Sbostic #undef _BSD_SIZE_T_ 268c1325ffSbostic #endif 278c1325ffSbostic 28a8c24cc3Sbostic #ifndef NULL 29a8c24cc3Sbostic #define NULL 0 30a8c24cc3Sbostic #endif 31a8c24cc3Sbostic 32e058e875Storek /* 33e058e875Storek * This is fairly grotesque, but pure ANSI code must not inspect the 34e058e875Storek * innards of an fpos_t anyway. The library internally uses off_t, 35e058e875Storek * which we assume is exactly as big as eight chars. 36e058e875Storek */ 37e058e875Storek #if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__) 38e058e875Storek typedef off_t fpos_t; 39e058e875Storek #else 40e058e875Storek typedef struct __sfpos { 41e058e875Storek char _pos[8]; 42e058e875Storek } fpos_t; 43e058e875Storek #endif 445fb3de76Stoy 458c1325ffSbostic #define _FSTDIO /* Define for new stdio with functions. */ 465fb3de76Stoy 477bde068dSbostic /* 487bde068dSbostic * NB: to fit things in six character monocase externals, the stdio 497bde068dSbostic * code uses the prefix `__s' for stdio objects, typically followed 507bde068dSbostic * by a three-character attempt at a mnemonic. 517bde068dSbostic */ 527bde068dSbostic 538c1325ffSbostic /* stdio buffers */ 548c1325ffSbostic struct __sbuf { 558c1325ffSbostic unsigned char *_base; 568c1325ffSbostic int _size; 578c1325ffSbostic }; 585fb3de76Stoy 598c1325ffSbostic /* 608c1325ffSbostic * stdio state variables. 618c1325ffSbostic * 628c1325ffSbostic * The following always hold: 638c1325ffSbostic * 648c1325ffSbostic * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR), 658c1325ffSbostic * _lbfsize is -_bf._size, else _lbfsize is 0 668c1325ffSbostic * if _flags&__SRD, _w is 0 678c1325ffSbostic * if _flags&__SWR, _r is 0 688c1325ffSbostic * 698c1325ffSbostic * This ensures that the getc and putc macros (or inline functions) never 708c1325ffSbostic * try to write or read from a file that is in `read' or `write' mode. 718c1325ffSbostic * (Moreover, they can, and do, automatically switch from read mode to 728c1325ffSbostic * write mode, and back, on "r+" and "w+" files.) 738c1325ffSbostic * 748c1325ffSbostic * _lbfsize is used only to make the inline line-buffered output stream 758c1325ffSbostic * code as compact as possible. 768c1325ffSbostic * 778c1325ffSbostic * _ub, _up, and _ur are used when ungetc() pushes back more characters 788c1325ffSbostic * than fit in the current _bf, or when ungetc() pushes back a character 798c1325ffSbostic * that does not match the previous one in _bf. When this happens, 808c1325ffSbostic * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff 818c1325ffSbostic * _ub._base!=NULL) and _up and _ur save the current values of _p and _r. 828c1325ffSbostic */ 838c1325ffSbostic typedef struct __sFILE { 848c1325ffSbostic unsigned char *_p; /* current position in (some) buffer */ 858c1325ffSbostic int _r; /* read space left for getc() */ 868c1325ffSbostic int _w; /* write space left for putc() */ 878c1325ffSbostic short _flags; /* flags, below; this FILE is free if 0 */ 888c1325ffSbostic short _file; /* fileno, if Unix descriptor, else -1 */ 898c1325ffSbostic struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ 908c1325ffSbostic int _lbfsize; /* 0 or -_bf._size, for inline putc */ 918c1325ffSbostic 928c1325ffSbostic /* operations */ 938c1325ffSbostic void *_cookie; /* cookie passed to io functions */ 94e505d896Sdonn int (*_close) __P((void *)); 95e505d896Sdonn int (*_read) __P((void *, char *, int)); 96e505d896Sdonn fpos_t (*_seek) __P((void *, fpos_t, int)); 97e505d896Sdonn int (*_write) __P((void *, const char *, int)); 98cbae850eSkfall 998c1325ffSbostic /* separate buffer for long sequences of ungetc() */ 1008c1325ffSbostic struct __sbuf _ub; /* ungetc buffer */ 1018c1325ffSbostic unsigned char *_up; /* saved _p when _p is doing ungetc data */ 1028c1325ffSbostic int _ur; /* saved _r when _r is counting ungetc data */ 1038c1325ffSbostic 1048c1325ffSbostic /* tricks to meet minimum requirements even when malloc() fails */ 1058c1325ffSbostic unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ 1068c1325ffSbostic unsigned char _nbuf[1]; /* guarantee a getc() buffer */ 1078c1325ffSbostic 1088c1325ffSbostic /* separate buffer for fgetline() when line crosses buffer boundary */ 1098c1325ffSbostic struct __sbuf _lb; /* buffer for fgetline() */ 1108c1325ffSbostic 1118c1325ffSbostic /* Unix stdio files get aligned to block boundaries on fseek() */ 1128c1325ffSbostic int _blksize; /* stat.st_blksize (may be != _bf._size) */ 1138c1325ffSbostic int _offset; /* current lseek offset */ 1148c1325ffSbostic } FILE; 1158c1325ffSbostic 1164b0a96f3Sdonn __BEGIN_DECLS 1178c1325ffSbostic extern FILE __sF[]; 1184b0a96f3Sdonn __END_DECLS 1198c1325ffSbostic 1208c1325ffSbostic #define __SLBF 0x0001 /* line buffered */ 1218c1325ffSbostic #define __SNBF 0x0002 /* unbuffered */ 1228c1325ffSbostic #define __SRD 0x0004 /* OK to read */ 1238c1325ffSbostic #define __SWR 0x0008 /* OK to write */ 1248c1325ffSbostic /* RD and WR are never simultaneously asserted */ 1258c1325ffSbostic #define __SRW 0x0010 /* open for reading & writing */ 1268c1325ffSbostic #define __SEOF 0x0020 /* found EOF */ 1278c1325ffSbostic #define __SERR 0x0040 /* found error */ 1288c1325ffSbostic #define __SMBF 0x0080 /* _buf is from malloc */ 1298c1325ffSbostic #define __SAPP 0x0100 /* fdopen()ed in append mode */ 1308c1325ffSbostic #define __SSTR 0x0200 /* this is an sprintf/snprintf string */ 1318c1325ffSbostic #define __SOPT 0x0400 /* do fseek() optimisation */ 1328c1325ffSbostic #define __SNPT 0x0800 /* do not do fseek() optimisation */ 1338c1325ffSbostic #define __SOFF 0x1000 /* set iff _offset is in fact correct */ 1348c1325ffSbostic #define __SMOD 0x2000 /* true => fgetline modified _p text */ 1358c1325ffSbostic 1368c1325ffSbostic /* 1378c1325ffSbostic * The following three definitions are for ANSI C, which took them 1388c1325ffSbostic * from System V, which brilliantly took internal interface macros and 1398c1325ffSbostic * made them official arguments to setvbuf(), without renaming them. 1408c1325ffSbostic * Hence, these ugly _IOxxx names are *supposed* to appear in user code. 1418c1325ffSbostic * 1428c1325ffSbostic * Although numbered as their counterparts above, the implementation 1438c1325ffSbostic * does not rely on this. 1448c1325ffSbostic */ 1458c1325ffSbostic #define _IOFBF 0 /* setvbuf should set fully buffered */ 1468c1325ffSbostic #define _IOLBF 1 /* setvbuf should set line buffered */ 1478c1325ffSbostic #define _IONBF 2 /* setvbuf should set unbuffered */ 1488c1325ffSbostic 1498c1325ffSbostic #define BUFSIZ 1024 /* size of buffer used by setbuf */ 1508c1325ffSbostic #define EOF (-1) 1518c1325ffSbostic 1528c1325ffSbostic /* 1538c1325ffSbostic * FOPEN_MAX is a minimum maximum, and should be the number of descriptors 1548c1325ffSbostic * that the kernel can provide without allocation of a resource that can 1558c1325ffSbostic * fail without the process sleeping. Do not use this for anything. 1568c1325ffSbostic */ 1578c1325ffSbostic #define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */ 1588c1325ffSbostic #define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */ 1598c1325ffSbostic 1608c1325ffSbostic /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */ 1618c1325ffSbostic #ifndef _ANSI_SOURCE 162e57e7607Sbostic #define P_tmpdir "/var/tmp/" 1638c1325ffSbostic #endif 1648c1325ffSbostic #define L_tmpnam 1024 /* XXX must be == PATH_MAX */ 1658c1325ffSbostic #define TMP_MAX 308915776 1668c1325ffSbostic 1678c1325ffSbostic #ifndef SEEK_SET 1688c1325ffSbostic #define SEEK_SET 0 /* set file offset to offset */ 1698c1325ffSbostic #endif 1708c1325ffSbostic #ifndef SEEK_CUR 1718c1325ffSbostic #define SEEK_CUR 1 /* set file offset to current plus offset */ 1728c1325ffSbostic #endif 1738c1325ffSbostic #ifndef SEEK_END 1748c1325ffSbostic #define SEEK_END 2 /* set file offset to EOF plus offset */ 1758c1325ffSbostic #endif 1768c1325ffSbostic 1778c1325ffSbostic #define stdin (&__sF[0]) 1788c1325ffSbostic #define stdout (&__sF[1]) 1798c1325ffSbostic #define stderr (&__sF[2]) 1808c1325ffSbostic 1818c1325ffSbostic /* 1828c1325ffSbostic * Functions defined in ANSI C standard. 1838c1325ffSbostic */ 1847bde068dSbostic __BEGIN_DECLS 1857bde068dSbostic void clearerr __P((FILE *)); 1867bde068dSbostic int fclose __P((FILE *)); 1877bde068dSbostic int feof __P((FILE *)); 1887bde068dSbostic int ferror __P((FILE *)); 1897bde068dSbostic int fflush __P((FILE *)); 1907bde068dSbostic int fgetc __P((FILE *)); 1917bde068dSbostic int fgetpos __P((FILE *, fpos_t *)); 1927bde068dSbostic char *fgets __P((char *, size_t, FILE *)); 193e505d896Sdonn FILE *fopen __P((const char *, const char *)); 1947bde068dSbostic int fprintf __P((FILE *, const char *, ...)); 1957bde068dSbostic int fputc __P((int, FILE *)); 1967bde068dSbostic int fputs __P((const char *, FILE *)); 197d852a0c9Sbostic size_t fread __P((void *, size_t, size_t, FILE *)); 198e505d896Sdonn FILE *freopen __P((const char *, const char *, FILE *)); 1997bde068dSbostic int fscanf __P((FILE *, const char *, ...)); 2007bde068dSbostic int fseek __P((FILE *, long, int)); 2017bde068dSbostic int fsetpos __P((FILE *, const fpos_t *)); 2027bde068dSbostic long ftell __P((const FILE *)); 203d852a0c9Sbostic size_t fwrite __P((const void *, size_t, size_t, FILE *)); 2047bde068dSbostic int getc __P((FILE *)); 2057bde068dSbostic int getchar __P((void)); 2067bde068dSbostic char *gets __P((char *)); 207d603dd2eSbostic #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE) 208d603dd2eSbostic extern int sys_nerr; /* perror(3) external variables */ 209d603dd2eSbostic extern char *sys_errlist[]; 210d603dd2eSbostic #endif 2117bde068dSbostic void perror __P((const char *)); 2127bde068dSbostic int printf __P((const char *, ...)); 2137bde068dSbostic int putc __P((int, FILE *)); 2147bde068dSbostic int putchar __P((int)); 2157bde068dSbostic int puts __P((const char *)); 2167bde068dSbostic int remove __P((const char *)); 217e505d896Sdonn int rename __P((const char *, const char *)); 2187bde068dSbostic void rewind __P((FILE *)); 2197bde068dSbostic int scanf __P((const char *, ...)); 2207bde068dSbostic void setbuf __P((FILE *, char *)); 2217bde068dSbostic int setvbuf __P((FILE *, char *, int, size_t)); 2227bde068dSbostic int sprintf __P((char *, const char *, ...)); 223e600f3eaStorek int sscanf __P((const char *, const char *, ...)); 2247bde068dSbostic FILE *tmpfile __P((void)); 2257bde068dSbostic char *tmpnam __P((char *)); 2267bde068dSbostic int ungetc __P((int, FILE *)); 2274e5efec7Sbostic int vfprintf __P((FILE *, const char *, _BSD_VA_LIST_)); 2284e5efec7Sbostic int vprintf __P((const char *, _BSD_VA_LIST_)); 2294e5efec7Sbostic int vsprintf __P((char *, const char *, _BSD_VA_LIST_)); 2307bde068dSbostic __END_DECLS 2318c1325ffSbostic 2328c1325ffSbostic /* 2338c1325ffSbostic * Functions defined in POSIX 1003.1. 2348c1325ffSbostic */ 2358c1325ffSbostic #ifndef _ANSI_SOURCE 2368c1325ffSbostic #define L_cuserid 9 /* size for cuserid(); UT_NAMESIZE + 1 */ 2378c1325ffSbostic #define L_ctermid 1024 /* size for ctermid(); PATH_MAX */ 2388c1325ffSbostic 2397bde068dSbostic __BEGIN_DECLS 2400db5e65cSbostic char *ctermid __P((char *)); 2417bde068dSbostic FILE *fdopen __P((int, const char *)); 2427bde068dSbostic int fileno __P((FILE *)); 2437bde068dSbostic __END_DECLS 244e505d896Sdonn #endif /* not ANSI */ 2458c1325ffSbostic 2468c1325ffSbostic /* 2478c1325ffSbostic * Routines that are purely local. 2488c1325ffSbostic */ 249e505d896Sdonn #if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE) 2507bde068dSbostic __BEGIN_DECLS 2517bde068dSbostic char *fgetline __P((FILE *, size_t *)); 2527bde068dSbostic int fpurge __P((FILE *)); 2537bde068dSbostic int getw __P((FILE *)); 2547bde068dSbostic int pclose __P((FILE *)); 255e505d896Sdonn FILE *popen __P((const char *, const char *)); 2567bde068dSbostic int putw __P((int, FILE *)); 2577bde068dSbostic void setbuffer __P((FILE *, char *, int)); 2587bde068dSbostic int setlinebuf __P((FILE *)); 259e57e7607Sbostic char *tempnam __P((const char *, const char *)); 2607bde068dSbostic int snprintf __P((char *, size_t, const char *, ...)); 2614e5efec7Sbostic int vsnprintf __P((char *, size_t, const char *, _BSD_VA_LIST_)); 2624e5efec7Sbostic int vscanf __P((const char *, _BSD_VA_LIST_)); 2634e5efec7Sbostic int vsscanf __P((const char *, const char *, _BSD_VA_LIST_)); 264*d812d6a1Sbostic FILE *zopen __P((const char *, const char *, int)); 2657bde068dSbostic __END_DECLS 2668c1325ffSbostic 2678c1325ffSbostic /* 268433618cdSdonn * This is a #define because the function is used internally and 269433618cdSdonn * (unlike vfscanf) the name __svfscanf is guaranteed not to collide 270433618cdSdonn * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined. 271433618cdSdonn */ 272433618cdSdonn #define vfscanf __svfscanf 273433618cdSdonn 274433618cdSdonn /* 2758c1325ffSbostic * Stdio function-access interface. 2768c1325ffSbostic */ 2777bde068dSbostic __BEGIN_DECLS 278e505d896Sdonn FILE *funopen __P((const void *, 279e505d896Sdonn int (*)(void *, char *, int), 280e505d896Sdonn int (*)(void *, const char *, int), 281e505d896Sdonn fpos_t (*)(void *, fpos_t, int), 282e505d896Sdonn int (*)(void *))); 2837bde068dSbostic __END_DECLS 2848c1325ffSbostic #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) 2858c1325ffSbostic #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) 2864b0a96f3Sdonn #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */ 2878c1325ffSbostic 2888c1325ffSbostic /* 2898c1325ffSbostic * Functions internal to the implementation. 2908c1325ffSbostic */ 2914b0a96f3Sdonn __BEGIN_DECLS 2927bde068dSbostic int __srget __P((FILE *)); 2934e5efec7Sbostic int __svfscanf __P((FILE *, const char *, _BSD_VA_LIST_)); 2947bde068dSbostic int __swbuf __P((int, FILE *)); 2954b0a96f3Sdonn __END_DECLS 2968c1325ffSbostic 2978c1325ffSbostic /* 2988c1325ffSbostic * The __sfoo macros are here so that we can 2998c1325ffSbostic * define function versions in the C library. 3008c1325ffSbostic */ 3018c1325ffSbostic #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++)) 302433618cdSdonn #if defined(__GNUC__) && defined(__STDC__) 303552d4617Sbostic static __inline int __sputc(int _c, FILE *_p) { 3048c1325ffSbostic if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n')) 3058c1325ffSbostic return (*_p->_p++ = _c); 3068c1325ffSbostic else 3078c1325ffSbostic return (__swbuf(_c, _p)); 3088c1325ffSbostic } 3098c1325ffSbostic #else 3108c1325ffSbostic /* 3118c1325ffSbostic * This has been tuned to generate reasonable code on the vax using pcc. 3128c1325ffSbostic */ 3138c1325ffSbostic #define __sputc(c, p) \ 3148c1325ffSbostic (--(p)->_w < 0 ? \ 3158c1325ffSbostic (p)->_w >= (p)->_lbfsize ? \ 3168c1325ffSbostic (*(p)->_p = (c)), *(p)->_p != '\n' ? \ 3178c1325ffSbostic (int)*(p)->_p++ : \ 3188c1325ffSbostic __swbuf('\n', p) : \ 3198c1325ffSbostic __swbuf((int)(c), p) : \ 3208c1325ffSbostic (*(p)->_p = (c), (int)*(p)->_p++)) 3218c1325ffSbostic #endif 3228c1325ffSbostic 3238c1325ffSbostic #define __sfeof(p) (((p)->_flags & __SEOF) != 0) 3248c1325ffSbostic #define __sferror(p) (((p)->_flags & __SERR) != 0) 3258c1325ffSbostic #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF))) 3268c1325ffSbostic #define __sfileno(p) ((p)->_file) 3278c1325ffSbostic 3288c1325ffSbostic #define feof(p) __sfeof(p) 3298c1325ffSbostic #define ferror(p) __sferror(p) 3308c1325ffSbostic #define clearerr(p) __sclearerr(p) 3318c1325ffSbostic 3328c1325ffSbostic #ifndef _ANSI_SOURCE 3338c1325ffSbostic #define fileno(p) __sfileno(p) 3348c1325ffSbostic #endif 3358c1325ffSbostic 3368c1325ffSbostic #ifndef lint 3378c1325ffSbostic #define getc(fp) __sgetc(fp) 3388c1325ffSbostic #define putc(x, fp) __sputc(x, fp) 3398c1325ffSbostic #endif /* lint */ 3408c1325ffSbostic 3418c1325ffSbostic #define getchar() getc(stdin) 3428c1325ffSbostic #define putchar(x) putc(x, stdout) 3438c1325ffSbostic #endif /* _STDIO_H_ */ 344