source: trunk/src/emx/include/stdio.h@ 1454

Last change on this file since 1454 was 1454, checked in by bird, 21 years ago

Joined with the fork() tree from netlabs.cvs.

  • Property cvs2svn:cvs-rev set to 1.12
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 18.3 KB
Line 
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.51 2003/01/13 08:41:47 tjr Exp $
38 */
39
40/** @file
41 * FreeBSD 5.1
42 * @changed bird: EMX isms + LIBC implementation specifics.
43 * @chagned bird: Made quite a few @todos on function which aren't implemented.
44 */
45
46#ifndef _STDIO_H_
47#define _STDIO_H_
48
49#include <sys/cdefs.h>
50#include <sys/_types.h>
51
52typedef __off_t fpos_t;
53
54#if !defined(_SIZE_T_DECLARED) && !defined(_SIZE_T) /* bird: emx */
55typedef __size_t size_t;
56#define _SIZE_T_DECLARED
57#define _SIZE_T /* bird: emx */
58#endif
59
60#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
61#if !defined(_VA_LIST_DECLARED) && !defined(_VA_LIST) /* bird: emx */
62typedef __va_list va_list;
63#define _VA_LIST_DECLARED
64#define _VA_LIST /* bird: emx */
65#endif
66#endif
67
68#ifndef NULL
69#define NULL 0
70#endif
71
72#if 0 /* bird: emx */
73
74#define _FSTDIO /* Define for new stdio with functions. */
75
76
77/*
78 * NB: to fit things in six character monocase externals, the stdio
79 * code uses the prefix `__s' for stdio objects, typically followed
80 * by a three-character attempt at a mnemonic.
81 */
82
83/* stdio buffers */
84struct __sbuf {
85 unsigned char *_base;
86 int _size;
87};
88
89/* hold a buncha junk that would grow the ABI */
90struct __sFILEX;
91
92/*
93 * stdio state variables.
94 *
95 * The following always hold:
96 *
97 * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
98 * _lbfsize is -_bf._size, else _lbfsize is 0
99 * if _flags&__SRD, _w is 0
100 * if _flags&__SWR, _r is 0
101 *
102 * This ensures that the getc and putc macros (or inline functions) never
103 * try to write or read from a file that is in `read' or `write' mode.
104 * (Moreover, they can, and do, automatically switch from read mode to
105 * write mode, and back, on "r+" and "w+" files.)
106 *
107 * _lbfsize is used only to make the inline line-buffered output stream
108 * code as compact as possible.
109 *
110 * _ub, _up, and _ur are used when ungetc() pushes back more characters
111 * than fit in the current _bf, or when ungetc() pushes back a character
112 * that does not match the previous one in _bf. When this happens,
113 * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
114 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
115 *
116 * NB: see WARNING above before changing the layout of this structure!
117 */
118typedef struct __sFILE {
119 unsigned char *_p; /* current position in (some) buffer */
120 int _r; /* read space left for getc() */
121 int _w; /* write space left for putc() */
122 short _flags; /* flags, below; this FILE is free if 0 */
123 short _file; /* fileno, if Unix descriptor, else -1 */
124 struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */
125 int _lbfsize; /* 0 or -_bf._size, for inline putc */
126
127 /* operations */
128 void *_cookie; /* cookie passed to io functions */
129 int (*_close)(void *);
130 int (*_read)(void *, char *, int);
131 fpos_t (*_seek)(void *, fpos_t, int);
132 int (*_write)(void *, const char *, int);
133
134 /* separate buffer for long sequences of ungetc() */
135 struct __sbuf _ub; /* ungetc buffer */
136 struct __sFILEX *_extra; /* additions to FILE to not break ABI */
137 int _ur; /* saved _r when _r is counting ungetc data */
138
139 /* tricks to meet minimum requirements even when malloc() fails */
140 unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
141 unsigned char _nbuf[1]; /* guarantee a getc() buffer */
142
143 /* separate buffer for fgetln() when line crosses buffer boundary */
144 struct __sbuf _lb; /* buffer for fgetln() */
145
146 /* Unix stdio files get aligned to block boundaries on fseek() */
147 int _blksize; /* stat.st_blksize (may be != _bf._size) */
148 fpos_t _offset; /* current lseek offset (see WARNING) */
149} FILE;
150
151__BEGIN_DECLS
152extern FILE *__stdinp;
153extern FILE *__stdoutp;
154extern FILE *__stderrp;
155__END_DECLS
156
157#define __SLBF 0x0001 /* line buffered */
158#define __SNBF 0x0002 /* unbuffered */
159#define __SRD 0x0004 /* OK to read */
160#define __SWR 0x0008 /* OK to write */
161 /* RD and WR are never simultaneously asserted */
162#define __SRW 0x0010 /* open for reading & writing */
163#define __SEOF 0x0020 /* found EOF */
164#define __SERR 0x0040 /* found error */
165#define __SMBF 0x0080 /* _buf is from malloc */
166#define __SAPP 0x0100 /* fdopen()ed in append mode */
167#define __SSTR 0x0200 /* this is an sprintf/snprintf string */
168#define __SOPT 0x0400 /* do fseek() optimization */
169#define __SNPT 0x0800 /* do not do fseek() optimization */
170#define __SOFF 0x1000 /* set iff _offset is in fact correct */
171#define __SMOD 0x2000 /* true => fgetln modified _p text */
172#define __SALC 0x4000 /* allocate string space dynamically */
173#define __SIGN 0x8000 /* ignore this file in _fwalk */
174
175#else /* bird: EMX specific FILE stuff starts. */
176
177#define _FILE_T
178#define _FILE_MEMBERS_HAVE_UNDERSCORE
179struct _file2;
180typedef struct _FILE
181{
182 char * _ptr;
183 char * _buffer;
184 int _rcount;
185 int _wcount;
186 int _handle;
187 int _flags;
188 int _buf_size;
189 int _tmpidx;
190 int _pid;
191 char _char_buf;
192 unsigned char _ungetc_count;
193 short _mbstate;
194 int (*_flush)(struct _FILE *, int);
195
196 /** The Mutex Semaphore. */
197 union
198 {
199#if defined (_SYS_FMUTEX_H)
200 _fmutex __fsem;
201#endif
202 char __rsem_ersatz[8];
203 } __u;
204 /** Pointer to the stream vector which thie FILE belongs to.
205 * This member is the first which should not be zeroed by _newstream()! */
206 void *__pSV;
207} FILE;
208
209__BEGIN_DECLS
210extern FILE *__stdinp;
211extern FILE *__stdoutp;
212extern FILE *__stderrp;
213__END_DECLS
214
215#endif /* bird: EMX specific FILE stuff ends. */
216
217
218/*
219 * The following three definitions are for ANSI C, which took them
220 * from System V, which brilliantly took internal interface macros and
221 * made them official arguments to setvbuf(), without renaming them.
222 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
223 *
224 * Although numbered as their counterparts above, the implementation
225 * does not rely on this.
226 */
227#define _IOFBF 0 /* setvbuf should set fully buffered */
228#define _IOLBF 0x20 /* bird: emx, was 1 */ /* setvbuf should set line buffered */
229#define _IONBF 0x40 /* bird: emx, was 2 */ /* setvbuf should set unbuffered */
230
231#define BUFSIZ 8192 /* bird: emx, was 1024 */ /* size of buffer used by setbuf */
232#define EOF (-1)
233
234/*
235 * FOPEN_MAX is a minimum maximum, and is the number of streams that
236 * stdio can provide without attempting to allocate further resources
237 * (which could fail). Do not use this for anything.
238 */
239 /* must be == _POSIX_STREAM_MAX <limits.h> */
240#define FOPEN_MAX 14 /* bird: emx, was 20 */ /* must be <= OPEN_MAX <sys/syslimits.h> */
241#define FILENAME_MAX 260 /* bird: emx, was 1024 */ /* must be <= PATH_MAX <sys/syslimits.h> */
242
243/* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
244#if __XSI_VISIBLE
245#define P_tmpdir "." /* bird: emx, was "/var/tmp/" */
246#endif
247#define L_tmpnam 260 /* bird: emx, was 1024 */ /* XXX must be == PATH_MAX */
248#define TMP_MAX 1000 /* bird: emx, was 308915776 */
249
250#ifndef SEEK_SET
251#define SEEK_SET 0 /* set file offset to offset */
252#endif
253#ifndef SEEK_CUR
254#define SEEK_CUR 1 /* set file offset to current plus offset */
255#endif
256#ifndef SEEK_END
257#define SEEK_END 2 /* set file offset to EOF plus offset */
258#endif
259
260#define stdin __stdinp
261#define stdout __stdoutp
262#define stderr __stderrp
263
264__BEGIN_DECLS
265/*
266 * Functions defined in ANSI C standard.
267 */
268void clearerr(FILE *);
269int fclose(FILE *);
270int feof(FILE *);
271int ferror(FILE *);
272int fflush(FILE *);
273int fgetc(FILE *);
274int fgetpos(FILE * __restrict, fpos_t * __restrict);
275char *fgets(char * __restrict, int, FILE * __restrict);
276FILE *fopen(const char * __restrict, const char * __restrict);
277int fprintf(FILE * __restrict, const char * __restrict, ...);
278int fputc(int, FILE *);
279int fputs(const char * __restrict, FILE * __restrict);
280size_t fread(void * __restrict, size_t, size_t, FILE * __restrict);
281FILE *freopen(const char * __restrict, const char * __restrict, FILE * __restrict);
282int fscanf(FILE * __restrict, const char * __restrict, ...);
283int fseek(FILE *, long, int);
284int fsetpos(FILE *, const fpos_t *);
285long ftell(FILE *);
286size_t fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
287#if 0 /* bird: emx */
288int getc(FILE *);
289#else /* bird: emx */
290/** @todo: Check the standard, if this is correct or not. declaration might be required. */
291#define getc(s) fgetc(s) /* bird: emx */
292#endif /* bird: emx */
293int getchar(void);
294char *gets(char *);
295void perror(const char *);
296int printf(const char * __restrict, ...);
297#if 0 /* bird: emx */
298int putc(int, FILE *);
299#else /* bird: emx */
300/** @todo: Check the standard, if this is correct or not. declaration might be required. */
301#define putc(c,s) fputc(c,s) /* bird: emx */
302#endif /* bird: emx */
303int putchar(int);
304int puts(const char *);
305int remove(const char *);
306int rename(const char *, const char *);
307void rewind(FILE *);
308int scanf(const char * __restrict, ...);
309void setbuf(FILE * __restrict, char * __restrict);
310int setvbuf(FILE * __restrict, char * __restrict, int, size_t);
311int sprintf(char * __restrict, const char * __restrict, ...);
312int sscanf(const char * __restrict, const char * __restrict, ...);
313FILE *tmpfile(void);
314char *tmpnam(char *);
315int ungetc(int, FILE *);
316int vfprintf(FILE * __restrict, const char * __restrict,
317 __va_list);
318int vprintf(const char * __restrict, __va_list);
319int vsprintf(char * __restrict, const char * __restrict,
320 __va_list);
321
322#if __ISO_C_VISIBLE >= 1999
323int snprintf(char * __restrict, size_t, const char * __restrict,
324 ...) __printflike(3, 4);
325int vfscanf(FILE * __restrict, const char * __restrict, __va_list)
326 __scanflike(2, 0);
327int vscanf(const char * __restrict, __va_list) __scanflike(1, 0);
328int vsnprintf(char * __restrict, size_t, const char * __restrict,
329 __va_list) __printflike(3, 0);
330int vsscanf(const char * __restrict, const char * __restrict, __va_list)
331 __scanflike(2, 0);
332#endif
333
334/*
335 * Functions defined in all versions of POSIX 1003.1.
336 */
337#if __BSD_VISIBLE || __POSIX_VISIBLE <= 199506
338/* size for cuserid(3); UT_NAMESIZE + 1, see <utmp.h> */
339#define L_cuserid 9 /* bird: emx, was 17 */ /* legacy */
340#endif
341
342#if __POSIX_VISIBLE
343#define L_ctermid 260 /* bird: emx, was 1024 */ /* size for ctermid(3); PATH_MAX */
344
345/** @todo char *ctermid(char *); */
346FILE *fdopen(int, const char *);
347int fileno(FILE *);
348#endif /* __POSIX_VISIBLE */
349
350#if __POSIX_VISIBLE >= 199209
351int pclose(FILE *);
352FILE *popen(const char *, const char *);
353#endif
354
355#if __POSIX_VISIBLE >= 199506
356/** @todo int ftrylockfile(FILE *); */
357/** @todo void flockfile(FILE *); */
358/** @todo void funlockfile(FILE *); */
359
360/*
361 * These are normally used through macros as defined below, but POSIX
362 * requires functions as well.
363 */
364/** @todo int getc_unlocked(FILE *); */
365/** @todo int getchar_unlocked(void); */
366/** @todo int putc_unlocked(int, FILE *); */
367/** @todo int putchar_unlocked(int); */
368#endif
369#if __BSD_VISIBLE
370/** @todo void clearerr_unlocked(FILE *); */
371/** @todo int feof_unlocked(FILE *); */
372/** @todo int ferror_unlocked(FILE *); */
373/** @todo int fileno_unlocked(FILE *); */
374#endif
375
376#if __POSIX_VISIBLE >= 200112
377int fseeko(FILE *, __off_t, int);
378__off_t ftello(FILE *);
379#endif
380
381#if __BSD_VISIBLE || __XSI_VISIBLE > 0 && __XSI_VISIBLE < 600
382int getw(FILE *);
383int putw(int, FILE *);
384#endif /* BSD or X/Open before issue 6 */
385
386#if __XSI_VISIBLE
387char *tempnam(const char *, const char *);
388#endif
389
390/*
391 * Routines that are purely local.
392 */
393#if __BSD_VISIBLE
394/** @todo int asprintf(char **, const char *, ...) __printflike(2, 3); */
395/** @todo char *ctermid_r(char *); */
396/** @todo char *fgetln(FILE *, size_t *); */
397#if __GNUC__ == 2 && __GNUC_MINOR__ >= 7 || __GNUC__ >= 3
398#define __ATTR_FORMAT_ARG __attribute__((__format_arg__(2)))
399#else
400#define __ATTR_FORMAT_ARG
401#endif
402/** @todo __const char *fmtcheck(const char *, const char *) __ATTR_FORMAT_ARG; */
403/** @todo int fpurge(FILE *); */
404void setbuffer(FILE *, char *, int);
405/** @todo int setlinebuf(FILE *); */
406/** @todo int vasprintf(char **, const char *, __va_list)
407 __printflike(2, 0); */
408
409/*
410 * The system error table contains messages for the first sys_nerr
411 * positive errno values. Use strerror() or strerror_r() from <string.h>
412 * instead.
413 */
414extern __const int sys_nerr;
415extern __const char *__const sys_errlist[];
416
417/*
418 * Stdio function-access interface.
419 */
420/** @todo FILE *funopen(const void *,
421 int (*)(void *, char *, int),
422 int (*)(void *, const char *, int),
423 fpos_t (*)(void *, fpos_t, int),
424 int (*)(void *)); */
425/** @todo #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) */
426/** @todo #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) */
427
428/*
429 * Portability hacks. See <sys/types.h>.
430 */
431#ifndef _FTRUNCATE_DECLARED
432#define _FTRUNCATE_DECLARED
433int ftruncate(int, __off_t);
434#endif
435#ifndef _LSEEK_DECLARED
436#define _LSEEK_DECLARED
437__off_t lseek(int, __off_t, int);
438#endif
439#ifndef _MMAP_DECLARED
440#define _MMAP_DECLARED
441void *mmap(void *, size_t, int, int, int, __off_t);
442#endif
443#ifndef _TRUNCATE_DECLARED
444#define _TRUNCATE_DECLARED
445int truncate(const char *, __off_t);
446#endif
447#endif /* __BSD_VISIBLE */
448
449#if 0 /* bird: Skip FreeBSD sepcific LIBC stuff. */
450/*
451 * Functions internal to the implementation.
452 */
453int __srget(FILE *);
454int __swbuf(int, FILE *);
455
456/*
457 * The __sfoo macros are here so that we can
458 * define function versions in the C library.
459 */
460#define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
461#if defined(__GNUC__) && defined(__STDC__)
462static __inline int __sputc(int _c, FILE *_p) {
463 if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
464 return (*_p->_p++ = _c);
465 else
466 return (__swbuf(_c, _p));
467}
468#else
469/*
470 * This has been tuned to generate reasonable code on the vax using pcc.
471 */
472#define __sputc(c, p) \
473 (--(p)->_w < 0 ? \
474 (p)->_w >= (p)->_lbfsize ? \
475 (*(p)->_p = (c)), *(p)->_p != '\n' ? \
476 (int)*(p)->_p++ : \
477 __swbuf('\n', p) : \
478 __swbuf((int)(c), p) : \
479 (*(p)->_p = (c), (int)*(p)->_p++))
480#endif
481
482#define __sfeof(p) (((p)->_flags & __SEOF) != 0)
483#define __sferror(p) (((p)->_flags & __SERR) != 0)
484#define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
485#define __sfileno(p) ((p)->_file)
486
487#if __BSD_VISIBLE
488/*
489 * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
490 * B.8.2.7 for the rationale behind the *_unlocked() macros.
491 */
492#define feof_unlocked(p) __sfeof(p)
493#define ferror_unlocked(p) __sferror(p)
494#define clearerr_unlocked(p) __sclearerr(p)
495#define fileno_unlocked(p) __sfileno(p)
496#endif
497#if __POSIX_VISIBLE >= 199506
498#define getc_unlocked(fp) __sgetc(fp)
499#define putc_unlocked(x, fp) __sputc(x, fp)
500
501#define getchar_unlocked() getc_unlocked(stdin)
502#define putchar_unlocked(x) putc_unlocked(x, stdout)
503#endif
504
505#endif /* bird: Skip FreeBSD sepcific LIBC stuff. */
506
507
508/* bird: start of EMX isms. */
509
510#if !defined (_IOREAD)
511/** @todo change to double underscore prefix to prevent confusion with
512 * setvbuf() constants. See the short rant about it above. */
513#define _IOREAD 0x01
514#define _IOWRT 0x02
515#define _IORW 0x04
516#define _IOEOF 0x08
517#define _IOERR 0x10
518#endif
519
520int _fill (FILE *);
521int _flush (int, FILE *);
522int _rmtmp (void);
523
524extern __inline__ int feof (FILE *_s)
525{
526 return (_s->_flags & _IOEOF ? 1 : 0);
527}
528
529extern __inline__ int ferror (FILE *_s)
530{
531 return (_s->_flags & _IOERR ? 1 : 0);
532}
533
534extern __inline__ int getchar (void) { return getc (stdin); }
535extern __inline__ int putchar (int _c) { return putc (_c, stdout); }
536
537#if !defined (__STRICT_ANSI__) && !defined (_POSIX_SOURCE) || defined(__USE_EMX)
538
539char *cuserid (char *);
540int fcloseall (void);
541int fgetchar (void);
542int flushall (void);
543int fputchar (int);
544
545#endif
546
547
548#if (!defined (__STRICT_ANSI__) && !defined (_POSIX_SOURCE)) || defined (_WITH_UNDERSCORE) || defined(__USE_EMX)
549
550int _fcloseall (void);
551FILE *_fdopen (int, __const__ char *);
552int _fgetchar (void);
553int _flushall (void);
554int _fputchar (int);
555int _fseek_hdr (FILE *);
556int _fsetmode (FILE *, __const__ char *);
557FILE *_fsopen (__const__ char *, __const__ char *, int);
558int _getw (FILE *);
559char *_mfclose (FILE *);
560FILE *_mfopen (char *, __const__ char *, size_t, int);
561int _pclose (FILE *);
562FILE *_popen (__const__ char *, __const__ char *);
563int _putw (int, FILE *);
564void _setbuffer (FILE *, char *, int);
565int _snprintf (char *, size_t, __const__ char *, ...);
566char *_tempnam (__const__ char *, __const__ char *);
567int _vsnprintf (char *, size_t, __const__ char *, va_list);
568
569#endif
570/* bird: end of EMX isms. */
571
572__END_DECLS
573#endif /* !_STDIO_H_ */
574
Note: See TracBrowser for help on using the repository browser.