X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fstdio%2Fungetwc.c;h=9edf366f90d5ce07d6c7ed3ff448981c66ddefd9;hb=59b64ff686cef2a87e9552658b2c8d2531f87176;hp=f7cde2e0c777737e7df8746306add7e03b7fe188;hpb=0b44a0315b47dd8eced9f3b7f31580cf14bbfc01;p=musl diff --git a/src/stdio/ungetwc.c b/src/stdio/ungetwc.c index f7cde2e0..9edf366f 100644 --- a/src/stdio/ungetwc.c +++ b/src/stdio/ungetwc.c @@ -1,45 +1,35 @@ #include "stdio_impl.h" +#include "locale_impl.h" +#include +#include +#include +#include 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(mbc, c)) < 0) - return WEOF; + int l; + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc; FLOCK(f); - f->mode |= f->mode+1; - - /* Fail if unreadable or writing and unable to flush */ - if ((f->flags & (F_ERR|F_NORD)) || (f->wpos && __oflow(f))) { - FUNLOCK(f); - return EOF; - } - - /* Clear write mode */ - f->wpos = f->wstop = f->wend = 0; - - /* Put the file in read mode */ - if (!f->rpos) f->rpos = f->rend = f->buf; + if (f->mode <= 0) fwide(f, 1); + *ploc = f->locale; - /* If unget buffer is nonempty, fail. */ - if (f->rpos < f->buf) { + 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); + *ploc = loc; return WEOF; } - /* Put character back into the buffer */ if (isascii(c)) *--f->rpos = c; else memcpy(f->rpos -= l, mbc, l); - /* Clear EOF */ f->flags &= ~F_EOF; FUNLOCK(f); + *ploc = loc; return c; }