From 4817f0c05519c45be13ad151559389a5dd996682 Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Tue, 18 Apr 2017 20:04:13 +0100 Subject: [PATCH] [core] fix handling of Unicode in user paths * What do you know, Microsoft screwed everyone with a *BROKEN* _wgetenv()... --- examples/profile.c | 5 +++-- examples/zadig.c | 2 +- libwdi/libwdi.c | 6 +++--- libwdi/msapi_utf8.h | 13 +++++++++++-- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/examples/profile.c b/examples/profile.c index cfc5bb0b..5bfcf72d 100644 --- a/examples/profile.c +++ b/examples/profile.c @@ -57,6 +57,7 @@ #include #include "profile.h" +#include "msapi_utf8.h" #if defined(_MSC_VER) #define strcasecmp _stricmp @@ -243,7 +244,7 @@ long profile_open_file(const char * filespec, len = (unsigned int)strlen(filespec)+1; if (filespec[0] == '~' && filespec[1] == '/') { - home_env = getenv("HOME"); + home_env = getenvU("HOME"); if (home_env) len += (unsigned int)strlen(home_env); } @@ -291,7 +292,7 @@ long profile_update_file(prf_file_t prf) if (retval) return retval; errno = 0; - f = fopen(prf->filespec, "r"); + f = fopenU(prf->filespec, "r"); if (f == NULL) { retval = errno; if (retval == 0) diff --git a/examples/zadig.c b/examples/zadig.c index d171d31b..6c1fb870 100644 --- a/examples/zadig.c +++ b/examples/zadig.c @@ -1793,7 +1793,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine if ((msg.message == WM_SYSKEYDOWN) && (msg.wParam == 'Z')) { for (r = TRUE, i = 0; i extract it - tmpdir = getenv("TEMP"); + tmpdir = getenvU("TEMP"); if (tmpdir == NULL) { wdi_warn("unable to use TEMP to extract file"); return WDI_ERROR_RESOURCE; @@ -1072,7 +1072,7 @@ int LIBWDI_API wdi_prepare_driver(struct wdi_device_info* device_info, const cha // Try to use the user's temp dir if no path is provided if ((path == NULL) || (path[0] == 0)) { - path = getenv("TEMP"); + path = getenvU("TEMP"); if (path == NULL) { wdi_err("no path provided and unable to use TEMP"); MUTEX_RETURN(WDI_ERROR_INVALID_PARAM); @@ -1445,7 +1445,7 @@ static int install_driver_internal(void* arglist) // Try to use the user's temp dir if no path is provided if ((params->path == NULL) || (params->path[0] == 0)) { - static_strcpy(path, getenv("TEMP")); + static_strcpy(path, getenvU("TEMP")); wdi_info("no path provided - installing from '%s'", path); } else { static_strcpy(path, params->path); diff --git a/libwdi/msapi_utf8.h b/libwdi/msapi_utf8.h index f0445bea..f9334551 100644 --- a/libwdi/msapi_utf8.h +++ b/libwdi/msapi_utf8.h @@ -649,8 +649,17 @@ static __inline FILE* fopenU(const char* filename, const char* mode) static __inline char* getenvU(const char* varname) { wconvert(varname); - char* ret; - ret = wchar_to_utf8(_wgetenv(wvarname)); + char* ret = NULL; + wchar_t* wbuf = NULL; + // _wgetenv() is *BROKEN* in MS compilers => use GetEnvironmentVariableW() + DWORD dwSize = GetEnvironmentVariableW(wvarname, wbuf, 0); + wbuf = calloc(dwSize, sizeof(wchar_t)); + if (wbuf == NULL) + return NULL; + dwSize = GetEnvironmentVariableW(wvarname, wbuf, dwSize); + if (dwSize != 0) + ret = wchar_to_utf8(wbuf); + free(wbuf); wfree(varname); return ret; }