stdio.h revision 8858
1333532Sdes/*- 2333532Sdes * Copyright (c) 1990, 1993 3333532Sdes * The Regents of the University of California. All rights reserved. 4333532Sdes * 5333532Sdes * This code is derived from software contributed to Berkeley by 6333532Sdes * Chris Torek. 7333532Sdes * 8333532Sdes * Redistribution and use in source and binary forms, with or without 9333532Sdes * modification, are permitted provided that the following conditions 10333532Sdes * are met: 11333532Sdes * 1. Redistributions of source code must retain the above copyright 12333532Sdes * notice, this list of conditions and the following disclaimer. 13333532Sdes * 2. Redistributions in binary form must reproduce the above copyright 14333532Sdes * notice, this list of conditions and the following disclaimer in the 15333532Sdes * documentation and/or other materials provided with the distribution. 16333532Sdes * 3. All advertising materials mentioning features or use of this software 17333532Sdes * must display the following acknowledgement: 18333532Sdes * This product includes software developed by the University of 19333532Sdes * California, Berkeley and its contributors. 20333532Sdes * 4. Neither the name of the University nor the names of its contributors 21333532Sdes * may be used to endorse or promote products derived from this software 22333532Sdes * without specific prior written permission. 23333532Sdes * 24333532Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25333532Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26333532Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27333532Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28333532Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29333532Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30333532Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31333532Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32333532Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33333532Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34333532Sdes * SUCH DAMAGE. 35333532Sdes * 36333532Sdes * @(#)stdio.h 8.4 (Berkeley) 1/4/94 37333532Sdes */ 38333532Sdes 39333532Sdes#ifndef _STDIO_H_ 40333532Sdes#define _STDIO_H_ 41333532Sdes 42333532Sdes#if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__) 43333532Sdes#include <sys/types.h> 44333532Sdes#endif 45333532Sdes 46333532Sdes#include <sys/cdefs.h> 47333532Sdes 48333532Sdes#include <machine/ansi.h> 49333532Sdes#ifdef _BSD_SIZE_T_ 50333532Sdestypedef _BSD_SIZE_T_ size_t; 51333532Sdes#undef _BSD_SIZE_T_ 52333532Sdes#endif 53333532Sdes 54333532Sdes#ifndef NULL 55333532Sdes#define NULL 0 56333532Sdes#endif 57333532Sdes 58333532Sdes/* 59333532Sdes * This is fairly grotesque, but pure ANSI code must not inspect the 60333532Sdes * innards of an fpos_t anyway. The library internally uses off_t, 61333532Sdes * which we assume is exactly as big as eight chars. (When we switch 62333532Sdes * to gcc 2.4 we will use __attribute__ here.) 63333532Sdes * 64333532Sdes * WARNING: the alignment constraints on an off_t and the struct below 65333532Sdes * differ on (e.g.) the SPARC. Hence, the placement of an fpos_t object 66333532Sdes * in a structure will change if fpos_t's are not aligned on 8-byte 67333532Sdes * boundaries. THIS IS A CROCK, but for now there is no way around it. 68356345Scy */ 69333532Sdes#if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__) 70333532Sdestypedef off_t fpos_t; 71333532Sdes#else 72333532Sdestypedef struct __sfpos { 73356345Scy char _pos[8]; 74333532Sdes} fpos_t; 75333532Sdes#endif 76356345Scy 77333532Sdes#define _FSTDIO /* Define for new stdio with functions. */ 78356345Scy 79333532Sdes/* 80333532Sdes * NB: to fit things in six character monocase externals, the stdio 81333532Sdes * code uses the prefix `__s' for stdio objects, typically followed 82333532Sdes * by a three-character attempt at a mnemonic. 83333532Sdes */ 84333532Sdes 85333532Sdes/* stdio buffers */ 86356345Scystruct __sbuf { 87333532Sdes unsigned char *_base; 88333532Sdes int _size; 89333532Sdes}; 90333532Sdes 91333532Sdes/* 92333532Sdes * stdio state variables. 93333532Sdes * 94333532Sdes * The following always hold: 95333532Sdes * 96333532Sdes * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR), 97333532Sdes * _lbfsize is -_bf._size, else _lbfsize is 0 98356345Scy * if _flags&__SRD, _w is 0 99333532Sdes * if _flags&__SWR, _r is 0 100333532Sdes * 101333532Sdes * This ensures that the getc and putc macros (or inline functions) never 102333532Sdes * try to write or read from a file that is in `read' or `write' mode. 103333532Sdes * (Moreover, they can, and do, automatically switch from read mode to 104333532Sdes * write mode, and back, on "r+" and "w+" files.) 105333532Sdes * 106333532Sdes * _lbfsize is used only to make the inline line-buffered output stream 107333532Sdes * code as compact as possible. 108333532Sdes * 109333532Sdes * _ub, _up, and _ur are used when ungetc() pushes back more characters 110333532Sdes * than fit in the current _bf, or when ungetc() pushes back a character 111333532Sdes * that does not match the previous one in _bf. When this happens, 112333532Sdes * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff 113333532Sdes * _ub._base!=NULL) and _up and _ur save the current values of _p and _r. 114333532Sdes * 115333532Sdes * NB: see WARNING above before changing the layout of this structure! 116333532Sdes */ 117333532Sdestypedef struct __sFILE { 118333532Sdes unsigned char *_p; /* current position in (some) buffer */ 119333532Sdes int _r; /* read space left for getc() */ 120333532Sdes int _w; /* write space left for putc() */ 121333532Sdes short _flags; /* flags, below; this FILE is free if 0 */ 122333532Sdes short _file; /* fileno, if Unix descriptor, else -1 */ 123333532Sdes struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ 124356345Scy int _lbfsize; /* 0 or -_bf._size, for inline putc */ 125333532Sdes 126333532Sdes /* operations */ 127333532Sdes void *_cookie; /* cookie passed to io functions */ 128333532Sdes int (*_close) __P((void *)); 129333532Sdes int (*_read) __P((void *, char *, int)); 130333532Sdes fpos_t (*_seek) __P((void *, fpos_t, int)); 131333532Sdes int (*_write) __P((void *, const char *, int)); 132333532Sdes 133333532Sdes /* separate buffer for long sequences of ungetc() */ 134333532Sdes struct __sbuf _ub; /* ungetc buffer */ 135333532Sdes unsigned char *_up; /* saved _p when _p is doing ungetc data */ 136333532Sdes int _ur; /* saved _r when _r is counting ungetc data */ 137356345Scy 138333532Sdes /* tricks to meet minimum requirements even when malloc() fails */ 139333532Sdes unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ 140333532Sdes unsigned char _nbuf[1]; /* guarantee a getc() buffer */ 141333532Sdes 142333532Sdes /* separate buffer for fgetln() when line crosses buffer boundary */ 143333532Sdes struct __sbuf _lb; /* buffer for fgetln() */ 144333532Sdes 145333532Sdes /* Unix stdio files get aligned to block boundaries on fseek() */ 146333532Sdes int _blksize; /* stat.st_blksize (may be != _bf._size) */ 147333532Sdes fpos_t _offset; /* current lseek offset (see WARNING) */ 148333532Sdes} FILE; 149333532Sdes 150333532Sdes__BEGIN_DECLS 151356345Scyextern FILE __sF[]; 152333532Sdes__END_DECLS 153333532Sdes 154333532Sdes#define __SLBF 0x0001 /* line buffered */ 155333532Sdes#define __SNBF 0x0002 /* unbuffered */ 156333532Sdes#define __SRD 0x0004 /* OK to read */ 157333532Sdes#define __SWR 0x0008 /* OK to write */ 158333532Sdes /* RD and WR are never simultaneously asserted */ 159333532Sdes#define __SRW 0x0010 /* open for reading & writing */ 160333532Sdes#define __SEOF 0x0020 /* found EOF */ 161333532Sdes#define __SERR 0x0040 /* found error */ 162333532Sdes#define __SMBF 0x0080 /* _buf is from malloc */ 163356345Scy#define __SAPP 0x0100 /* fdopen()ed in append mode */ 164333532Sdes#define __SSTR 0x0200 /* this is an sprintf/snprintf string */ 165333532Sdes#define __SOPT 0x0400 /* do fseek() optimisation */ 166333532Sdes#define __SNPT 0x0800 /* do not do fseek() optimisation */ 167333532Sdes#define __SOFF 0x1000 /* set iff _offset is in fact correct */ 168333532Sdes#define __SMOD 0x2000 /* true => fgetln modified _p text */ 169333532Sdes 170333532Sdes/* 171333532Sdes * The following three definitions are for ANSI C, which took them 172333532Sdes * from System V, which brilliantly took internal interface macros and 173333532Sdes * made them official arguments to setvbuf(), without renaming them. 174333532Sdes * Hence, these ugly _IOxxx names are *supposed* to appear in user code. 175333532Sdes * 176333532Sdes * Although numbered as their counterparts above, the implementation 177333532Sdes * does not rely on this. 178356345Scy */ 179333532Sdes#define _IOFBF 0 /* setvbuf should set fully buffered */ 180333532Sdes#define _IOLBF 1 /* setvbuf should set line buffered */ 181333532Sdes#define _IONBF 2 /* setvbuf should set unbuffered */ 182333532Sdes 183333532Sdes#define BUFSIZ 1024 /* size of buffer used by setbuf */ 184333532Sdes#define EOF (-1) 185333532Sdes 186333532Sdes/* 187333532Sdes * FOPEN_MAX is a minimum maximum, and is the number of streams that 188333532Sdes * stdio can provide without attempting to allocate further resources 189333532Sdes * (which could fail). Do not use this for anything. 190333532Sdes */ 191333532Sdes /* must be == _POSIX_STREAM_MAX <limits.h> */ 192333532Sdes#define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */ 193333532Sdes#define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */ 194333532Sdes 195333532Sdes/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */ 196333532Sdes#ifndef _ANSI_SOURCE 197333532Sdes#define P_tmpdir "/var/tmp/" 198333532Sdes#endif 199333532Sdes#define L_tmpnam 1024 /* XXX must be == PATH_MAX */ 200333532Sdes#define TMP_MAX 308915776 201333532Sdes 202333532Sdes#ifndef SEEK_SET 203333532Sdes#define SEEK_SET 0 /* set file offset to offset */ 204333532Sdes#endif 205333532Sdes#ifndef SEEK_CUR 206333532Sdes#define SEEK_CUR 1 /* set file offset to current plus offset */ 207333532Sdes#endif 208333532Sdes#ifndef SEEK_END 209333532Sdes#define SEEK_END 2 /* set file offset to EOF plus offset */ 210333532Sdes#endif 211333532Sdes 212333532Sdes#define stdin (&__sF[0]) 213333532Sdes#define stdout (&__sF[1]) 214333532Sdes#define stderr (&__sF[2]) 215333532Sdes 216333532Sdes/* 217333532Sdes * Functions defined in ANSI C standard. 218333532Sdes */ 219333532Sdes__BEGIN_DECLS 220333532Sdesvoid clearerr __P((FILE *)); 221356345Scyint fclose __P((FILE *)); 222356345Scyint feof __P((FILE *)); 223356345Scyint ferror __P((FILE *)); 224333532Sdesint fflush __P((FILE *)); 225333532Sdesint fgetc __P((FILE *)); 226356345Scyint fgetpos __P((FILE *, fpos_t *)); 227333532Sdeschar *fgets __P((char *, size_t, FILE *)); 228333532SdesFILE *fopen __P((const char *, const char *)); 229356345Scyint fprintf __P((FILE *, const char *, ...)); 230333532Sdesint fputc __P((int, FILE *)); 231333532Sdesint fputs __P((const char *, FILE *)); 232333532Sdessize_t fread __P((void *, size_t, size_t, FILE *)); 233333532SdesFILE *freopen __P((const char *, const char *, FILE *)); 234333532Sdesint fscanf __P((FILE *, const char *, ...)); 235333532Sdesint fseek __P((FILE *, long, int)); 236333532Sdesint fsetpos __P((FILE *, const fpos_t *)); 237333532Sdeslong ftell __P((const FILE *)); 238333532Sdessize_t fwrite __P((const void *, size_t, size_t, FILE *)); 239333532Sdesint getc __P((FILE *)); 240333532Sdesint getchar __P((void)); 241333532Sdeschar *gets __P((char *)); 242356345Scy#if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE) 243333532Sdesextern __const int sys_nerr; /* perror(3) external variables */ 244333532Sdesextern __const char *__const sys_errlist[]; 245356345Scy#endif 246333532Sdesvoid perror __P((const char *)); 247356345Scyint printf __P((const char *, ...)); 248356345Scyint putc __P((int, FILE *)); 249356345Scyint putchar __P((int)); 250333532Sdesint puts __P((const char *)); 251356345Scyint remove __P((const char *)); 252356345Scyint rename __P((const char *, const char *)); 253333532Sdesvoid rewind __P((FILE *)); 254356345Scyint scanf __P((const char *, ...)); 255356345Scyvoid setbuf __P((FILE *, char *)); 256356345Scyint setvbuf __P((FILE *, char *, int, size_t)); 257356345Scyint sprintf __P((char *, const char *, ...)); 258356345Scyint sscanf __P((const char *, const char *, ...)); 259356345ScyFILE *tmpfile __P((void)); 260356345Scychar *tmpnam __P((char *)); 261356345Scyint ungetc __P((int, FILE *)); 262356345Scyint vfprintf __P((FILE *, const char *, _BSD_VA_LIST_)); 263356345Scyint vprintf __P((const char *, _BSD_VA_LIST_)); 264356345Scyint vsprintf __P((char *, const char *, _BSD_VA_LIST_)); 265356345Scy__END_DECLS 266356345Scy 267356345Scy/* 268356345Scy * Functions defined in POSIX 1003.1. 269356345Scy */ 270356345Scy#ifndef _ANSI_SOURCE 271333532Sdes#define L_cuserid 9 /* size for cuserid(); UT_NAMESIZE + 1 */ 272333532Sdes#define L_ctermid 1024 /* size for ctermid(); PATH_MAX */ 273333532Sdes 274333532Sdes__BEGIN_DECLS 275333532Sdeschar *ctermid __P((char *)); 276333532SdesFILE *fdopen __P((int, const char *)); 277356345Scyint fileno __P((FILE *)); 278356345Scy__END_DECLS 279333532Sdes#endif /* not ANSI */ 280356345Scy 281356345Scy/* 282356345Scy * Routines that are purely local. 283356345Scy */ 284356345Scy#if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE) 285356345Scy__BEGIN_DECLS 286356345Scychar *fgetln __P((FILE *, size_t *)); 287356345Scyint fpurge __P((FILE *)); 288366095Scyint getw __P((FILE *)); 289366095Scyint pclose __P((FILE *)); 290366095ScyFILE *popen __P((const char *, const char *)); 291366095Scyint putw __P((int, FILE *)); 292333532Sdesvoid setbuffer __P((FILE *, char *, int)); 293333532Sdesint setlinebuf __P((FILE *)); 294333532Sdeschar *tempnam __P((const char *, const char *)); 295333532Sdesint snprintf __P((char *, size_t, const char *, ...)); 296333532Sdesint vsnprintf __P((char *, size_t, const char *, _BSD_VA_LIST_)); 297333532Sdesint vscanf __P((const char *, _BSD_VA_LIST_)); 298333532Sdesint vsscanf __P((const char *, const char *, _BSD_VA_LIST_)); 299333532SdesFILE *zopen __P((const char *, const char *, int)); 300333532Sdes__END_DECLS 301333532Sdes 302333532Sdes/* 303 * This is a #define because the function is used internally and 304 * (unlike vfscanf) the name __svfscanf is guaranteed not to collide 305 * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined. 306 */ 307#define vfscanf __svfscanf 308 309/* 310 * Stdio function-access interface. 311 */ 312__BEGIN_DECLS 313FILE *funopen __P((const void *, 314 int (*)(void *, char *, int), 315 int (*)(void *, const char *, int), 316 fpos_t (*)(void *, fpos_t, int), 317 int (*)(void *))); 318__END_DECLS 319#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) 320#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) 321#endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */ 322 323/* 324 * Functions internal to the implementation. 325 */ 326__BEGIN_DECLS 327int __srget __P((FILE *)); 328int __svfscanf __P((FILE *, const char *, _BSD_VA_LIST_)); 329int __swbuf __P((int, FILE *)); 330__END_DECLS 331 332/* 333 * The __sfoo macros are here so that we can 334 * define function versions in the C library. 335 */ 336#define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++)) 337#if defined(__GNUC__) && defined(__STDC__) 338static __inline int __sputc(int _c, FILE *_p) { 339 if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n')) 340 return (*_p->_p++ = _c); 341 else 342 return (__swbuf(_c, _p)); 343} 344#else 345/* 346 * This has been tuned to generate reasonable code on the vax using pcc. 347 */ 348#define __sputc(c, p) \ 349 (--(p)->_w < 0 ? \ 350 (p)->_w >= (p)->_lbfsize ? \ 351 (*(p)->_p = (c)), *(p)->_p != '\n' ? \ 352 (int)*(p)->_p++ : \ 353 __swbuf('\n', p) : \ 354 __swbuf((int)(c), p) : \ 355 (*(p)->_p = (c), (int)*(p)->_p++)) 356#endif 357 358#define __sfeof(p) (((p)->_flags & __SEOF) != 0) 359#define __sferror(p) (((p)->_flags & __SERR) != 0) 360#define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF))) 361#define __sfileno(p) ((p)->_file) 362 363#define feof(p) __sfeof(p) 364#define ferror(p) __sferror(p) 365#define clearerr(p) __sclearerr(p) 366 367#ifndef _ANSI_SOURCE 368#define fileno(p) __sfileno(p) 369#endif 370 371#ifndef lint 372#define getc(fp) __sgetc(fp) 373#define putc(x, fp) __sputc(x, fp) 374#endif /* lint */ 375 376#define getchar() getc(stdin) 377#define putchar(x) putc(x, stdout) 378#endif /* _STDIO_H_ */ 379