rewrite popen to use posix_spawn instead of fragile vfork hacks
[musl] / src / stdio / ungetwc.c
1 #include "stdio_impl.h"
2 #include <wchar.h>
3 #include <limits.h>
4 #include <ctype.h>
5 #include <string.h>
6
7 wint_t ungetwc(wint_t c, FILE *f)
8 {
9         unsigned char mbc[MB_LEN_MAX];
10         int l=1;
11
12         if (c == WEOF) return c;
13
14         /* Try conversion early so we can fail without locking if invalid */
15         if (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)
16                 return WEOF;
17
18         FLOCK(f);
19
20         f->mode |= f->mode+1;
21
22         if ((!f->rend && __toread(f)) || f->rpos < f->buf - UNGET + l) {
23                 FUNLOCK(f);
24                 return EOF;
25         }
26
27         if (isascii(c)) *--f->rpos = c;
28         else memcpy(f->rpos -= l, mbc, l);
29
30         f->flags &= ~F_EOF;
31
32         FUNLOCK(f);
33         return c;
34 }