simplify by using krnCreateROMHeader()
[AROS.git] / compiler / posixc / vfscanf.c
blob0e4163113f98b46669656b08a5c71ffd2b5c2626
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function vfscanf()
6 */
8 /* Original source from libnix */
10 #include <proto/dos.h>
11 #include <errno.h>
12 #include <stdarg.h>
13 #include "__fdesc.h"
14 #include "__stdio.h"
17 ** If the VFSCANF_DIRECT_DOS define is set to 1, dos.library functions FGetC()
18 ** and UnGetC() are used directly, for possibly better speed. Otherwise
19 ** the clib functions fgetc/ungetc are used.
22 #define VFSCANF_DIRECT_DOS 1
24 #if VFSCANF_DIRECT_DOS
26 struct __vfscanf_handle
28 FILE *stream;
29 fdesc *fdesc;
32 static int __getc(void *_h);
33 static int __ungetc(int c, void *_h);
35 #endif
37 /*****************************************************************************
39 NAME */
40 #include <stdio.h>
42 int vfscanf (
44 /* SYNOPSIS */
45 FILE * stream,
46 const char * format,
47 va_list args)
49 /* FUNCTION
50 Read the scream, scan it as the format specified and write the
51 result of the conversion into the specified arguments.
53 INPUTS
54 stream - A stream to read from
55 format - A scanf() format string.
56 args - A list of arguments for the results.
58 RESULT
59 The number of converted arguments.
61 NOTES
63 EXAMPLE
65 BUGS
67 SEE ALSO
69 INTERNALS
71 ******************************************************************************/
73 #if VFSCANF_DIRECT_DOS
74 struct __vfscanf_handle h;
76 h.stream = stream;
77 h.fdesc = __getfdesc(stream->fd);
79 if (!h.fdesc)
81 errno = EBADF;
82 return 0;
86 Flush (h.fdesc->fcb->handle);
88 return __vcscan (&h, __getc, __ungetc, format, args);
89 #else
90 fdesc *fdesc;
92 fdesc = __getfdesc(stream->fd);
94 if (!fdesc)
96 errno = EBADF;
97 return 0;
100 Flush (fdesc->fcb->handle);
102 return __vcscan (stream, fgetc, ungetc, format, args);
104 #endif
105 } /* vfscanf */
107 #if VFSCANF_DIRECT_DOS
109 static int __ungetc(int c, void *_h)
111 struct __vfscanf_handle *h = _h;
112 /* Note: changes here might require changes in ungetc.c!! */
114 if (c < -1)
115 c = (unsigned int)c;
117 if (!UnGetC(h->fdesc->fcb->handle, c))
119 errno = __stdc_ioerr2errno(IoErr());
121 if (errno)
123 h->stream->flags |= __POSIXC_STDIO_ERROR;
125 else
127 h->stream->flags |= __POSIXC_STDIO_EOF;
130 c = EOF;
133 return c;
136 static int __getc(void *_h)
138 struct __vfscanf_handle *h = _h;
139 fdesc *fdesc = h->fdesc;
140 int c;
142 /* Note: changes here might require changes in fgetc.c!! */
144 FLUSHONREADCHECK
146 c = FGetC(h->fdesc->fcb->handle);
148 if (c == EOF)
150 c = IoErr();
152 if (c)
154 errno = __stdc_ioerr2errno(c);
155 h->stream->flags |= __POSIXC_STDIO_ERROR;
157 else
159 h->stream->flags |= __POSIXC_STDIO_EOF;
162 c = EOF;
165 return c;
168 #endif