Skip to content

Commit

Permalink
[core] fix handling of Unicode in user paths
Browse files Browse the repository at this point in the history
* What do you know, Microsoft screwed everyone with a *BROKEN* _wgetenv()...
  • Loading branch information
pbatard committed Apr 18, 2017
1 parent 7cfd33a commit 4817f0c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
5 changes: 3 additions & 2 deletions examples/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include <limits.h>

#include "profile.h"
#include "msapi_utf8.h"

#if defined(_MSC_VER)
#define strcasecmp _stricmp
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/zadig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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<ARRAYSIZE(system_dir); i++) {
path[0] = 0;
safe_strcpy(path, MAX_PATH, getenv("WINDIR"));
safe_strcpy(path, MAX_PATH, getenvU("WINDIR"));
safe_strcat(path, MAX_PATH, "\\");
safe_strcat(path, MAX_PATH, system_dir[i]);
safe_strcat(path, MAX_PATH, "\\libusb-1.0.dll");
Expand Down
6 changes: 3 additions & 3 deletions libwdi/libwdi.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ int get_version_info(int driver_type, VS_FIXEDFILEINFO* driver_info)
}

// First, we need a physical file => extract it
tmpdir = getenv("TEMP");
tmpdir = getenvU("TEMP");
if (tmpdir == NULL) {
wdi_warn("unable to use TEMP to extract file");
return WDI_ERROR_RESOURCE;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions libwdi/msapi_utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 4817f0c

Please sign in to comment.