X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fstdio%2Ffgetwc.c;h=aa10b818566a8f1d19af680a9d00458d101572fa;hb=7590203c486d9002522019045d34ee3dee0a66f5;hp=e455cfec408bc101c7c15077e68d45f4491e3395;hpb=16f18d036d9a7bf590ee6eb86785c0a9658220b6;p=musl diff --git a/src/stdio/fgetwc.c b/src/stdio/fgetwc.c index e455cfec..aa10b818 100644 --- a/src/stdio/fgetwc.c +++ b/src/stdio/fgetwc.c @@ -5,35 +5,42 @@ static wint_t __fgetwc_unlocked_internal(FILE *f) { - mbstate_t st = { 0 }; wchar_t wc; int c; - unsigned char b; size_t l; /* Convert character from buffer if possible */ - if (f->rpos < f->rend) { - l = mbrtowc(&wc, (void *)f->rpos, f->rend - f->rpos, &st); - if (l+2 >= 2) { + if (f->rpos != f->rend) { + l = mbtowc(&wc, (void *)f->rpos, f->rend - f->rpos); + if (l+1 >= 1) { 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 */ - while (l == -2) { + mbstate_t st = { 0 }; + unsigned char b; + int first = 1; + do { b = c = getc_unlocked(f); if (c < 0) { - if (!mbsinit(&st)) errno = EILSEQ; + if (!first) { + f->flags |= F_ERR; + errno = EILSEQ; + } return WEOF; } l = mbrtowc(&wc, (void *)&b, 1, &st); - if (l == -1) return WEOF; - } + if (l == -1) { + if (!first) { + f->flags |= F_ERR; + ungetc(b, f); + } + return WEOF; + } + first = 0; + } while (l == -2); return wc; }