lib/getenv.c | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/getenv.c b/lib/getenv.c index 48ee972..fe27a24 100644 --- a/lib/getenv.c +++ b/lib/getenv.c @@ -26,26 +26,23 @@ #include #include "curl_memory.h" +#include "curlx.h" #include "memdebug.h" -static char *GetEnv(const char *variable) +#if defined(_WIN32) +static wchar_t *GetEnv(const wchar_t *variable) { -#if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP) || \ - defined(__ORBIS__) || defined(__PROSPERO__) /* PlayStation 4 and 5 */ - (void)variable; - return NULL; -#elif defined(_WIN32) /* This uses Windows API instead of C runtime getenv() to get the environment variable since some changes aren't always visible to the latter. #4774 */ - char *buf = NULL; - char *tmp; + wchar_t *buf = NULL; + wchar_t *tmp; DWORD bufsize; DWORD rc = 1; const DWORD max = 32768; /* max env var size from MSCRT source */ for(;;) { - tmp = realloc(buf, rc); + tmp = (wchar_t*)realloc(buf, rc * sizeof(wchar_t)); if(!tmp) { free(buf); return NULL; @@ -56,25 +53,41 @@ static char *GetEnv(const char *variable) /* It's possible for rc to be 0 if the variable was found but empty. Since getenv doesn't make that distinction we ignore it as well. */ - rc = GetEnvironmentVariableA(variable, buf, bufsize); + rc = GetEnvironmentVariableW(variable, buf, bufsize); if(!rc || rc == bufsize || rc > max) { free(buf); return NULL; } /* if rc < bufsize then rc is bytes written not including null */ - if(rc < bufsize) + if(rc < bufsize) { return buf; - + } /* else rc is bytes needed, try again */ } +} +#endif + +char *curl_getenv(const char *variable) +{ +#if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP) || \ + defined(__ORBIS__) || defined(__PROSPERO__) /* PlayStation 4 and 5 */ + (void)variable; + return NULL; +#elif defined(_WIN32) + char *value = NULL; + wchar_t *variable_w = curlx_convert_UTF8_to_wchar(variable); + if(variable_w) { + wchar_t *value_w = GetEnv(variable_w); + if(value_w) { + value = curlx_convert_wchar_to_UTF8(value_w); + free(value_w); + } + free(variable_w); + } + return value; #else char *env = getenv(variable); return (env && env[0])?strdup(env):NULL; #endif } - -char *curl_getenv(const char *v) -{ - return GetEnv(v); -}