fputwc.c 812 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "stdio_impl.h"
  2. #include "locale_impl.h"
  3. #include <wchar.h>
  4. #include <limits.h>
  5. #include <ctype.h>
  6. wint_t __fputwc_unlocked(wchar_t c, FILE *f)
  7. {
  8. char mbc[MB_LEN_MAX];
  9. int l;
  10. locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  11. if (f->mode <= 0) fwide(f, 1);
  12. *ploc = f->locale;
  13. if (isascii(c)) {
  14. c = putc_unlocked(c, f);
  15. } else if (f->wpos + MB_LEN_MAX < f->wend) {
  16. l = wctomb((void *)f->wpos, c);
  17. if (l < 0) c = WEOF;
  18. else f->wpos += l;
  19. } else {
  20. l = wctomb(mbc, c);
  21. if (l < 0 || __fwritex((void *)mbc, l, f) < l) c = WEOF;
  22. }
  23. if (c==WEOF) f->flags |= F_ERR;
  24. *ploc = loc;
  25. return c;
  26. }
  27. wint_t fputwc(wchar_t c, FILE *f)
  28. {
  29. FLOCK(f);
  30. c = __fputwc_unlocked(c, f);
  31. FUNLOCK(f);
  32. return c;
  33. }
  34. weak_alias(__fputwc_unlocked, fputwc_unlocked);
  35. weak_alias(__fputwc_unlocked, putwc_unlocked);