146112Sbostic /*-
262376Sbostic * Copyright (c) 1990, 1993
362376Sbostic * The Regents of the University of California. All rights reserved.
421399Sdist *
546112Sbostic * This code is derived from software contributed to Berkeley by
646112Sbostic * Chris Torek.
746112Sbostic *
846112Sbostic * %sccs.include.redist.c%
946112Sbostic *
10*69116Sbostic * @(#)stdio.h 8.5 (Berkeley) 04/29/95
1121399Sdist */
1221399Sdist
1346112Sbostic #ifndef _STDIO_H_
1446112Sbostic #define _STDIO_H_
1546112Sbostic
1654832Storek #if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
1754082Sbostic #include <sys/types.h>
1854832Storek #endif
1954832Storek
2046142Sbostic #include <sys/cdefs.h>
2146112Sbostic
2247181Sbostic #include <machine/ansi.h>
2354257Sbostic #ifdef _BSD_SIZE_T_
2454257Sbostic typedef _BSD_SIZE_T_ size_t;
2554257Sbostic #undef _BSD_SIZE_T_
2646112Sbostic #endif
2746112Sbostic
2838139Sbostic #ifndef NULL
2938139Sbostic #define NULL 0
3038139Sbostic #endif
3138139Sbostic
3254832Storek /*
3354832Storek * This is fairly grotesque, but pure ANSI code must not inspect the
3454832Storek * innards of an fpos_t anyway. The library internally uses off_t,
3560292Storek * which we assume is exactly as big as eight chars. (When we switch
3660292Storek * to gcc 2.4 we will use __attribute__ here.)
3760292Storek *
3860292Storek * WARNING: the alignment constraints on an off_t and the struct below
3960292Storek * differ on (e.g.) the SPARC. Hence, the placement of an fpos_t object
4060292Storek * in a structure will change if fpos_t's are not aligned on 8-byte
4160292Storek * boundaries. THIS IS A CROCK, but for now there is no way around it.
4254832Storek */
4354832Storek #if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
4454832Storek typedef off_t fpos_t;
4554832Storek #else
4654832Storek typedef struct __sfpos {
4754832Storek char _pos[8];
4854832Storek } fpos_t;
4954832Storek #endif
503165Stoy
5146112Sbostic #define _FSTDIO /* Define for new stdio with functions. */
5246112Sbostic
5346142Sbostic /*
5446142Sbostic * NB: to fit things in six character monocase externals, the stdio
5546142Sbostic * code uses the prefix `__s' for stdio objects, typically followed
5646142Sbostic * by a three-character attempt at a mnemonic.
5746142Sbostic */
5846142Sbostic
5946112Sbostic /* stdio buffers */
6046112Sbostic struct __sbuf {
6146112Sbostic unsigned char *_base;
6246112Sbostic int _size;
6346112Sbostic };
6446112Sbostic
6546112Sbostic /*
6646112Sbostic * stdio state variables.
6746112Sbostic *
6846112Sbostic * The following always hold:
6946112Sbostic *
7046112Sbostic * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
7146112Sbostic * _lbfsize is -_bf._size, else _lbfsize is 0
7246112Sbostic * if _flags&__SRD, _w is 0
7346112Sbostic * if _flags&__SWR, _r is 0
7446112Sbostic *
7546112Sbostic * This ensures that the getc and putc macros (or inline functions) never
7646112Sbostic * try to write or read from a file that is in `read' or `write' mode.
7746112Sbostic * (Moreover, they can, and do, automatically switch from read mode to
7846112Sbostic * write mode, and back, on "r+" and "w+" files.)
7946112Sbostic *
8046112Sbostic * _lbfsize is used only to make the inline line-buffered output stream
8146112Sbostic * code as compact as possible.
8246112Sbostic *
8346112Sbostic * _ub, _up, and _ur are used when ungetc() pushes back more characters
8446112Sbostic * than fit in the current _bf, or when ungetc() pushes back a character
8546112Sbostic * that does not match the previous one in _bf. When this happens,
8646112Sbostic * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
8746112Sbostic * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
8860292Storek *
8960292Storek * NB: see WARNING above before changing the layout of this structure!
9046112Sbostic */
9146112Sbostic typedef struct __sFILE {
9246112Sbostic unsigned char *_p; /* current position in (some) buffer */
9346112Sbostic int _r; /* read space left for getc() */
9446112Sbostic int _w; /* write space left for putc() */
9546112Sbostic short _flags; /* flags, below; this FILE is free if 0 */
9646112Sbostic short _file; /* fileno, if Unix descriptor, else -1 */
9746112Sbostic struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */
9846112Sbostic int _lbfsize; /* 0 or -_bf._size, for inline putc */
9946112Sbostic
10046112Sbostic /* operations */
10146112Sbostic void *_cookie; /* cookie passed to io functions */
10246519Sdonn int (*_close) __P((void *));
10346519Sdonn int (*_read) __P((void *, char *, int));
10446519Sdonn fpos_t (*_seek) __P((void *, fpos_t, int));
10546519Sdonn int (*_write) __P((void *, const char *, int));
10646112Sbostic
10746112Sbostic /* separate buffer for long sequences of ungetc() */
10846112Sbostic struct __sbuf _ub; /* ungetc buffer */
10946112Sbostic unsigned char *_up; /* saved _p when _p is doing ungetc data */
11046112Sbostic int _ur; /* saved _r when _r is counting ungetc data */
11146112Sbostic
11246112Sbostic /* tricks to meet minimum requirements even when malloc() fails */
11346112Sbostic unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
11446112Sbostic unsigned char _nbuf[1]; /* guarantee a getc() buffer */
11546112Sbostic
11665330Sbostic /* separate buffer for fgetln() when line crosses buffer boundary */
11765330Sbostic struct __sbuf _lb; /* buffer for fgetln() */
11846112Sbostic
11946112Sbostic /* Unix stdio files get aligned to block boundaries on fseek() */
12046112Sbostic int _blksize; /* stat.st_blksize (may be != _bf._size) */
12160292Storek fpos_t _offset; /* current lseek offset (see WARNING) */
12246112Sbostic } FILE;
12346112Sbostic
12446736Sdonn __BEGIN_DECLS
12546112Sbostic extern FILE __sF[];
12646736Sdonn __END_DECLS
12746112Sbostic
12846112Sbostic #define __SLBF 0x0001 /* line buffered */
12946112Sbostic #define __SNBF 0x0002 /* unbuffered */
13046112Sbostic #define __SRD 0x0004 /* OK to read */
13146112Sbostic #define __SWR 0x0008 /* OK to write */
13246112Sbostic /* RD and WR are never simultaneously asserted */
13346112Sbostic #define __SRW 0x0010 /* open for reading & writing */
13446112Sbostic #define __SEOF 0x0020 /* found EOF */
13546112Sbostic #define __SERR 0x0040 /* found error */
13646112Sbostic #define __SMBF 0x0080 /* _buf is from malloc */
13746112Sbostic #define __SAPP 0x0100 /* fdopen()ed in append mode */
13846112Sbostic #define __SSTR 0x0200 /* this is an sprintf/snprintf string */
13946112Sbostic #define __SOPT 0x0400 /* do fseek() optimisation */
14046112Sbostic #define __SNPT 0x0800 /* do not do fseek() optimisation */
14146112Sbostic #define __SOFF 0x1000 /* set iff _offset is in fact correct */
14265330Sbostic #define __SMOD 0x2000 /* true => fgetln modified _p text */
14346112Sbostic
14446112Sbostic /*
14546112Sbostic * The following three definitions are for ANSI C, which took them
14646112Sbostic * from System V, which brilliantly took internal interface macros and
14746112Sbostic * made them official arguments to setvbuf(), without renaming them.
14846112Sbostic * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
14946112Sbostic *
15046112Sbostic * Although numbered as their counterparts above, the implementation
15146112Sbostic * does not rely on this.
15246112Sbostic */
15346112Sbostic #define _IOFBF 0 /* setvbuf should set fully buffered */
15446112Sbostic #define _IOLBF 1 /* setvbuf should set line buffered */
15546112Sbostic #define _IONBF 2 /* setvbuf should set unbuffered */
15646112Sbostic
15746112Sbostic #define BUFSIZ 1024 /* size of buffer used by setbuf */
1583165Stoy #define EOF (-1)
1593165Stoy
16046112Sbostic /*
16165402Sbostic * FOPEN_MAX is a minimum maximum, and is the number of streams that
16265402Sbostic * stdio can provide without attempting to allocate further resources
16365402Sbostic * (which could fail). Do not use this for anything.
16446112Sbostic */
16565402Sbostic /* must be == _POSIX_STREAM_MAX <limits.h> */
16646112Sbostic #define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */
16746112Sbostic #define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */
1683165Stoy
16946112Sbostic /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
17046112Sbostic #ifndef _ANSI_SOURCE
17146621Sbostic #define P_tmpdir "/var/tmp/"
17246112Sbostic #endif
17346112Sbostic #define L_tmpnam 1024 /* XXX must be == PATH_MAX */
17446112Sbostic #define TMP_MAX 308915776
17546112Sbostic
17646112Sbostic #ifndef SEEK_SET
17746112Sbostic #define SEEK_SET 0 /* set file offset to offset */
17846112Sbostic #endif
17946112Sbostic #ifndef SEEK_CUR
18046112Sbostic #define SEEK_CUR 1 /* set file offset to current plus offset */
18146112Sbostic #endif
18246112Sbostic #ifndef SEEK_END
18346112Sbostic #define SEEK_END 2 /* set file offset to EOF plus offset */
18446112Sbostic #endif
18546112Sbostic
18646112Sbostic #define stdin (&__sF[0])
18746112Sbostic #define stdout (&__sF[1])
18846112Sbostic #define stderr (&__sF[2])
18946112Sbostic
19046112Sbostic /*
19146112Sbostic * Functions defined in ANSI C standard.
19246112Sbostic */
19346142Sbostic __BEGIN_DECLS
19446142Sbostic void clearerr __P((FILE *));
19546142Sbostic int fclose __P((FILE *));
19646142Sbostic int feof __P((FILE *));
19746142Sbostic int ferror __P((FILE *));
19846142Sbostic int fflush __P((FILE *));
19946142Sbostic int fgetc __P((FILE *));
20046142Sbostic int fgetpos __P((FILE *, fpos_t *));
20146142Sbostic char *fgets __P((char *, size_t, FILE *));
20246519Sdonn FILE *fopen __P((const char *, const char *));
20346142Sbostic int fprintf __P((FILE *, const char *, ...));
20446142Sbostic int fputc __P((int, FILE *));
20546142Sbostic int fputs __P((const char *, FILE *));
20650949Sbostic size_t fread __P((void *, size_t, size_t, FILE *));
20746519Sdonn FILE *freopen __P((const char *, const char *, FILE *));
20846142Sbostic int fscanf __P((FILE *, const char *, ...));
20946142Sbostic int fseek __P((FILE *, long, int));
21046142Sbostic int fsetpos __P((FILE *, const fpos_t *));
211*69116Sbostic long ftell __P((FILE *));
21250949Sbostic size_t fwrite __P((const void *, size_t, size_t, FILE *));
21346142Sbostic int getc __P((FILE *));
21446142Sbostic int getchar __P((void));
21546142Sbostic char *gets __P((char *));
21649975Sbostic #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
21749975Sbostic extern int sys_nerr; /* perror(3) external variables */
21865375Sbostic extern __const char *__const sys_errlist[];
21949975Sbostic #endif
22046142Sbostic void perror __P((const char *));
22146142Sbostic int printf __P((const char *, ...));
22246142Sbostic int putc __P((int, FILE *));
22346142Sbostic int putchar __P((int));
22446142Sbostic int puts __P((const char *));
22546142Sbostic int remove __P((const char *));
22646519Sdonn int rename __P((const char *, const char *));
22746142Sbostic void rewind __P((FILE *));
22846142Sbostic int scanf __P((const char *, ...));
22946142Sbostic void setbuf __P((FILE *, char *));
23046142Sbostic int setvbuf __P((FILE *, char *, int, size_t));
23146142Sbostic int sprintf __P((char *, const char *, ...));
23256486Storek int sscanf __P((const char *, const char *, ...));
23346142Sbostic FILE *tmpfile __P((void));
23446142Sbostic char *tmpnam __P((char *));
23546142Sbostic int ungetc __P((int, FILE *));
23654257Sbostic int vfprintf __P((FILE *, const char *, _BSD_VA_LIST_));
23754257Sbostic int vprintf __P((const char *, _BSD_VA_LIST_));
23854257Sbostic int vsprintf __P((char *, const char *, _BSD_VA_LIST_));
23946142Sbostic __END_DECLS
24036852Skfall
24146112Sbostic /*
24246112Sbostic * Functions defined in POSIX 1003.1.
24346112Sbostic */
24446112Sbostic #ifndef _ANSI_SOURCE
24546112Sbostic #define L_cuserid 9 /* size for cuserid(); UT_NAMESIZE + 1 */
24646112Sbostic #define L_ctermid 1024 /* size for ctermid(); PATH_MAX */
24746112Sbostic
24846142Sbostic __BEGIN_DECLS
24946474Sbostic char *ctermid __P((char *));
25046142Sbostic FILE *fdopen __P((int, const char *));
25146142Sbostic int fileno __P((FILE *));
25246142Sbostic __END_DECLS
25346519Sdonn #endif /* not ANSI */
25446112Sbostic
25546112Sbostic /*
25646112Sbostic * Routines that are purely local.
25746112Sbostic */
25846519Sdonn #if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
25946142Sbostic __BEGIN_DECLS
26065330Sbostic char *fgetln __P((FILE *, size_t *));
26146142Sbostic int fpurge __P((FILE *));
26246142Sbostic int getw __P((FILE *));
26346142Sbostic int pclose __P((FILE *));
26446519Sdonn FILE *popen __P((const char *, const char *));
26546142Sbostic int putw __P((int, FILE *));
26646142Sbostic void setbuffer __P((FILE *, char *, int));
26746142Sbostic int setlinebuf __P((FILE *));
26846621Sbostic char *tempnam __P((const char *, const char *));
26946142Sbostic int snprintf __P((char *, size_t, const char *, ...));
27054257Sbostic int vsnprintf __P((char *, size_t, const char *, _BSD_VA_LIST_));
27154257Sbostic int vscanf __P((const char *, _BSD_VA_LIST_));
27254257Sbostic int vsscanf __P((const char *, const char *, _BSD_VA_LIST_));
27356968Sbostic FILE *zopen __P((const char *, const char *, int));
27446142Sbostic __END_DECLS
27546112Sbostic
27646112Sbostic /*
27747029Sdonn * This is a #define because the function is used internally and
27847029Sdonn * (unlike vfscanf) the name __svfscanf is guaranteed not to collide
27947029Sdonn * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined.
28047029Sdonn */
28147029Sdonn #define vfscanf __svfscanf
28247029Sdonn
28347029Sdonn /*
28446112Sbostic * Stdio function-access interface.
28546112Sbostic */
28646142Sbostic __BEGIN_DECLS
28746519Sdonn FILE *funopen __P((const void *,
28846519Sdonn int (*)(void *, char *, int),
28946519Sdonn int (*)(void *, const char *, int),
29046519Sdonn fpos_t (*)(void *, fpos_t, int),
29146519Sdonn int (*)(void *)));
29246142Sbostic __END_DECLS
29346112Sbostic #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
29446112Sbostic #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
29546736Sdonn #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
29646112Sbostic
29746112Sbostic /*
29846112Sbostic * Functions internal to the implementation.
29946112Sbostic */
30046736Sdonn __BEGIN_DECLS
30146142Sbostic int __srget __P((FILE *));
30254257Sbostic int __svfscanf __P((FILE *, const char *, _BSD_VA_LIST_));
30346142Sbostic int __swbuf __P((int, FILE *));
30446736Sdonn __END_DECLS
30546112Sbostic
30646112Sbostic /*
30746112Sbostic * The __sfoo macros are here so that we can
30846112Sbostic * define function versions in the C library.
30946112Sbostic */
31046112Sbostic #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
31147029Sdonn #if defined(__GNUC__) && defined(__STDC__)
__sputc(int _c,FILE * _p)31250718Sbostic static __inline int __sputc(int _c, FILE *_p) {
31346112Sbostic if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
31446112Sbostic return (*_p->_p++ = _c);
31546112Sbostic else
31646112Sbostic return (__swbuf(_c, _p));
31746112Sbostic }
31846112Sbostic #else
31946112Sbostic /*
32046112Sbostic * This has been tuned to generate reasonable code on the vax using pcc.
32146112Sbostic */
32246112Sbostic #define __sputc(c, p) \
32346112Sbostic (--(p)->_w < 0 ? \
32446112Sbostic (p)->_w >= (p)->_lbfsize ? \
32546112Sbostic (*(p)->_p = (c)), *(p)->_p != '\n' ? \
32646112Sbostic (int)*(p)->_p++ : \
32746112Sbostic __swbuf('\n', p) : \
32846112Sbostic __swbuf((int)(c), p) : \
32946112Sbostic (*(p)->_p = (c), (int)*(p)->_p++))
33046112Sbostic #endif
33146112Sbostic
33246112Sbostic #define __sfeof(p) (((p)->_flags & __SEOF) != 0)
33346112Sbostic #define __sferror(p) (((p)->_flags & __SERR) != 0)
33446112Sbostic #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
33546112Sbostic #define __sfileno(p) ((p)->_file)
33646112Sbostic
33746112Sbostic #define feof(p) __sfeof(p)
33846112Sbostic #define ferror(p) __sferror(p)
33946112Sbostic #define clearerr(p) __sclearerr(p)
34046112Sbostic
34146112Sbostic #ifndef _ANSI_SOURCE
34246112Sbostic #define fileno(p) __sfileno(p)
34346112Sbostic #endif
34446112Sbostic
34546112Sbostic #ifndef lint
34646112Sbostic #define getc(fp) __sgetc(fp)
34746112Sbostic #define putc(x, fp) __sputc(x, fp)
34846112Sbostic #endif /* lint */
34946112Sbostic
35046112Sbostic #define getchar() getc(stdin)
35146112Sbostic #define putchar(x) putc(x, stdout)
35246112Sbostic #endif /* _STDIO_H_ */
353