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/fgetwc.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/fgetwc.c')
-rw-r--r-- | src/stdio/fgetwc.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/stdio/fgetwc.c b/src/stdio/fgetwc.c new file mode 100644 index 00000000..c990545f --- /dev/null +++ b/src/stdio/fgetwc.c @@ -0,0 +1,51 @@ +#include "stdio_impl.h" + +wint_t __fgetwc_unlocked(FILE *f) +{ + mbstate_t st = { 0 }; + wchar_t wc; + int c; + unsigned char b; + size_t l; + + f->mode |= f->mode+1; + + /* Convert character from buffer if possible */ + if (f->rpos < f->rend) { + l = mbrtowc(&wc, f->rpos, f->rend - f->rpos, &st); + if (l+2 >= 2) { + f->rpos += l + !l; /* l==0 means 1 byte, null */ + return wc; + } + if (l == -1) { + f->rpos++; + return WEOF; + } + } else l = -2; + + /* Convert character byte-by-byte from __uflow */ + while (l == -2) { + b = c = __uflow(f); + if (c < 0) { + if (!mbsinit(&st)) errno = EILSEQ; + return WEOF; + } + l = mbrtowc(&wc, &b, 1, &st); + if (l == -1) return WEOF; + } + + FUNLOCK(f); + return wc; +} + +wint_t fgetwc(FILE *f) +{ + wint_t c; + FLOCK(f); + c = __fgetwc_unlocked(f); + FUNLOCK(f); + return c; +} + +weak_alias(__fgetwc_unlocked, fgetwc_unlocked); +weak_alias(__fgetwc_unlocked, getwc_unlocked); |