From 4abab5ad6c8465a7528ccdd5f49367da05f78bbd Mon Sep 17 00:00:00 2001 From: Vladimir Azarov Date: Tue, 1 Oct 2024 15:47:05 +0200 Subject: Initial version --- src/stdio/ungetwc.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/stdio/ungetwc.c (limited to 'src/stdio/ungetwc.c') diff --git a/src/stdio/ungetwc.c b/src/stdio/ungetwc.c new file mode 100644 index 0000000..9edf366 --- /dev/null +++ b/src/stdio/ungetwc.c @@ -0,0 +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; + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc; + + FLOCK(f); + + if (f->mode <= 0) fwide(f, 1); + *ploc = f->locale; + + 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; + } + + if (isascii(c)) *--f->rpos = c; + else memcpy(f->rpos -= l, mbc, l); + + f->flags &= ~F_EOF; + + FUNLOCK(f); + *ploc = loc; + return c; +} -- cgit v1.2.3