]> git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/stdio/fgetws.c
b08b30491af14982248461caafd62d3a3aa64909
[wasi-libc.git] / libc-top-half / musl / src / stdio / fgetws.c
1 #include "stdio_impl.h"
2 #include <wchar.h>
3 #include <errno.h>
4
5 wint_t __fgetwc_unlocked(FILE *);
6
7 wchar_t *fgetws(wchar_t *restrict s, int n, FILE *restrict f)
8 {
9 wchar_t *p = s;
10
11 if (!n--) return s;
12
13 FLOCK(f);
14
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;
24 }
25 *p = 0;
26 if (ferror(f) || errno==EILSEQ) p = s;
27
28 FUNLOCK(f);
29
30 return (p == s) ? NULL : s;
31 }
32
33 weak_alias(fgetws, fgetws_unlocked);