Remove extra whitespace.
[dragonfly.git] / include / stdio.h
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)stdio.h 8.5 (Berkeley) 4/29/95
37 * $FreeBSD: src/include/stdio.h,v 1.24.2.5 2002/11/09 08:07:20 imp Exp $
e4dcda4d 38 * $DragonFly: src/include/stdio.h,v 1.14 2008/06/05 17:53:10 swildner Exp $
984263bc
MD
39 */
40
41#ifndef _STDIO_H_
42#define _STDIO_H_
43
44#include <sys/cdefs.h>
e2565a42
MD
45#ifndef _SYS_STDINT_H_
46#include <sys/stdint.h>
47#endif
48#ifndef _MACHINE_STDARG_H_
49#include <machine/stdarg.h>
50#endif
984263bc 51
a81b3ef3
JR
52#ifndef _SIZE_T_DECLARED
53#define _SIZE_T_DECLARED
e2565a42 54typedef __size_t size_t;
984263bc
MD
55#endif
56
e4dcda4d 57#include <sys/_null.h>
984263bc 58
e2565a42 59typedef __off_t fpos_t;
984263bc 60
984263bc
MD
61/*
62 * stdio state variables.
63 *
64 * The following always hold:
65 *
66 * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
67 * _lbfsize is -_bf._size, else _lbfsize is 0
68 * if _flags&__SRD, _w is 0
69 * if _flags&__SWR, _r is 0
70 *
71 * This ensures that the getc and putc macros (or inline functions) never
72 * try to write or read from a file that is in `read' or `write' mode.
73 * (Moreover, they can, and do, automatically switch from read mode to
74 * write mode, and back, on "r+" and "w+" files.)
75 *
76 * _lbfsize is used only to make the inline line-buffered output stream
77 * code as compact as possible.
78 *
55b5af84 79 * WARNING: Do not change the order of the fields in __FILE_public!
984263bc 80 */
55b5af84
JS
81typedef struct __FILE FILE;
82
83struct __FILE_public {
84 unsigned char *_p; /* current position in (some) buffer */
85 int _flags; /* flags, below; this FILE is free if 0 */
86 int _fileno; /* fileno, if Unix descriptor, else -1 */
87 __ssize_t _r; /* read space left for getc() */
88 __ssize_t _w; /* write space left for putc() */
89 __ssize_t _lbfsize; /* 0 or -_bf._size, for inline putc */
90};
984263bc
MD
91
92__BEGIN_DECLS
93extern FILE *__stdinp, *__stdoutp, *__stderrp;
94__END_DECLS
95
96#define __SLBF 0x0001 /* line buffered */
97#define __SNBF 0x0002 /* unbuffered */
98#define __SRD 0x0004 /* OK to read */
99#define __SWR 0x0008 /* OK to write */
100 /* RD and WR are never simultaneously asserted */
101#define __SRW 0x0010 /* open for reading & writing */
102#define __SEOF 0x0020 /* found EOF */
103#define __SERR 0x0040 /* found error */
104#define __SMBF 0x0080 /* _buf is from malloc */
105#define __SAPP 0x0100 /* fdopen()ed in append mode */
106#define __SSTR 0x0200 /* this is an sprintf/snprintf string */
107#define __SOPT 0x0400 /* do fseek() optimization */
108#define __SNPT 0x0800 /* do not do fseek() optimization */
109#define __SOFF 0x1000 /* set iff _offset is in fact correct */
110#define __SMOD 0x2000 /* true => fgetln modified _p text */
111#define __SALC 0x4000 /* allocate string space dynamically */
112
113/*
114 * The following three definitions are for ANSI C, which took them
115 * from System V, which brilliantly took internal interface macros and
116 * made them official arguments to setvbuf(), without renaming them.
117 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
118 *
119 * Although numbered as their counterparts above, the implementation
120 * does not rely on this.
121 */
122#define _IOFBF 0 /* setvbuf should set fully buffered */
123#define _IOLBF 1 /* setvbuf should set line buffered */
124#define _IONBF 2 /* setvbuf should set unbuffered */
125
126#define BUFSIZ 1024 /* size of buffer used by setbuf */
127#define EOF (-1)
128
129/*
130 * FOPEN_MAX is a minimum maximum, and is the number of streams that
131 * stdio can provide without attempting to allocate further resources
132 * (which could fail). Do not use this for anything.
133 */
134 /* must be == _POSIX_STREAM_MAX <limits.h> */
135#define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */
136#define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */
137
138/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
139#ifndef _ANSI_SOURCE
3986dc9b 140#define P_tmpdir "/tmp/"
984263bc
MD
141#endif
142#define L_tmpnam 1024 /* XXX must be == PATH_MAX */
143#define TMP_MAX 308915776
144
145#ifndef SEEK_SET
146#define SEEK_SET 0 /* set file offset to offset */
147#endif
148#ifndef SEEK_CUR
149#define SEEK_CUR 1 /* set file offset to current plus offset */
150#endif
151#ifndef SEEK_END
152#define SEEK_END 2 /* set file offset to EOF plus offset */
153#endif
154
155#define stdin (__stdinp)
156#define stdout (__stdoutp)
157#define stderr (__stderrp)
158
159/*
160 * Functions defined in ANSI C standard.
161 */
162__BEGIN_DECLS
ca39809d
JS
163void clearerr(FILE *);
164int fclose(FILE *);
165int feof(FILE *);
1d9a26a3 166int feof_unlocked(FILE *);
ca39809d 167int ferror(FILE *);
1d9a26a3 168int ferror_unlocked(FILE *);
ca39809d
JS
169int fflush(FILE *);
170int fgetc(FILE *);
171int fgetpos(FILE *, fpos_t *);
172char *fgets(char *, int, FILE *);
173FILE *fopen(const char *, const char *);
174int fprintf(FILE *, const char *, ...);
175int fputc(int, FILE *);
176int fputs(const char *, FILE *);
177size_t fread(void *, size_t, size_t, FILE *);
178FILE *freopen(const char *, const char *, FILE *);
179int fscanf(FILE *, const char *, ...);
180int fseek(FILE *, long, int);
181int fsetpos(FILE *, const fpos_t *);
182long ftell(FILE *);
183size_t fwrite(const void *, size_t, size_t, FILE *);
184int getc(FILE *);
1d9a26a3 185int getc_unlocked(FILE *);
ca39809d 186int getchar(void);
1d9a26a3 187int getchar_unlocked(void);
ca39809d 188char *gets(char *);
984263bc
MD
189#if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
190extern __const int sys_nerr; /* perror(3) external variables */
191extern __const char *__const sys_errlist[];
192#endif
ca39809d
JS
193void perror(const char *);
194int printf(const char *, ...);
195int putc(int, FILE *);
1d9a26a3 196int putc_unlocked(int, FILE *);
ca39809d 197int putchar(int);
1d9a26a3 198int putchar_unlocked(int);
ca39809d
JS
199int puts(const char *);
200int remove(const char *);
201int rename(const char *, const char *);
202void rewind(FILE *);
203int scanf(const char *, ...);
204void setbuf(FILE *, char *);
205int setvbuf(FILE *, char *, int, size_t);
206int sprintf(char *, const char *, ...);
207int sscanf(const char *, const char *, ...);
208FILE *tmpfile(void);
209char *tmpnam(char *);
210int ungetc(int, FILE *);
211int vfprintf(FILE *, const char *, __va_list);
212int vprintf(const char *, __va_list);
213int vsprintf(char *, const char *, __va_list);
984263bc
MD
214__END_DECLS
215
216/*
217 * Functions defined in POSIX 1003.1.
218 */
219#ifndef _ANSI_SOURCE
220/* size for cuserid(3); UT_NAMESIZE + 1, see <utmp.h> */
221#define L_cuserid 17
222
223#define L_ctermid 1024 /* size for ctermid(3); PATH_MAX */
224
225__BEGIN_DECLS
ca39809d
JS
226char *ctermid(char *);
227FILE *fdopen(int, const char *);
228int fileno(FILE *);
1d9a26a3 229int fileno_unlocked(FILE *);
ca39809d
JS
230int ftrylockfile(FILE *);
231void flockfile(FILE *);
232void funlockfile(FILE *);
984263bc
MD
233__END_DECLS
234#endif /* not ANSI */
235
236/*
237 * Portability hacks. See <sys/types.h>.
238 */
239#if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
240__BEGIN_DECLS
241#ifndef _FTRUNCATE_DECLARED
242#define _FTRUNCATE_DECLARED
ca39809d 243int ftruncate(int, __off_t);
984263bc
MD
244#endif
245#ifndef _LSEEK_DECLARED
246#define _LSEEK_DECLARED
ca39809d 247__off_t lseek(int, __off_t, int);
984263bc
MD
248#endif
249#ifndef _MMAP_DECLARED
250#define _MMAP_DECLARED
ca39809d 251void *mmap(void *, size_t, int, int, int, __off_t);
984263bc
MD
252#endif
253#ifndef _TRUNCATE_DECLARED
254#define _TRUNCATE_DECLARED
ca39809d 255int truncate(const char *, __off_t);
984263bc
MD
256#endif
257__END_DECLS
258#endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
259
260/*
261 * Routines that are purely local.
262 */
263#if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
264__BEGIN_DECLS
ca39809d
JS
265int asprintf(char **, const char *, ...) __printflike(2, 3);
266char *ctermid_r(char *);
55b5af84 267void *fcookie(FILE *);
ca39809d 268char *fgetln(FILE *, size_t *);
984263bc
MD
269#if __GNUC__ == 2 && __GNUC_MINOR__ >= 7 || __GNUC__ >= 3
270#define __ATTR_FORMAT_ARG __attribute__((__format_arg__(2)))
271#else
272#define __ATTR_FORMAT_ARG
273#endif
ca39809d 274const char *fmtcheck(const char *, const char *) __ATTR_FORMAT_ARG;
55b9a823 275__ssize_t __fpending(const FILE *);
ca39809d
JS
276int fpurge(FILE *);
277int fseeko(FILE *, __off_t, int);
278__off_t ftello(FILE *);
279int getw(FILE *);
280int pclose(FILE *);
281FILE *popen(const char *, const char *);
282int putw(int, FILE *);
283void setbuffer(FILE *, char *, int);
284int setlinebuf(FILE *);
285char *tempnam(const char *, const char *);
286int snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
287int vasprintf(char **, const char *, __va_list)
984263bc 288 __printflike(2, 0);
7c4af8e1 289int vsnprintf (char *, size_t, const char *, __va_list)
984263bc 290 __printflike(3, 0);
ca39809d
JS
291int vscanf(const char *, __va_list) __scanflike(1, 0);
292int vsscanf(const char *, const char *, __va_list)
984263bc
MD
293 __scanflike(2, 0);
294__END_DECLS
295
296/*
297 * This is a #define because the function is used internally and
17ea2221 298 * (unlike vfscanf) the name __vfscanf is guaranteed not to collide
984263bc
MD
299 * with a user function when _ANSI_SOURCE or _POSIX_SOURCE is defined.
300 */
17ea2221 301#define vfscanf __vfscanf
984263bc
MD
302
303/*
304 * Stdio function-access interface.
305 */
306__BEGIN_DECLS
ca39809d
JS
307FILE *funopen(const void *,
308 int (*)(void *, char *, int),
309 int (*)(void *, const char *, int),
310 fpos_t (*)(void *, fpos_t, int),
311 int (*)(void *));
984263bc
MD
312__END_DECLS
313#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
314#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
315#endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
316
317/*
318 * Functions internal to the implementation.
319 */
320__BEGIN_DECLS
ca39809d
JS
321int __srget(FILE *);
322int __vfscanf(FILE *, const char *, __va_list);
323int __svfscanf(FILE *, const char *, __va_list);
324int __swbuf(int, FILE *);
dc6df0be 325size_t __sreadahead(FILE *);
984263bc
MD
326__END_DECLS
327
328/*
55b5af84
JS
329 * The __sfoo functions are here so that we can
330 * define real function versions in the C library.
984263bc 331 */
55b5af84
JS
332static __inline int
333__sgetc(FILE *_fp)
334{
335 struct __FILE_public *_p = (struct __FILE_public *)_fp;
336
337 if (--_p->_r < 0)
338 return (__srget(_fp));
339 else
340 return (*_p->_p++);
341}
342
343static __inline int
344__sputc(int _c, FILE *_fp)
345{
346 struct __FILE_public *_p = (struct __FILE_public *)_fp;
347
348 if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && _c != '\n'))
984263bc
MD
349 return (*_p->_p++ = _c);
350 else
55b5af84 351 return (__swbuf(_c, _fp));
984263bc 352}
984263bc 353
55b5af84
JS
354static __inline int
355__sfeof(FILE *_fp)
356{
357 struct __FILE_public *_p = (struct __FILE_public *)_fp;
358
359 return ((_p->_flags & __SEOF) != 0);
360}
361
362static __inline int
363__sferror(FILE *_fp)
364{
365 struct __FILE_public *_p = (struct __FILE_public *)_fp;
366
367 return ((_p->_flags & __SERR) != 0);
368}
369
370static __inline void
371__sclearerr(FILE *_fp)
372{
373 struct __FILE_public *_p = (struct __FILE_public *)_fp;
374
375 _p->_flags &= ~(__SERR|__SEOF);
376}
377
378static __inline int
379__sfileno(FILE *_fp)
380{
381 struct __FILE_public *_p = (struct __FILE_public *)_fp;
382
383 return (_p->_fileno);
384}
984263bc
MD
385
386/*
387 * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
388 * B.8.2.7 for the rationale behind the *_unlocked() macros.
389 */
390#define feof_unlocked(p) __sfeof(p)
391#define ferror_unlocked(p) __sferror(p)
392#define clearerr_unlocked(p) __sclearerr(p)
393
394#ifndef _ANSI_SOURCE
395#define fileno_unlocked(p) __sfileno(p)
396#endif
397
17ea2221
MD
398#define getc_unlocked(fp) __sgetc(fp)
399#define putc_unlocked(x, fp) __sputc(x, fp)
984263bc 400
984263bc 401#define getchar_unlocked() getc_unlocked(stdin)
984263bc
MD
402#define putchar_unlocked(x) putc_unlocked(x, stdout)
403
404#endif /* !_STDIO_H_ */