fgetws.c 638 B

123456789101112131415161718192021222324252627282930313233
  1. #include "stdio_impl.h"
  2. #include <wchar.h>
  3. #include <errno.h>
  4. wint_t __fgetwc_unlocked(FILE *);
  5. wchar_t *fgetws(wchar_t *restrict s, int n, FILE *restrict f)
  6. {
  7. wchar_t *p = s;
  8. if (!n--) return s;
  9. FLOCK(f);
  10. /* Setup a dummy errno so we can detect EILSEQ. This is
  11. * the only way to catch encoding errors in the form of a
  12. * partial character just before EOF. */
  13. errno = EAGAIN;
  14. for (; n; n--) {
  15. wint_t c = __fgetwc_unlocked(f);
  16. if (c == WEOF) break;
  17. *p++ = c;
  18. if (c == '\n') break;
  19. }
  20. *p = 0;
  21. if (ferror(f) || errno==EILSEQ) p = s;
  22. FUNLOCK(f);
  23. return (p == s) ? NULL : s;
  24. }
  25. weak_alias(fgetws, fgetws_unlocked);