fix treatment by fgetws of encoding errors as eof
[musl.git] / src / stdio / fgetws.c
blobb08b30491af14982248461caafd62d3a3aa64909
1 #include "stdio_impl.h"
2 #include <wchar.h>
3 #include <errno.h>
5 wint_t __fgetwc_unlocked(FILE *);
7 wchar_t *fgetws(wchar_t *restrict s, int n, FILE *restrict f)
9 wchar_t *p = s;
11 if (!n--) return s;
13 FLOCK(f);
15 /* Setup a dummy errno so we can detect EILSEQ. This is
16 * the only way to catch encoding errors in the form of a
17 * partial character just before EOF. */
18 errno = EAGAIN;
19 for (; n; n--) {
20 wint_t c = __fgetwc_unlocked(f);
21 if (c == WEOF) break;
22 *p++ = c;
23 if (c == '\n') break;
25 *p = 0;
26 if (ferror(f) || errno==EILSEQ) p = s;
28 FUNLOCK(f);
30 return (p == s) ? NULL : s;
33 weak_alias(fgetws, fgetws_unlocked);