bits/syscall.h: add landlock syscalls from linux v5.13
[musl.git] / src / stdio / ungetwc.c
blob9edf366f90d5ce07d6c7ed3ff448981c66ddefd9
1 #include "stdio_impl.h"
2 #include "locale_impl.h"
3 #include <wchar.h>
4 #include <limits.h>
5 #include <ctype.h>
6 #include <string.h>
8 wint_t ungetwc(wint_t c, FILE *f)
10 unsigned char mbc[MB_LEN_MAX];
11 int l;
12 locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
14 FLOCK(f);
16 if (f->mode <= 0) fwide(f, 1);
17 *ploc = f->locale;
19 if (!f->rpos) __toread(f);
20 if (!f->rpos || c == WEOF || (l = wcrtomb((void *)mbc, c, 0)) < 0 ||
21 f->rpos < f->buf - UNGET + l) {
22 FUNLOCK(f);
23 *ploc = loc;
24 return WEOF;
27 if (isascii(c)) *--f->rpos = c;
28 else memcpy(f->rpos -= l, mbc, l);
30 f->flags &= ~F_EOF;
32 FUNLOCK(f);
33 *ploc = loc;
34 return c;