winstorecompat: provide GetUserName

It's needed by:
- getlogin() in mingwex

GetEnvironmentVariableA("USERNAME") gives the same information (tested on
win10 in cmd.exe and PowerShell).

No implementation provided when building winstorecompat for win8 as
GetEnvironmentVariableA is not allowed and GetUserNameEx requires -lsecur32.

Signed-off-by: Martin Storsjö <martin@martin.st>
diff --git a/mingw-w64-headers/include/winbase.h b/mingw-w64-headers/include/winbase.h
index 0dc0782..1bc8531 100644
--- a/mingw-w64-headers/include/winbase.h
+++ b/mingw-w64-headers/include/winbase.h
@@ -2409,8 +2409,6 @@
   WINBASEAPI WINBOOL WINAPI SetComputerNameExA (COMPUTER_NAME_FORMAT NameType, LPCTSTR lpBuffer);
   WINBASEAPI WINBOOL WINAPI DnsHostnameToComputerNameA (LPCSTR Hostname, LPSTR ComputerName, LPDWORD nSize);
   WINBASEAPI WINBOOL WINAPI DnsHostnameToComputerNameW (LPCWSTR Hostname, LPWSTR ComputerName, LPDWORD nSize);
-  WINADVAPI WINBOOL WINAPI GetUserNameA (LPSTR lpBuffer, LPDWORD pcbBuffer);
-  WINADVAPI WINBOOL WINAPI GetUserNameW (LPWSTR lpBuffer, LPDWORD pcbBuffer);
 
 #ifndef UNICODE
 #define SetComputerNameEx SetComputerNameExA
@@ -2418,7 +2416,6 @@
 
 #define SetComputerName __MINGW_NAME_AW(SetComputerName)
 #define DnsHostnameToComputerName __MINGW_NAME_AW(DnsHostnameToComputerName)
-#define GetUserName __MINGW_NAME_AW(GetUserName)
 
 #define LOGON32_LOGON_INTERACTIVE 2
 #define LOGON32_LOGON_NETWORK 3
@@ -2504,6 +2501,12 @@
 #define VerifyVersionInfo __MINGW_NAME_AW(VerifyVersionInfo)
 #endif
 
+#if WINAPI_FAMILY_PARTITION (WINAPI_PARTITION_DESKTOP) || defined(WINSTORECOMPAT)
+  WINADVAPI WINBOOL WINAPI GetUserNameA (LPSTR lpBuffer, LPDWORD pcbBuffer);
+  WINADVAPI WINBOOL WINAPI GetUserNameW (LPWSTR lpBuffer, LPDWORD pcbBuffer);
+#define GetUserName __MINGW_NAME_AW(GetUserName)
+#endif
+
 #if WINAPI_FAMILY_PARTITION (WINAPI_PARTITION_APP)
   WINADVAPI WINBOOL WINAPI LookupAccountNameA (LPCSTR lpSystemName, LPCSTR lpAccountName, PSID Sid, LPDWORD cbSid, LPSTR ReferencedDomainName, LPDWORD cchReferencedDomainName, PSID_NAME_USE peUse);
   WINADVAPI WINBOOL WINAPI LookupAccountNameW (LPCWSTR lpSystemName, LPCWSTR lpAccountName, PSID Sid, LPDWORD cbSid, LPWSTR ReferencedDomainName, LPDWORD cchReferencedDomainName, PSID_NAME_USE peUse);
diff --git a/mingw-w64-libraries/winstorecompat/Makefile.am b/mingw-w64-libraries/winstorecompat/Makefile.am
index 5423ab9..a1f7994 100644
--- a/mingw-w64-libraries/winstorecompat/Makefile.am
+++ b/mingw-w64-libraries/winstorecompat/Makefile.am
@@ -41,4 +41,5 @@
   src/RtlCaptureContext.c \
   src/RtlVirtualUnwind.c \
   src/RtlRestoreContext.c \
+  src/GetUserName.c \
   $(NULL)
diff --git a/mingw-w64-libraries/winstorecompat/src/GetUserName.c b/mingw-w64-libraries/winstorecompat/src/GetUserName.c
new file mode 100644
index 0000000..52ae9db
--- /dev/null
+++ b/mingw-w64-libraries/winstorecompat/src/GetUserName.c
@@ -0,0 +1,63 @@
+/*
+    Copyright (c) 2020 mingw-w64 project
+
+    Contributing authors: Steve Lhomme
+
+    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.
+*/
+
+#define GetUserNameA __GetUserNameA
+#define GetUserNameW __GetUserNameW
+#include <windows.h>
+#undef GetUserNameA
+#undef GetUserNameW
+
+WINBOOL WINAPI GetUserNameA(LPSTR lpBuffer, LPDWORD pcbBuffer)
+{
+#if _WIN32_WINNT >= _WIN32_WINNT_WIN10
+    DWORD written = GetEnvironmentVariableA("USERNAME", lpBuffer, *pcbBuffer);
+    if (written != 0 && written <= *pcbBuffer) {
+        *pcbBuffer = written + 1;
+        return TRUE;
+    }
+#endif
+    SetLastError(ERROR_ENVVAR_NOT_FOUND);
+    return FALSE;
+}
+
+WINBOOL WINAPI GetUserNameW(LPWSTR lpBuffer, LPDWORD pcbBuffer)
+{
+#if _WIN32_WINNT >= _WIN32_WINNT_WIN10
+    DWORD written = GetEnvironmentVariableW(L"USERNAME", lpBuffer, *pcbBuffer);
+    if (written != 0 && written <= *pcbBuffer) {
+        *pcbBuffer = written + 1;
+        return TRUE;
+    }
+#endif
+    SetLastError(ERROR_ENVVAR_NOT_FOUND);
+    return FALSE;
+}
+
+#ifdef _X86_
+WINBOOL(*__MINGW_IMP_SYMBOL(GetUserNameA))(LPSTR lpBuffer, LPDWORD pcbBuffer) asm("__imp__GetUserNameA@8") = GetUserNameA;
+WINBOOL(*__MINGW_IMP_SYMBOL(GetUserNameW))(LPWSTR lpBuffer, LPDWORD pcbBuffer) asm("__imp__GetUserNameW@8") = GetUserNameW;
+#else
+WINBOOL(*__MINGW_IMP_SYMBOL(GetUserNameA))(LPSTR lpBuffer, LPDWORD pcbBuffer) asm("__imp_GetUserNameA") = GetUserNameA;
+WINBOOL(*__MINGW_IMP_SYMBOL(GetUserNameW))(LPWSTR lpBuffer, LPDWORD pcbBuffer) asm("__imp_GetUserNameW") = GetUserNameW;
+#endif