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