ungetwc.c 648 B

1234567891011121314151617181920212223242526272829303132333435
  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>
  7. wint_t ungetwc(wint_t c, FILE *f)
  8. {
  9. unsigned char mbc[MB_LEN_MAX];
  10. int l=1;
  11. locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  12. FLOCK(f);
  13. if (f->mode <= 0) fwide(f, 1);
  14. *ploc = f->locale;
  15. if (!f->rpos) __toread(f);
  16. if (!f->rpos || f->rpos < f->buf - UNGET + l || c == WEOF ||
  17. (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)) {
  18. FUNLOCK(f);
  19. *ploc = loc;
  20. return WEOF;
  21. }
  22. if (isascii(c)) *--f->rpos = c;
  23. else memcpy(f->rpos -= l, mbc, l);
  24. f->flags &= ~F_EOF;
  25. FUNLOCK(f);
  26. *ploc = loc;
  27. return c;
  28. }