diff options
author | Rich Felker <dalias@aerifal.cx> | 2011-02-12 00:22:29 -0500 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2011-02-12 00:22:29 -0500 |
commit | 0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 (patch) | |
tree | 6eaef0d8a720fa3da580de87b647fff796fe80b3 /src/stdio/vfscanf.c | |
download | musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.gz musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.bz2 musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.xz musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.zip |
initial check-in, version 0.5.0v0.5.0
Diffstat (limited to 'src/stdio/vfscanf.c')
-rw-r--r-- | src/stdio/vfscanf.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/stdio/vfscanf.c b/src/stdio/vfscanf.c new file mode 100644 index 00000000..69f45081 --- /dev/null +++ b/src/stdio/vfscanf.c @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <ctype.h> + +#include "stdio_impl.h" +#include "__scanf.h" + +static void f_read(rctx_t *r) +{ + FILE *f = r->opaque; + if ((r->c = __uflow(f)) >= 0) r->l++; +} + +int vfscanf(FILE *f, const char *fmt, va_list ap) +{ + size_t l = strlen(fmt), i, result; + rctx_t r = { f_read, (void *)f, 0, isspace }; + wchar_t fmt2[l+1]; + + if (l > 0x100000) { + errno = ENOMEM; + return -1; + } + for (i=0; i<=l; i++) fmt2[i] = (unsigned char)fmt[i]; + + FLOCK(f); + + result = __scanf(&r, fmt2, ap); + + if (r.u && r.c >= 0) { + /* This code takes care of the case where the caller performs + * a nonmatching scanf to leave a character in the unscan + * buffer, followed by an unget, followed by a scanf that + * matches zero characters. In this case the final 'unread' + * character must be returned to the unget buffer rather than + * the unscan buffer. */ + f->rpos--; + } + + FUNLOCK(f); + return result; +} |