summaryrefslogtreecommitdiff
path: root/src/wchar/fgetws.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/wchar/fgetws.c')
-rw-r--r--src/wchar/fgetws.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/wchar/fgetws.c b/src/wchar/fgetws.c
new file mode 100644
index 00000000..5262a79d
--- /dev/null
+++ b/src/wchar/fgetws.c
@@ -0,0 +1,37 @@
+#include <wchar.h>
+#include "stdio.h"
+#include "nonstd/assert.h"
+
+wchar_t * fgetws(wchar_t * restrict s, int n, FILE * restrict stream)
+{
+ ASSERT_NONNULL(s);
+ ASSERT_NONNULL(stream);
+
+ if (fwide(stream, 1) <= 0) {
+ /* not a wide stream */
+ return NULL;
+ }
+
+ int i;
+ for (i = 0; i < n; i++) {
+ s[i] = fgetwc(stream);
+ if (s[i] == WEOF) {
+ if (feof(stream)) {
+ s[i] = L'\0';
+ return s;
+ }
+ /* read or encoding error */
+ return NULL;
+ }
+ if (s[i] == L'\0') {
+ break;
+ }
+ }
+ s[i] = L'\0';
+
+ return s;
+}
+
+/*
+STDC(199409)
+*/