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

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

Extended fmutex interface.

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