Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions lib/getenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,19 @@

#include "memdebug.h"

static char *GetEnv(const char *variable)
#ifdef WIN32
static TCHAR *GetEnvWin32(const TCHAR *variable)
{
#if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP)
(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;
TCHAR *buf = NULL;
TCHAR *tmp;
DWORD bufsize;
DWORD rc = 1;
const DWORD max = 32768; /* max env var size from MSCRT source */

for(;;) {
tmp = realloc(buf, rc);
tmp = (TCHAR*)realloc(buf, rc * sizeof(TCHAR));
if(!tmp) {
free(buf);
return NULL;
Expand All @@ -53,18 +50,42 @@ 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 = GetEnvironmentVariable(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

static char *GetEnv(const char *variable)
{
#if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP)
(void)variable;
return NULL;
#elif defined(WIN32)
#ifdef UNICODE
char *value = NULL;
wchar_t *variable_w = curlx_convert_UTF8_to_wchar(variable);
if(variable_w) {
wchar_t *value_w = GetEnvWin32(variable_w);
if(value_w) {
value = curlx_convert_wchar_to_UTF8(value_w);
free(value_w);
}
free(variable_w);
}
return value;
#else
return GetEnvWin32(variable);
#endif
#else
char *env = getenv(variable);
return (env && env[0])?strdup(env):NULL;
Expand Down