correct locking in stdio functions that tried to be lock-free
[musl] / src / stdio / ungetwc.c
index f7cde2e..5282fee 100644 (file)
@@ -8,36 +8,21 @@ wint_t ungetwc(wint_t c, FILE *f)
        if (c == WEOF) return c;
 
        /* Try conversion early so we can fail without locking if invalid */
-       if (!isascii(c) && (l = wctomb(mbc, c)) < 0)
+       if (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)
                return WEOF;
 
        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))) {
+       if ((!f->rend && __toread(f)) || f->rpos < f->buf - UNGET + l) {
                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 unget buffer is nonempty, fail. */
-       if (f->rpos < f->buf) {
-               FUNLOCK(f);
-               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);