st_blocks are quad's now
[unix-history] / usr / src / include / stdio.h
CommitLineData
238b8fe5
KB
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * %sccs.include.redist.c%
586c39b1 9 *
446b1b9b 10 * @(#)stdio.h 5.20 (Berkeley) %G%
586c39b1
DF
11 */
12
238b8fe5
KB
13#ifndef _STDIO_H_
14#define _STDIO_H_
15
446b1b9b 16#include <sys/types.h>
5038e9e5 17#include <sys/cdefs.h>
238b8fe5 18
6124b236 19#include <machine/ansi.h>
238b8fe5
KB
20#ifdef _SIZE_T_
21typedef _SIZE_T_ size_t;
22#undef _SIZE_T_
23#endif
24
97b8a2b7
KB
25#ifndef NULL
26#define NULL 0
27#endif
28
446b1b9b 29typedef off_t fpos_t; /* Must match off_t <sys/types.h> */
238b8fe5
KB
30
31#define _FSTDIO /* Define for new stdio with functions. */
32
5038e9e5
KB
33/*
34 * NB: to fit things in six character monocase externals, the stdio
35 * code uses the prefix `__s' for stdio objects, typically followed
36 * by a three-character attempt at a mnemonic.
37 */
38
238b8fe5
KB
39/* stdio buffers */
40struct __sbuf {
41 unsigned char *_base;
42 int _size;
43};
44
45/*
46 * stdio state variables.
47 *
48 * The following always hold:
49 *
50 * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
51 * _lbfsize is -_bf._size, else _lbfsize is 0
52 * if _flags&__SRD, _w is 0
53 * if _flags&__SWR, _r is 0
54 *
55 * This ensures that the getc and putc macros (or inline functions) never
56 * try to write or read from a file that is in `read' or `write' mode.
57 * (Moreover, they can, and do, automatically switch from read mode to
58 * write mode, and back, on "r+" and "w+" files.)
59 *
60 * _lbfsize is used only to make the inline line-buffered output stream
61 * code as compact as possible.
62 *
63 * _ub, _up, and _ur are used when ungetc() pushes back more characters
64 * than fit in the current _bf, or when ungetc() pushes back a character
65 * that does not match the previous one in _bf. When this happens,
66 * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
67 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
68 */
69typedef struct __sFILE {
70 unsigned char *_p; /* current position in (some) buffer */
71 int _r; /* read space left for getc() */
72 int _w; /* write space left for putc() */
73 short _flags; /* flags, below; this FILE is free if 0 */
74 short _file; /* fileno, if Unix descriptor, else -1 */
75 struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */
76 int _lbfsize; /* 0 or -_bf._size, for inline putc */
77
78 /* operations */
79 void *_cookie; /* cookie passed to io functions */
4b22ff03
DS
80 int (*_close) __P((void *));
81 int (*_read) __P((void *, char *, int));
82 fpos_t (*_seek) __P((void *, fpos_t, int));
83 int (*_write) __P((void *, const char *, int));
238b8fe5
KB
84
85 /* separate buffer for long sequences of ungetc() */
86 struct __sbuf _ub; /* ungetc buffer */
87 unsigned char *_up; /* saved _p when _p is doing ungetc data */
88 int _ur; /* saved _r when _r is counting ungetc data */
89
90 /* tricks to meet minimum requirements even when malloc() fails */
91 unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
92 unsigned char _nbuf[1]; /* guarantee a getc() buffer */
93
94 /* separate buffer for fgetline() when line crosses buffer boundary */
95 struct __sbuf _lb; /* buffer for fgetline() */
96
97 /* Unix stdio files get aligned to block boundaries on fseek() */
98 int _blksize; /* stat.st_blksize (may be != _bf._size) */
99 int _offset; /* current lseek offset */
100} FILE;
101
d9f57e12 102__BEGIN_DECLS
238b8fe5 103extern FILE __sF[];
d9f57e12 104__END_DECLS
238b8fe5
KB
105
106#define __SLBF 0x0001 /* line buffered */
107#define __SNBF 0x0002 /* unbuffered */
108#define __SRD 0x0004 /* OK to read */
109#define __SWR 0x0008 /* OK to write */
110 /* RD and WR are never simultaneously asserted */
111#define __SRW 0x0010 /* open for reading & writing */
112#define __SEOF 0x0020 /* found EOF */
113#define __SERR 0x0040 /* found error */
114#define __SMBF 0x0080 /* _buf is from malloc */
115#define __SAPP 0x0100 /* fdopen()ed in append mode */
116#define __SSTR 0x0200 /* this is an sprintf/snprintf string */
117#define __SOPT 0x0400 /* do fseek() optimisation */
118#define __SNPT 0x0800 /* do not do fseek() optimisation */
119#define __SOFF 0x1000 /* set iff _offset is in fact correct */
120#define __SMOD 0x2000 /* true => fgetline modified _p text */
121
122/*
123 * The following three definitions are for ANSI C, which took them
124 * from System V, which brilliantly took internal interface macros and
125 * made them official arguments to setvbuf(), without renaming them.
126 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
127 *
128 * Although numbered as their counterparts above, the implementation
129 * does not rely on this.
130 */
131#define _IOFBF 0 /* setvbuf should set fully buffered */
132#define _IOLBF 1 /* setvbuf should set line buffered */
133#define _IONBF 2 /* setvbuf should set unbuffered */
134
135#define BUFSIZ 1024 /* size of buffer used by setbuf */
7002c7bd
MT
136#define EOF (-1)
137
238b8fe5
KB
138/*
139 * FOPEN_MAX is a minimum maximum, and should be the number of descriptors
140 * that the kernel can provide without allocation of a resource that can
141 * fail without the process sleeping. Do not use this for anything.
142 */
143#define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */
144#define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */
7002c7bd 145
238b8fe5
KB
146/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
147#ifndef _ANSI_SOURCE
45c3183f 148#define P_tmpdir "/var/tmp/"
238b8fe5
KB
149#endif
150#define L_tmpnam 1024 /* XXX must be == PATH_MAX */
151#define TMP_MAX 308915776
152
153#ifndef SEEK_SET
154#define SEEK_SET 0 /* set file offset to offset */
155#endif
156#ifndef SEEK_CUR
157#define SEEK_CUR 1 /* set file offset to current plus offset */
158#endif
159#ifndef SEEK_END
160#define SEEK_END 2 /* set file offset to EOF plus offset */
161#endif
162
163#define stdin (&__sF[0])
164#define stdout (&__sF[1])
165#define stderr (&__sF[2])
166
167/*
168 * Functions defined in ANSI C standard.
169 */
5038e9e5
KB
170__BEGIN_DECLS
171void clearerr __P((FILE *));
172int fclose __P((FILE *));
173int feof __P((FILE *));
174int ferror __P((FILE *));
175int fflush __P((FILE *));
176int fgetc __P((FILE *));
177int fgetpos __P((FILE *, fpos_t *));
178char *fgets __P((char *, size_t, FILE *));
4b22ff03 179FILE *fopen __P((const char *, const char *));
5038e9e5
KB
180int fprintf __P((FILE *, const char *, ...));
181int fputc __P((int, FILE *));
182int fputs __P((const char *, FILE *));
ccb7e87d 183size_t fread __P((void *, size_t, size_t, FILE *));
4b22ff03 184FILE *freopen __P((const char *, const char *, FILE *));
5038e9e5
KB
185int fscanf __P((FILE *, const char *, ...));
186int fseek __P((FILE *, long, int));
187int fsetpos __P((FILE *, const fpos_t *));
188long ftell __P((const FILE *));
ccb7e87d 189size_t fwrite __P((const void *, size_t, size_t, FILE *));
5038e9e5
KB
190int getc __P((FILE *));
191int getchar __P((void));
192char *gets __P((char *));
c3ae73e3
KB
193#if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
194extern int sys_nerr; /* perror(3) external variables */
195extern char *sys_errlist[];
196#endif
5038e9e5
KB
197void perror __P((const char *));
198int printf __P((const char *, ...));
199int putc __P((int, FILE *));
200int putchar __P((int));
201int puts __P((const char *));
202int remove __P((const char *));
4b22ff03 203int rename __P((const char *, const char *));
5038e9e5
KB
204void rewind __P((FILE *));
205int scanf __P((const char *, ...));
206void setbuf __P((FILE *, char *));
207int setvbuf __P((FILE *, char *, int, size_t));
208int sprintf __P((char *, const char *, ...));
209int sscanf __P((char *, const char *, ...));
210FILE *tmpfile __P((void));
211char *tmpnam __P((char *));
212int ungetc __P((int, FILE *));
213int vfprintf __P((FILE *, const char *, _VA_LIST_));
214int vprintf __P((const char *, _VA_LIST_));
215int vsprintf __P((char *, const char *, _VA_LIST_));
216__END_DECLS
238b8fe5
KB
217
218/*
219 * Functions defined in POSIX 1003.1.
220 */
221#ifndef _ANSI_SOURCE
222#define L_cuserid 9 /* size for cuserid(); UT_NAMESIZE + 1 */
223#define L_ctermid 1024 /* size for ctermid(); PATH_MAX */
224
5038e9e5 225__BEGIN_DECLS
41dd63a8 226char *ctermid __P((char *));
5038e9e5
KB
227FILE *fdopen __P((int, const char *));
228int fileno __P((FILE *));
229__END_DECLS
4b22ff03 230#endif /* not ANSI */
238b8fe5
KB
231
232/*
233 * Routines that are purely local.
234 */
4b22ff03 235#if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
5038e9e5
KB
236__BEGIN_DECLS
237char *fgetline __P((FILE *, size_t *));
238int fpurge __P((FILE *));
239int getw __P((FILE *));
240int pclose __P((FILE *));
4b22ff03 241FILE *popen __P((const char *, const char *));
5038e9e5
KB
242int putw __P((int, FILE *));
243void setbuffer __P((FILE *, char *, int));
244int setlinebuf __P((FILE *));
45c3183f 245char *tempnam __P((const char *, const char *));
5038e9e5
KB
246int snprintf __P((char *, size_t, const char *, ...));
247int vsnprintf __P((char *, size_t, const char *, _VA_LIST_));
29d5a679
DS
248int vscanf __P((const char *, _VA_LIST_));
249int vsscanf __P((const char *, const char *, _VA_LIST_));
5038e9e5 250__END_DECLS
cede253f 251
6b5bab0e
DS
252/*
253 * This is a #define because the function is used internally and
254 * (unlike vfscanf) the name __svfscanf is guaranteed not to collide
255 * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined.
256 */
257#define vfscanf __svfscanf
258
238b8fe5
KB
259/*
260 * Stdio function-access interface.
261 */
5038e9e5 262__BEGIN_DECLS
4b22ff03
DS
263FILE *funopen __P((const void *,
264 int (*)(void *, char *, int),
265 int (*)(void *, const char *, int),
266 fpos_t (*)(void *, fpos_t, int),
267 int (*)(void *)));
5038e9e5 268__END_DECLS
238b8fe5
KB
269#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
270#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
d9f57e12 271#endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
238b8fe5
KB
272
273/*
274 * Functions internal to the implementation.
275 */
d9f57e12 276__BEGIN_DECLS
5038e9e5 277int __srget __P((FILE *));
6b5bab0e 278int __svfscanf __P((FILE *, const char *, _VA_LIST_));
5038e9e5 279int __swbuf __P((int, FILE *));
d9f57e12 280__END_DECLS
238b8fe5
KB
281
282/*
283 * The __sfoo macros are here so that we can
284 * define function versions in the C library.
285 */
286#define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
6b5bab0e 287#if defined(__GNUC__) && defined(__STDC__)
a0b1156b 288static __inline int __sputc(int _c, FILE *_p) {
238b8fe5
KB
289 if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
290 return (*_p->_p++ = _c);
291 else
292 return (__swbuf(_c, _p));
293}
294#else
295/*
296 * This has been tuned to generate reasonable code on the vax using pcc.
297 */
298#define __sputc(c, p) \
299 (--(p)->_w < 0 ? \
300 (p)->_w >= (p)->_lbfsize ? \
301 (*(p)->_p = (c)), *(p)->_p != '\n' ? \
302 (int)*(p)->_p++ : \
303 __swbuf('\n', p) : \
304 __swbuf((int)(c), p) : \
305 (*(p)->_p = (c), (int)*(p)->_p++))
306#endif
307
308#define __sfeof(p) (((p)->_flags & __SEOF) != 0)
309#define __sferror(p) (((p)->_flags & __SERR) != 0)
310#define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
311#define __sfileno(p) ((p)->_file)
312
313#define feof(p) __sfeof(p)
314#define ferror(p) __sferror(p)
315#define clearerr(p) __sclearerr(p)
316
317#ifndef _ANSI_SOURCE
318#define fileno(p) __sfileno(p)
319#endif
320
321#ifndef lint
322#define getc(fp) __sgetc(fp)
323#define putc(x, fp) __sputc(x, fp)
324#endif /* lint */
325
326#define getchar() getc(stdin)
327#define putchar(x) putc(x, stdout)
328#endif /* _STDIO_H_ */