]> git.saurik.com Git - apple/libc.git/blob - include/stdio.h
4c365f98a78f05ce84af230c843976dd50d96512
[apple/libc.git] / include / stdio.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*-
26 * Copyright (c) 1990, 1993
27 * The Regents of the University of California. All rights reserved.
28 *
29 * This code is derived from software contributed to Berkeley by
30 * Chris Torek.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)stdio.h 8.5 (Berkeley) 4/29/95
61 */
62
63 #ifndef _STDIO_H_
64 #define _STDIO_H_
65
66 #if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
67 #include <sys/types.h>
68 #endif
69
70 #include <sys/cdefs.h>
71
72 #include <machine/ansi.h>
73 #ifndef _BSD_SIZE_T_DEFINED_
74 #define _BSD_SIZE_T_DEFINED_
75 typedef _BSD_SIZE_T_ size_t;
76 #endif
77
78 #ifndef NULL
79 #define NULL 0
80 #endif
81
82 #if !defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)
83 typedef off_t fpos_t;
84 #else
85 #include <machine/types.h>
86 typedef int64_t fpos_t;
87 #endif
88
89 #define _FSTDIO /* Define for new stdio with functions. */
90
91 /*
92 * NB: to fit things in six character monocase externals, the stdio
93 * code uses the prefix `__s' for stdio objects, typically followed
94 * by a three-character attempt at a mnemonic.
95 */
96
97 /* stdio buffers */
98 struct __sbuf {
99 unsigned char *_base;
100 int _size;
101 };
102
103 /* hold a buncha junk that would grow the ABI */
104 struct __sFILEX;
105
106 /*
107 * stdio state variables.
108 *
109 * The following always hold:
110 *
111 * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
112 * _lbfsize is -_bf._size, else _lbfsize is 0
113 * if _flags&__SRD, _w is 0
114 * if _flags&__SWR, _r is 0
115 *
116 * This ensures that the getc and putc macros (or inline functions) never
117 * try to write or read from a file that is in `read' or `write' mode.
118 * (Moreover, they can, and do, automatically switch from read mode to
119 * write mode, and back, on "r+" and "w+" files.)
120 *
121 * _lbfsize is used only to make the inline line-buffered output stream
122 * code as compact as possible.
123 *
124 * _ub, _up, and _ur are used when ungetc() pushes back more characters
125 * than fit in the current _bf, or when ungetc() pushes back a character
126 * that does not match the previous one in _bf. When this happens,
127 * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
128 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
129 *
130 * NB: see WARNING above before changing the layout of this structure!
131 */
132 typedef struct __sFILE {
133 unsigned char *_p; /* current position in (some) buffer */
134 int _r; /* read space left for getc() */
135 int _w; /* write space left for putc() */
136 short _flags; /* flags, below; this FILE is free if 0 */
137 short _file; /* fileno, if Unix descriptor, else -1 */
138 struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */
139 int _lbfsize; /* 0 or -_bf._size, for inline putc */
140
141 /* operations */
142 void *_cookie; /* cookie passed to io functions */
143 int (*_close)(void *);
144 int (*_read) (void *, char *, int);
145 fpos_t (*_seek) (void *, fpos_t, int);
146 int (*_write)(void *, const char *, int);
147
148 /* separate buffer for long sequences of ungetc() */
149 struct __sbuf _ub; /* ungetc buffer */
150 struct __sFILEX *_extra; /* additions to FILE to not break ABI */
151 int _ur; /* saved _r when _r is counting ungetc data */
152
153 /* tricks to meet minimum requirements even when malloc() fails */
154 unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
155 unsigned char _nbuf[1]; /* guarantee a getc() buffer */
156
157 /* separate buffer for fgetln() when line crosses buffer boundary */
158 struct __sbuf _lb; /* buffer for fgetln() */
159
160 /* Unix stdio files get aligned to block boundaries on fseek() */
161 int _blksize; /* stat.st_blksize (may be != _bf._size) */
162 fpos_t _offset; /* current lseek offset (see WARNING) */
163 } FILE;
164
165 __BEGIN_DECLS
166 extern FILE __sF[];
167 __END_DECLS
168
169 #define __SLBF 0x0001 /* line buffered */
170 #define __SNBF 0x0002 /* unbuffered */
171 #define __SRD 0x0004 /* OK to read */
172 #define __SWR 0x0008 /* OK to write */
173 /* RD and WR are never simultaneously asserted */
174 #define __SRW 0x0010 /* open for reading & writing */
175 #define __SEOF 0x0020 /* found EOF */
176 #define __SERR 0x0040 /* found error */
177 #define __SMBF 0x0080 /* _buf is from malloc */
178 #define __SAPP 0x0100 /* fdopen()ed in append mode */
179 #define __SSTR 0x0200 /* this is an sprintf/snprintf string */
180 #define __SOPT 0x0400 /* do fseek() optimisation */
181 #define __SNPT 0x0800 /* do not do fseek() optimisation */
182 #define __SOFF 0x1000 /* set iff _offset is in fact correct */
183 #define __SMOD 0x2000 /* true => fgetln modified _p text */
184 #define __SALC 0x4000 /* allocate string space dynamically */
185 #define __SIGN 0x8000 /* ignore this file in _fwalk */
186
187 /*
188 * The following three definitions are for ANSI C, which took them
189 * from System V, which brilliantly took internal interface macros and
190 * made them official arguments to setvbuf(), without renaming them.
191 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
192 *
193 * Although numbered as their counterparts above, the implementation
194 * does not rely on this.
195 */
196 #define _IOFBF 0 /* setvbuf should set fully buffered */
197 #define _IOLBF 1 /* setvbuf should set line buffered */
198 #define _IONBF 2 /* setvbuf should set unbuffered */
199
200 #define BUFSIZ 1024 /* size of buffer used by setbuf */
201 #define EOF (-1)
202
203 /*
204 * FOPEN_MAX is a minimum maximum, and is the number of streams that
205 * stdio can provide without attempting to allocate further resources
206 * (which could fail). Do not use this for anything.
207 */
208 /* must be == _POSIX_STREAM_MAX <limits.h> */
209 #define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */
210 #define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */
211
212 /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
213 #ifndef _ANSI_SOURCE
214 #define P_tmpdir "/var/tmp/"
215 #endif
216 #define L_tmpnam 1024 /* XXX must be == PATH_MAX */
217 #define TMP_MAX 308915776
218
219 #ifndef SEEK_SET
220 #define SEEK_SET 0 /* set file offset to offset */
221 #endif
222 #ifndef SEEK_CUR
223 #define SEEK_CUR 1 /* set file offset to current plus offset */
224 #endif
225 #ifndef SEEK_END
226 #define SEEK_END 2 /* set file offset to EOF plus offset */
227 #endif
228
229 #define stdin (&__sF[0])
230 #define stdout (&__sF[1])
231 #define stderr (&__sF[2])
232
233 /*
234 * Functions defined in ANSI C standard.
235 */
236 __BEGIN_DECLS
237 void clearerr(FILE *);
238 int fclose(FILE *);
239 int feof(FILE *);
240 int ferror(FILE *);
241 int fflush(FILE *);
242 int fgetc(FILE *);
243 int fgetpos(FILE *, fpos_t *);
244 char *fgets(char *, int, FILE *);
245 FILE *fopen(const char *, const char *);
246 int fprintf(FILE *, const char *, ...);
247 int fputc(int, FILE *);
248 int fputs(const char *, FILE *);
249 size_t fread(void *, size_t, size_t, FILE *);
250 FILE *freopen(const char *, const char *, FILE *);
251 int fscanf(FILE *, const char *, ...);
252 int fseek(FILE *, long, int);
253 int fsetpos(FILE *, const fpos_t *);
254 long ftell(FILE *);
255 size_t fwrite(const void *, size_t, size_t, FILE *);
256 int getc(FILE *);
257 int getchar(void);
258 char *gets(char *);
259 #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
260 extern __const int sys_nerr; /* perror(3) external variables */
261 extern __const char *__const sys_errlist[];
262 #endif
263 void perror(const char *);
264 int printf(const char *, ...);
265 int putc(int, FILE *);
266 int putchar(int);
267 int puts(const char *);
268 int remove(const char *);
269 int rename (const char *, const char *);
270 void rewind(FILE *);
271 int scanf(const char *, ...);
272 void setbuf(FILE *, char *);
273 int setvbuf(FILE *, char *, int, size_t);
274 int sprintf(char *, const char *, ...);
275 int sscanf(const char *, const char *, ...);
276 FILE *tmpfile(void);
277 char *tmpnam(char *);
278 int ungetc(int, FILE *);
279 int vfprintf(FILE *, const char *, _BSD_VA_LIST_);
280 int vprintf(const char *, _BSD_VA_LIST_);
281 int vsprintf(char *, const char *, _BSD_VA_LIST_);
282 int asprintf(char **, const char *, ...);
283 int vasprintf(char **, const char *, _BSD_VA_LIST_);
284 __END_DECLS
285
286 /*
287 * Functions defined in POSIX 1003.1.
288 */
289 #ifndef _ANSI_SOURCE
290 #define L_cuserid 9 /* size for cuserid(); UT_NAMESIZE + 1 */
291 #define L_ctermid 1024 /* size for ctermid(); PATH_MAX */
292
293 __BEGIN_DECLS
294 char *ctermid(char *);
295 char *ctermid_r(char *);
296 FILE *fdopen(int, const char *);
297 int fileno(FILE *);
298 __END_DECLS
299 #endif /* not ANSI */
300
301 /*
302 * Routines that are purely local.
303 */
304 #if !defined (_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
305 __BEGIN_DECLS
306 char *fgetln(FILE *, size_t *);
307 void flockfile(FILE *);
308 __const char
309 *fmtcheck(const char *, const char *);
310 int fpurge(FILE *);
311 int fseeko(FILE *, fpos_t, int);
312 fpos_t ftello(FILE *);
313 int ftrylockfile(FILE *);
314 void funlockfile(FILE *);
315 int getc_unlocked(FILE *);
316 int getchar_unlocked(void);
317 int getw(FILE *);
318 int pclose(FILE *);
319 FILE *popen(const char *, const char *);
320 int putc_unlocked(int, FILE *);
321 int putchar_unlocked(int);
322 int putw(int, FILE *);
323 void setbuffer(FILE *, char *, int);
324 int setlinebuf(FILE *);
325 char *tempnam(const char *, const char *);
326 int snprintf(char *, size_t, const char *, ...);
327 int vfscanf(FILE *, const char *, _BSD_VA_LIST_);
328 int vsnprintf(char *, size_t, const char *, _BSD_VA_LIST_);
329 int vscanf(const char *, _BSD_VA_LIST_);
330 int vsscanf(const char *, const char *, _BSD_VA_LIST_);
331 FILE *zopen(const char *, const char *, int);
332 __END_DECLS
333
334 /*
335 * Stdio function-access interface.
336 */
337 __BEGIN_DECLS
338 FILE *funopen(const void *,
339 int (*)(void *, char *, int),
340 int (*)(void *, const char *, int),
341 fpos_t (*)(void *, fpos_t, int),
342 int (*)(void *));
343 __END_DECLS
344 #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
345 #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
346 #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
347
348 /*
349 * Functions internal to the implementation.
350 */
351 __BEGIN_DECLS
352 int __srget(FILE *);
353 int __svfscanf(FILE *, const char *, _BSD_VA_LIST_);
354 int __swbuf(int, FILE *);
355 __END_DECLS
356
357 /*
358 * The __sfoo macros are here so that we can
359 * define function versions in the C library.
360 */
361 #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
362 #if defined(__GNUC__) && defined(__STDC__)
363 static __inline int __sputc(int _c, FILE *_p) {
364 if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
365 return (*_p->_p++ = _c);
366 else
367 return (__swbuf(_c, _p));
368 }
369 #else
370 /*
371 * This has been tuned to generate reasonable code on the vax using pcc.
372 */
373 #define __sputc(c, p) \
374 (--(p)->_w < 0 ? \
375 (p)->_w >= (p)->_lbfsize ? \
376 (*(p)->_p = (c)), *(p)->_p != '\n' ? \
377 (int)*(p)->_p++ : \
378 __swbuf('\n', p) : \
379 __swbuf((int)(c), p) : \
380 (*(p)->_p = (c), (int)*(p)->_p++))
381 #endif
382
383 #define __sfeof(p) (((p)->_flags & __SEOF) != 0)
384 #define __sferror(p) (((p)->_flags & __SERR) != 0)
385 #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
386 #define __sfileno(p) ((p)->_file)
387
388 #define feof_unlocked(p) __sfeof(p)
389 #define ferror_unlocked(p) __sferror(p)
390 #define clearerr_unlocked(p) __sclearerr(p)
391
392 #ifndef _ANSI_SOURCE
393 #define fileno_unlocked(p) __sfileno(p)
394 #endif
395
396 #ifndef lint
397 #define getc_unlocked(fp) __sgetc(fp)
398 #define putc_unlocked(x, fp) __sputc(x, fp)
399 #endif /* lint */
400
401 #define getchar_unlocked() getc_unlocked(stdin)
402 #define putchar_unlocked(x) putc_unlocked(x, stdout)
403 #endif /* _STDIO_H_ */