X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fstdio%2Fungetwc.c;h=9edf366f90d5ce07d6c7ed3ff448981c66ddefd9;hb=54316a52b2119edf73e274c8b4f25d7757f7b4d3;hp=8cc85a6bfd2a00b615ecccdc96fbfc519e4b77f5;hpb=c6d441e3a246370d9c459396ec22b096db93850e;p=musl diff --git a/src/stdio/ungetwc.c b/src/stdio/ungetwc.c index 8cc85a6b..9edf366f 100644 --- a/src/stdio/ungetwc.c +++ b/src/stdio/ungetwc.c @@ -1,4 +1,5 @@ #include "stdio_impl.h" +#include "locale_impl.h" #include #include #include @@ -7,21 +8,20 @@ wint_t ungetwc(wint_t c, FILE *f) { unsigned char mbc[MB_LEN_MAX]; - int l=1; - - if (c == WEOF) return c; - - /* Try conversion early so we can fail without locking if invalid */ - if (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0) - return WEOF; + int l; + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc; FLOCK(f); - f->mode |= f->mode+1; + if (f->mode <= 0) fwide(f, 1); + *ploc = f->locale; - if ((!f->rend && __toread(f)) || f->rpos < f->buf - UNGET + l) { + if (!f->rpos) __toread(f); + if (!f->rpos || c == WEOF || (l = wcrtomb((void *)mbc, c, 0)) < 0 || + f->rpos < f->buf - UNGET + l) { FUNLOCK(f); - return EOF; + *ploc = loc; + return WEOF; } if (isascii(c)) *--f->rpos = c; @@ -30,5 +30,6 @@ wint_t ungetwc(wint_t c, FILE *f) f->flags &= ~F_EOF; FUNLOCK(f); + *ploc = loc; return c; }