crt: Add support for C95 wcstok() function for non-UCRT import libraries

All non-UCRT crt library versions provide wcstok symbol which is not C95+
compatible. Its function prototype is missing the third argument. In UCRT
version this function without third argument is named _wcstok(). So rename
wcstok symbol to _wcstok in all non-UCRT import libraries for compatibility
with UCRT.

msvcr80+ provides wcstok_s symbol which is C95+ compatible wcstok()
function. msvcr80+ wcstok_s() function has same function prototype as C95+
wcstok() and the only difference between MS wcstok_s and C95 wcstok is that
MS wcstok_s validates parameters and sets errno. C95 version has undefined
behavior when called with invalid parameters (e.g. Linux version crashes).

So add appropriate aliases of wcstok_s in msvcr80+ import libraries to have
C95+ compatible wcstok() function under the correct C95 name wcstok.

For pre-msvcr80 import libraries, provide mingw-w64 implementation of
wcstok_s() and C95+ wcstok() functions. So when linking with any crt
library, the wcstok symbol resolve to the correct C95 compatible wcstok()
function. mingw-w64 implementation of wcstok is copied from musl libc which
is very compact and some parts from musl libc are already used in mingw-w64.

Update header files to reflect these changes. With this change, C95+
compatible wcstok() function is available for all builds, not only UCRT as
it was before this change.

For compatibility with C99, add missing restrict keyword for last wcstok()
argument in header files.

mingw-w64 is currently missing MS _wcstok() function for UCRT builds.
So provide it into UCRT import libraries.

Visual Studio 2015+ redefines wcstok() to _wcstok() when macro
_CRT_NON_CONFORMING_WCSTOK is defined. And in C++ mode it provides
overloaded C++ function wcstok() with two arguments unless the macro
_CRT_NO_INLINE_DEPRECATED_WCSTOK is defined. It is documented on:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strtok-strtok-l-wcstok-wcstok-l-mbstok-mbstok-l

Implement same logic for _CRT_NON_CONFORMING_WCSTOK to get the old behavior
by default. And provides C++ inline two-argument function unless define
_CRT_NO_INLINE_DEPRECATED_WCSTOK is set.

With this change mingw-w64 always provides C95+ compatible wcstok()
function and MS two-arg _wcstok() function in all CRT import libraries.
Also every CRT import library provides MS wcstok_s() function.

Signed-off-by: Martin Storsjö <martin@martin.st>
diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index cd136ab..1864e35 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -391,7 +391,8 @@
   stdio/ucrt_vscanf.c \
   stdio/ucrt_vsnprintf.c \
   stdio/ucrt_vsprintf.c \
-  stdio/ucrt_vsscanf.c
+  stdio/ucrt_vsscanf.c \
+  string/ucrt__wcstok.c
 
 # Files included in libucrtapp.a
 src_ucrtapp=\
@@ -482,7 +483,8 @@
   misc/wctob.c \
   stdio/_fstat64i32.c \
   stdio/_scprintf.c \
-  stdio/_vscprintf.c
+  stdio/_vscprintf.c \
+  string/wcstok.c
 
 # Files included in libmsvcrt-os.a (for msvcrt.dll) on x86_64
 src_msvcrt64=\
@@ -527,7 +529,8 @@
   misc/wassert.c \
   misc/wcrtomb.c \
   misc/wcsnlen.c \
-  misc/wctob.c
+  misc/wctob.c \
+  string/wcstok.c
 
 # Files included in libmsvcrt-os.a (for msvcrt.dll) on arm32
 src_msvcrtarm32=\
@@ -746,7 +749,8 @@
   misc/wctob.c \
   stdio/_fstat64i32.c \
   stdio/fseeki64.c \
-  stdio/mingw_lock.c
+  stdio/mingw_lock.c \
+  string/wcstok.c
 
 src_pre_msvcr100=\
   misc/imaxdiv.c
diff --git a/mingw-w64-crt/def-include/crt-aliases.def.in b/mingw-w64-crt/def-include/crt-aliases.def.in
index 0cc2833..3fb824c 100644
--- a/mingw-w64-crt/def-include/crt-aliases.def.in
+++ b/mingw-w64-crt/def-include/crt-aliases.def.in
@@ -230,6 +230,11 @@
 ADD_UNDERSCORE(y1)
 ADD_UNDERSCORE(yn)
 
+; This is list of symbol aliases for C95 functions
+#ifdef USE_WCSTOK_S_FOR_WCSTOK
+wcstok == wcstok_s
+#endif
+
 ; This is list of symbol aliases for C99 functions
 ; ADD_UNDERSCORE(logb)
 #ifdef WITH_NEXTAFTER_ALIAS
diff --git a/mingw-w64-crt/lib-common/msvcr120_app.def.in b/mingw-w64-crt/lib-common/msvcr120_app.def.in
index 4508a43..4f17fb6 100644
--- a/mingw-w64-crt/lib-common/msvcr120_app.def.in
+++ b/mingw-w64-crt/lib-common/msvcr120_app.def.in
@@ -2310,7 +2310,7 @@
 wcstod
 wcstof
 wcstoimax
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 F_ARM32(wcstold) ; Can't use long double functions from the CRT on x86
@@ -2334,4 +2334,5 @@
 wscanf_s
 
 #define CRTAPP
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib-common/msvcrt.def.in b/mingw-w64-crt/lib-common/msvcrt.def.in
index 6359a79..c30f27b 100644
--- a/mingw-w64-crt/lib-common/msvcrt.def.in
+++ b/mingw-w64-crt/lib-common/msvcrt.def.in
@@ -1117,7 +1117,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by mingw-w64 or by wcstok_s alias
 wcstol
 wcstombs
 wcstoul
@@ -1845,7 +1845,7 @@
 F_ARM_ANY(wcsnlen) ; i386 and x64 wcsnlen replaced by emu
 F_ARM_ANY(wcsrtombs) ; i386 and x64 wcsrtombs replaced by emu
 wcsrtombs_s
-wcstok_s
+F_ARM_ANY(wcstok_s) ; i386 and x64 wcstok_s replaced by emu
 wcstombs_s
 F_ARM_ANY(wctob) ; i386 and x64 wctob replaced by emu
 wctomb_s
@@ -1923,6 +1923,9 @@
 ; i386 strtoll, strtoull, strtoimax, strtoumax, wcstoll, wcstoull, wcstoimax and wcstoumax aliases provided by emu
 #define WITH_STRTO64_ALIAS
 #endif
+#if defined(DEF_ARM32) || defined(DEF_ARM64)
+#define USE_WCSTOK_S_FOR_WCSTOK
+#endif
 #include "crt-aliases.def.in"
 
 ; This is list of additional symbol aliases not available in any CRT library
diff --git a/mingw-w64-crt/lib32/crtdll.def.in b/mingw-w64-crt/lib32/crtdll.def.in
index 82cf0bc..fd7efbc 100644
--- a/mingw-w64-crt/lib32/crtdll.def.in
+++ b/mingw-w64-crt/lib32/crtdll.def.in
@@ -607,7 +607,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by mingw-w64
 wcstol
 wcstombs
 wcstoul
diff --git a/mingw-w64-crt/lib32/msvcr100.def.in b/mingw-w64-crt/lib32/msvcr100.def.in
index 03929c7..b1d73df 100644
--- a/mingw-w64-crt/lib32/msvcr100.def.in
+++ b/mingw-w64-crt/lib32/msvcr100.def.in
@@ -1885,7 +1885,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -1909,4 +1909,5 @@
 #define WITH_IMAXDIV_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib32/msvcr100d.def.in b/mingw-w64-crt/lib32/msvcr100d.def.in
index 30a7307..0ec2317 100644
--- a/mingw-w64-crt/lib32/msvcr100d.def.in
+++ b/mingw-w64-crt/lib32/msvcr100d.def.in
@@ -1948,7 +1948,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -1972,4 +1972,5 @@
 #define WITH_IMAXDIV_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib32/msvcr110.def.in b/mingw-w64-crt/lib32/msvcr110.def.in
index 089a865..dee3662 100644
--- a/mingw-w64-crt/lib32/msvcr110.def.in
+++ b/mingw-w64-crt/lib32/msvcr110.def.in
@@ -2014,7 +2014,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -2057,4 +2057,5 @@
 #define WITH_IMAXDIV_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib32/msvcr110d.def.in b/mingw-w64-crt/lib32/msvcr110d.def.in
index c68ee7b..7719fbd 100644
--- a/mingw-w64-crt/lib32/msvcr110d.def.in
+++ b/mingw-w64-crt/lib32/msvcr110d.def.in
@@ -2081,7 +2081,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -2124,4 +2124,5 @@
 #define WITH_IMAXDIV_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib32/msvcr120.def.in b/mingw-w64-crt/lib32/msvcr120.def.in
index 25a271b..31b4dad 100644
--- a/mingw-w64-crt/lib32/msvcr120.def.in
+++ b/mingw-w64-crt/lib32/msvcr120.def.in
@@ -2275,7 +2275,7 @@
 wcstod
 wcstof
 wcstoimax
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 ; wcstold ; Can't use long double functions from the CRT on x86
@@ -2298,4 +2298,5 @@
 wscanf
 wscanf_s
 
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib32/msvcr120d.def.in b/mingw-w64-crt/lib32/msvcr120d.def.in
index e1bb5f4..8eba08a 100644
--- a/mingw-w64-crt/lib32/msvcr120d.def.in
+++ b/mingw-w64-crt/lib32/msvcr120d.def.in
@@ -2342,7 +2342,7 @@
 wcstod
 wcstof
 wcstoimax
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 ; wcstold ; Can't use long double functions from the CRT on x86
@@ -2365,4 +2365,5 @@
 wscanf
 wscanf_s
 
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib32/msvcr40d.def.in b/mingw-w64-crt/lib32/msvcr40d.def.in
index 544816d..b2408fb 100644
--- a/mingw-w64-crt/lib32/msvcr40d.def.in
+++ b/mingw-w64-crt/lib32/msvcr40d.def.in
@@ -1641,7 +1641,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by mingw-w64
 wcstol
 wcstombs
 wcstoul
diff --git a/mingw-w64-crt/lib32/msvcr70.def.in b/mingw-w64-crt/lib32/msvcr70.def.in
index 689f8f7..12932e8 100644
--- a/mingw-w64-crt/lib32/msvcr70.def.in
+++ b/mingw-w64-crt/lib32/msvcr70.def.in
@@ -891,7 +891,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by mingw-w64
 wcstol
 wcstombs
 wcstoul
diff --git a/mingw-w64-crt/lib32/msvcr70d.def.in b/mingw-w64-crt/lib32/msvcr70d.def.in
index 910b883..a367799 100644
--- a/mingw-w64-crt/lib32/msvcr70d.def.in
+++ b/mingw-w64-crt/lib32/msvcr70d.def.in
@@ -935,7 +935,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by mingw-w64
 wcstol
 wcstombs
 wcstoul
diff --git a/mingw-w64-crt/lib32/msvcr71.def.in b/mingw-w64-crt/lib32/msvcr71.def.in
index 7904cf8..50fe1a2 100644
--- a/mingw-w64-crt/lib32/msvcr71.def.in
+++ b/mingw-w64-crt/lib32/msvcr71.def.in
@@ -885,7 +885,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by mingw-w64
 wcstol
 wcstombs
 wcstoul
diff --git a/mingw-w64-crt/lib32/msvcr71d.def.in b/mingw-w64-crt/lib32/msvcr71d.def.in
index d6bf80e..da5a56e 100644
--- a/mingw-w64-crt/lib32/msvcr71d.def.in
+++ b/mingw-w64-crt/lib32/msvcr71d.def.in
@@ -929,7 +929,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by mingw-w64
 wcstol
 wcstombs
 wcstoul
diff --git a/mingw-w64-crt/lib32/msvcr80.def.in b/mingw-w64-crt/lib32/msvcr80.def.in
index ec463fb..9b1b5d1 100644
--- a/mingw-w64-crt/lib32/msvcr80.def.in
+++ b/mingw-w64-crt/lib32/msvcr80.def.in
@@ -1519,7 +1519,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -1553,4 +1553,5 @@
 #define WITH_LLABS_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib32/msvcr80d.def.in b/mingw-w64-crt/lib32/msvcr80d.def.in
index 2711952..1931dc3 100644
--- a/mingw-w64-crt/lib32/msvcr80d.def.in
+++ b/mingw-w64-crt/lib32/msvcr80d.def.in
@@ -1602,7 +1602,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -1631,4 +1631,5 @@
 #define WITH_LLABS_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib32/msvcr90.def.in b/mingw-w64-crt/lib32/msvcr90.def.in
index c39b475..697b928 100644
--- a/mingw-w64-crt/lib32/msvcr90.def.in
+++ b/mingw-w64-crt/lib32/msvcr90.def.in
@@ -1517,7 +1517,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -1538,4 +1538,5 @@
 #define WITH_LLABS_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib32/msvcr90d.def.in b/mingw-w64-crt/lib32/msvcr90d.def.in
index 90e7cbd..f1da7b6 100644
--- a/mingw-w64-crt/lib32/msvcr90d.def.in
+++ b/mingw-w64-crt/lib32/msvcr90d.def.in
@@ -1589,7 +1589,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -1610,4 +1610,5 @@
 #define WITH_LLABS_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib32/msvcrt10.def.in b/mingw-w64-crt/lib32/msvcrt10.def.in
index 0f99e6a..b062592 100644
--- a/mingw-w64-crt/lib32/msvcrt10.def.in
+++ b/mingw-w64-crt/lib32/msvcrt10.def.in
@@ -1285,7 +1285,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by mingw-w64
 wcstol
 wcstombs
 wcstoul
diff --git a/mingw-w64-crt/lib32/msvcrt20.def.in b/mingw-w64-crt/lib32/msvcrt20.def.in
index 781f86a..4e3ff13 100644
--- a/mingw-w64-crt/lib32/msvcrt20.def.in
+++ b/mingw-w64-crt/lib32/msvcrt20.def.in
@@ -1509,7 +1509,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by mingw-w64
 wcstol
 wcstombs
 wcstoul
diff --git a/mingw-w64-crt/lib32/msvcrt40.def.in b/mingw-w64-crt/lib32/msvcrt40.def.in
index 3122f0e..1941bd9 100644
--- a/mingw-w64-crt/lib32/msvcrt40.def.in
+++ b/mingw-w64-crt/lib32/msvcrt40.def.in
@@ -1609,7 +1609,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by mingw-w64
 wcstol
 wcstombs
 wcstoul
diff --git a/mingw-w64-crt/lib32/msvcrtd.def.in b/mingw-w64-crt/lib32/msvcrtd.def.in
index 04426ad..f725eea 100644
--- a/mingw-w64-crt/lib32/msvcrtd.def.in
+++ b/mingw-w64-crt/lib32/msvcrtd.def.in
@@ -835,7 +835,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by mingw-w64
 wcstol
 wcstombs
 wcstoul
diff --git a/mingw-w64-crt/lib64/msvcr100.def.in b/mingw-w64-crt/lib64/msvcr100.def.in
index f1f680f..9b6bc48 100644
--- a/mingw-w64-crt/lib64/msvcr100.def.in
+++ b/mingw-w64-crt/lib64/msvcr100.def.in
@@ -1847,7 +1847,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -1871,4 +1871,5 @@
 #define WITH_IMAXDIV_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib64/msvcr100d.def.in b/mingw-w64-crt/lib64/msvcr100d.def.in
index d4d4040..70793d2 100644
--- a/mingw-w64-crt/lib64/msvcr100d.def.in
+++ b/mingw-w64-crt/lib64/msvcr100d.def.in
@@ -1912,7 +1912,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -1936,4 +1936,5 @@
 #define WITH_IMAXDIV_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib64/msvcr110.def.in b/mingw-w64-crt/lib64/msvcr110.def.in
index e7c3570..7779436 100644
--- a/mingw-w64-crt/lib64/msvcr110.def.in
+++ b/mingw-w64-crt/lib64/msvcr110.def.in
@@ -1971,7 +1971,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -2014,4 +2014,5 @@
 #define WITH_IMAXDIV_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib64/msvcr110d.def.in b/mingw-w64-crt/lib64/msvcr110d.def.in
index e401c9d..b86002e 100644
--- a/mingw-w64-crt/lib64/msvcr110d.def.in
+++ b/mingw-w64-crt/lib64/msvcr110d.def.in
@@ -2036,7 +2036,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -2079,4 +2079,5 @@
 #define WITH_IMAXDIV_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib64/msvcr120.def.in b/mingw-w64-crt/lib64/msvcr120.def.in
index 0762aab..f6e681b 100644
--- a/mingw-w64-crt/lib64/msvcr120.def.in
+++ b/mingw-w64-crt/lib64/msvcr120.def.in
@@ -2231,7 +2231,7 @@
 wcstod
 wcstof
 wcstoimax
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 ; wcstold ; Can't use long double functions from the CRT on x86
@@ -2254,4 +2254,5 @@
 wscanf
 wscanf_s
 
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib64/msvcr120d.def.in b/mingw-w64-crt/lib64/msvcr120d.def.in
index 3af9c66..6f6df48 100644
--- a/mingw-w64-crt/lib64/msvcr120d.def.in
+++ b/mingw-w64-crt/lib64/msvcr120d.def.in
@@ -2296,7 +2296,7 @@
 wcstod
 wcstof
 wcstoimax
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 ; wcstold ; Can't use long double functions from the CRT on x86
@@ -2319,4 +2319,5 @@
 wscanf
 wscanf_s
 
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib64/msvcr80.def.in b/mingw-w64-crt/lib64/msvcr80.def.in
index 1e66668..08d9723 100644
--- a/mingw-w64-crt/lib64/msvcr80.def.in
+++ b/mingw-w64-crt/lib64/msvcr80.def.in
@@ -1467,7 +1467,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -1501,4 +1501,5 @@
 #define WITH_LLABS_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib64/msvcr80d.def.in b/mingw-w64-crt/lib64/msvcr80d.def.in
index 3228feb..caeb5ce 100644
--- a/mingw-w64-crt/lib64/msvcr80d.def.in
+++ b/mingw-w64-crt/lib64/msvcr80d.def.in
@@ -1544,7 +1544,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -1573,4 +1573,5 @@
 #define WITH_LLABS_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib64/msvcr90.def.in b/mingw-w64-crt/lib64/msvcr90.def.in
index 73de424..ddf7bfb 100644
--- a/mingw-w64-crt/lib64/msvcr90.def.in
+++ b/mingw-w64-crt/lib64/msvcr90.def.in
@@ -1465,7 +1465,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -1486,4 +1486,5 @@
 #define WITH_LLABS_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib64/msvcr90d.def.in b/mingw-w64-crt/lib64/msvcr90d.def.in
index 87f8d48..c26790c 100644
--- a/mingw-w64-crt/lib64/msvcr90d.def.in
+++ b/mingw-w64-crt/lib64/msvcr90d.def.in
@@ -1531,7 +1531,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -1552,4 +1552,5 @@
 #define WITH_LLABS_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/libarm32/msvcr110.def.in b/mingw-w64-crt/libarm32/msvcr110.def.in
index d0af03f..f736370 100644
--- a/mingw-w64-crt/libarm32/msvcr110.def.in
+++ b/mingw-w64-crt/libarm32/msvcr110.def.in
@@ -1958,7 +1958,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -2001,4 +2001,5 @@
 #define WITH_IMAXDIV_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/libarm32/msvcr110d.def.in b/mingw-w64-crt/libarm32/msvcr110d.def.in
index 2dd7f38..66a344c 100644
--- a/mingw-w64-crt/libarm32/msvcr110d.def.in
+++ b/mingw-w64-crt/libarm32/msvcr110d.def.in
@@ -2023,7 +2023,7 @@
 wcsspn
 wcsstr
 wcstod
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstombs
@@ -2066,4 +2066,5 @@
 #define WITH_IMAXDIV_ALIAS
 #define WITH_STRTO64_ALIAS
 #define WITH_STRTO64_L_ALIAS
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/libarm32/msvcr120.def.in b/mingw-w64-crt/libarm32/msvcr120.def.in
index c32ad49..e43e29d 100644
--- a/mingw-w64-crt/libarm32/msvcr120.def.in
+++ b/mingw-w64-crt/libarm32/msvcr120.def.in
@@ -2197,7 +2197,7 @@
 wcstod
 wcstof
 wcstoimax
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstold
@@ -2220,4 +2220,5 @@
 wscanf
 wscanf_s
 
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/libarm32/msvcr120d.def.in b/mingw-w64-crt/libarm32/msvcr120d.def.in
index b30ad0e..7241a9e 100644
--- a/mingw-w64-crt/libarm32/msvcr120d.def.in
+++ b/mingw-w64-crt/libarm32/msvcr120d.def.in
@@ -2262,7 +2262,7 @@
 wcstod
 wcstof
 wcstoimax
-wcstok
+_wcstok == wcstok ; provide nonstandard function "wcstok" in the DLL, under the name "_wcstok" to match UCRT, the C95+ compatible "wcstok" is provided by wcstok_s alias
 wcstok_s
 wcstol
 wcstold
@@ -2285,4 +2285,5 @@
 wscanf
 wscanf_s
 
+#define USE_WCSTOK_S_FOR_WCSTOK
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/string/ucrt__wcstok.c b/mingw-w64-crt/string/ucrt__wcstok.c
new file mode 100644
index 0000000..67c4936
--- /dev/null
+++ b/mingw-w64-crt/string/ucrt__wcstok.c
@@ -0,0 +1,14 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#include <wchar.h>
+
+wchar_t *__cdecl _wcstok(wchar_t *restrict str, const wchar_t *restrict delim)
+{
+  /* NULL as a third param can be specified only for UCRT version of wcstok() */
+  return wcstok(str, delim, NULL);
+}
+wchar_t *(__cdecl *__MINGW_IMP_SYMBOL(_wcstok))(wchar_t *restrict, const wchar_t *restrict) = _wcstok;
diff --git a/mingw-w64-crt/string/wcstok.c b/mingw-w64-crt/string/wcstok.c
new file mode 100644
index 0000000..598d8ed
--- /dev/null
+++ b/mingw-w64-crt/string/wcstok.c
@@ -0,0 +1,41 @@
+/*
+    Copyright © 2005-2020 Rich Felker, et al.
+
+    Permission is hereby granted, free of charge, to any person obtaining
+    a copy of this software and associated documentation files (the
+    "Software"), to deal in the Software without restriction, including
+    without limitation the rights to use, copy, modify, merge, publish,
+    distribute, sublicense, and/or sell copies of the Software, and to
+    permit persons to whom the Software is furnished to do so, subject to
+    the following conditions:
+
+    The above copyright notice and this permission notice shall be
+    included in all copies or substantial portions of the Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include <errno.h>
+#include <wchar.h>
+
+wchar_t *__cdecl wcstok_s(wchar_t *restrict s, const wchar_t *restrict sep, wchar_t **restrict p)
+{
+  if (!p || !sep) { errno = EINVAL; return NULL; } /* added for wcstok_s */
+  if (!s && !(s = *p)) { errno = EINVAL; return NULL; }
+  s += wcsspn(s, sep);
+  if (!*s) return *p = NULL;
+  *p = s + wcscspn(s, sep);
+  if (**p) *(*p)++ = 0;
+  else *p = 0;
+  return s;
+}
+wchar_t *(__cdecl *__MINGW_IMP_SYMBOL(wcstok_s))(wchar_t *restrict, const wchar_t *restrict, wchar_t **restrict) = wcstok_s;
+
+wchar_t * __attribute__ ((alias ("wcstok_s"))) __cdecl wcstok (wchar_t *restrict, const wchar_t *restrict, wchar_t **restrict);
+extern wchar_t * (__cdecl * __attribute__((alias (__MINGW64_STRINGIFY(__MINGW_IMP_SYMBOL(wcstok_s))))) __MINGW_IMP_SYMBOL(wcstok))(wchar_t *restrict, const wchar_t *restrict, wchar_t **restrict);
diff --git a/mingw-w64-headers/crt/string.h b/mingw-w64-headers/crt/string.h
index c160c3c..813cd5b 100644
--- a/mingw-w64-headers/crt/string.h
+++ b/mingw-w64-headers/crt/string.h
@@ -144,10 +144,13 @@
   _CONST_RETURN wchar_t *__cdecl wcsrchr(const wchar_t *_Str,wchar_t _Ch);
   size_t __cdecl wcsspn(const wchar_t *_Str,const wchar_t *_Control);
   _CONST_RETURN wchar_t *__cdecl wcsstr(const wchar_t *_Str,const wchar_t *_SubStr);
-#if defined(_UCRT)
-  wchar_t *__cdecl wcstok(wchar_t * __restrict__ _Str,const wchar_t * __restrict__ _Delim,wchar_t **_Ptr) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
-#else
-  wchar_t *__cdecl wcstok(wchar_t * __restrict__ _Str,const wchar_t * __restrict__ _Delim) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
+  wchar_t *__cdecl wcstok(wchar_t * __restrict__ _Str,const wchar_t * __restrict__ _Delim,wchar_t ** __restrict__ _Ptr) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
+  wchar_t *__cdecl _wcstok(wchar_t * __restrict__ _Str,const wchar_t * __restrict__ _Delim) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
+#if defined(_CRT_NON_CONFORMING_WCSTOK) && !defined(__cplusplus)
+  #define wcstok _wcstok
+#endif
+#if !defined(_CRT_NO_INLINE_DEPRECATED_WCSTOK) && defined(__cplusplus)
+  extern "C++" inline wchar_t *__cdecl wcstok(wchar_t * __restrict__ _Str,const wchar_t * __restrict__ _Delim) { return _wcstok(_Str, _Delim); }
 #endif
   _CRTIMP wchar_t *__cdecl _wcserror(int _ErrNum) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
   _CRTIMP wchar_t *__cdecl __wcserror(const wchar_t *_Str) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
diff --git a/mingw-w64-headers/crt/wchar.h b/mingw-w64-headers/crt/wchar.h
index e05da79..6aa1daf 100644
--- a/mingw-w64-headers/crt/wchar.h
+++ b/mingw-w64-headers/crt/wchar.h
@@ -1214,10 +1214,13 @@
   _CONST_RETURN wchar_t *__cdecl wcsrchr(const wchar_t *_Str,wchar_t _Ch);
   size_t __cdecl wcsspn(const wchar_t *_Str,const wchar_t *_Control);
   _CONST_RETURN wchar_t *__cdecl wcsstr(const wchar_t *_Str,const wchar_t *_SubStr);
-#if defined(_UCRT)
-  wchar_t *__cdecl wcstok(wchar_t * __restrict__ _Str,const wchar_t * __restrict__ _Delim,wchar_t **_Ptr) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
-#else
-  wchar_t *__cdecl wcstok(wchar_t * __restrict__ _Str,const wchar_t * __restrict__ _Delim) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
+  wchar_t *__cdecl wcstok(wchar_t * __restrict__ _Str,const wchar_t * __restrict__ _Delim,wchar_t ** __restrict__ _Ptr) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
+  wchar_t *__cdecl _wcstok(wchar_t * __restrict__ _Str,const wchar_t * __restrict__ _Delim) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
+#if defined(_CRT_NON_CONFORMING_WCSTOK) && !defined(__cplusplus)
+  #define wcstok _wcstok
+#endif
+#if !defined(_CRT_NO_INLINE_DEPRECATED_WCSTOK) && defined(__cplusplus)
+  extern "C++" inline wchar_t *__cdecl wcstok(wchar_t * __restrict__ _Str,const wchar_t * __restrict__ _Delim) { return _wcstok(_Str, _Delim); }
 #endif
   _CRTIMP wchar_t *__cdecl _wcserror(int _ErrNum) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
   _CRTIMP wchar_t *__cdecl __wcserror(const wchar_t *_Str) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;