diff --git a/libc/Makefile b/libc/Makefile
index 92d06d9d..c58ef40f 100644
--- a/libc/Makefile
+++ b/libc/Makefile
@@ -79,6 +79,7 @@ lldiv.o \
localtime.o \
localtime_r.o \
mbrtowc.o \
+mbsrtowcs.o \
mbtowc.o \
memccpy.o \
memchr.o \
diff --git a/libc/include/wchar.h b/libc/include/wchar.h
index bad8a152..7e784baa 100644
--- a/libc/include/wchar.h
+++ b/libc/include/wchar.h
@@ -61,6 +61,7 @@ __BEGIN_DECLS
struct tm;
+size_t mbsrtowcs(wchar_t* restrict, const char** restrict, size_t, mbstate_t* restrict);
size_t wcrtomb(char* restrict, wchar_t, mbstate_t* restrict);
size_t mbrtowc(wchar_t* restrict, const char* restrict, size_t, mbstate_t* restrict);
wchar_t* wcscat(wchar_t* restrict, const wchar_t* restrict);
@@ -106,7 +107,6 @@ long double wcstold(const wchar_t* restrict, wchar_t** restrict);
long long wcstoll(const wchar_t* restrict, wchar_t** restrict, int);
long wcstol(const wchar_t* restrict, wchar_t** restrict, int);
size_t mbrlen(const char* restrict, size_t, mbstate_t* restrict);
-size_t mbsrtowcs(wchar_t* restrict, const char** restrict, size_t, mbstate_t* restrict);
size_t wcsftime(wchar_t* restrict, size_t, const wchar_t* restrict, const struct tm* restrict);
size_t wcsrtombs(char* restrict, const wchar_t** restrict, size_t, mbstate_t* restrict);
size_t wcsxfrm(wchar_t* restrict, const wchar_t* restrict, size_t);
diff --git a/libc/mbsrtowcs.cpp b/libc/mbsrtowcs.cpp
new file mode 100644
index 00000000..f056018c
--- /dev/null
+++ b/libc/mbsrtowcs.cpp
@@ -0,0 +1,72 @@
+/*******************************************************************************
+
+ Copyright(C) Jonas 'Sortie' Termansen 2013.
+
+ This file is part of the Sortix C Library.
+
+ The Sortix C Library is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or (at your
+ option) any later version.
+
+ The Sortix C Library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with the Sortix C Library. If not, see .
+
+ mbsrtowcs.cpp
+ Convert a multibyte string to a wide-character string.
+
+*******************************************************************************/
+
+#include
+#include
+#include
+#include
+#include
+
+extern "C" size_t mbsrtowcs(wchar_t* dst, const char** src_ptr, size_t dst_len,
+ mbstate_t* ps)
+{
+ assert(src_ptr && *src_ptr);
+ // Avoid changing *src_ptr if dst is NULL.
+ const char* local_src_ptr = *src_ptr;
+ if ( !dst )
+ src_ptr = &local_src_ptr;
+ // For some reason, the standards don't mandate that the secret ps variable
+ // is reset when ps is NULL, unlike mbstowcs that always resets this
+ // variable. We'll avoid resetting the variable here in case any programs
+ // actually take advantage of this fact.
+ static mbstate_t static_ps;
+ if ( !ps )
+ ps = &static_ps;
+ size_t ret = 0;
+ size_t src_len = strlen(*src_ptr);
+ while ( !dst || dst_len )
+ {
+ mbstate_t saved_ps = *ps;
+ size_t consumed = mbrtowc(dst, *src_ptr, src_len, ps);
+ if ( consumed == (size_t) 0 )
+ {
+ *src_ptr = NULL;
+ break;
+ }
+ if ( consumed == (size_t) -1 )
+ return (size_t) -1;
+ if ( consumed == (size_t) -2 )
+ {
+ *ps = saved_ps;
+ break;
+ }
+ *src_ptr += consumed;
+ src_len -= consumed;
+ if ( dst )
+ dst++,
+ dst_len--;
+ ret++;
+ }
+ return ret;
+}