From 772acb9f1d17d603030cde5660b2ff8773297be5 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 11 Mar 2010 22:25:22 +0000 Subject: * win32/win32.c (init_env): get rid of alloca() for outer string. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26883 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- win32/win32.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'win32') diff --git a/win32/win32.c b/win32/win32.c index 268c3c7480..46a2ce5016 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -434,10 +434,18 @@ static void init_env(void) { static const WCHAR TMPDIR[] = L"TMPDIR"; - WCHAR env[_MAX_PATH]; - WCHAR *buf; + struct {WCHAR name[6], eq, val[_MAX_PATH];} wk; DWORD len; BOOL f; +#define env wk.val +#define set_env_val(vname) do { \ + typedef char namesizecheck[numberof(wk.name) < numberof(vname) - 1 ? -1 : 1]; \ + WCHAR *const buf = wk.name + numberof(wk.name) - numberof(vname); \ + MEMCPY(buf, vname, WCHAR, numberof(vname) - 1); \ + _wputenv(buf); \ + } while (0) + + wk.eq = L'='; if (!GetEnvironmentVariableW(L"HOME", env, numberof(env))) { f = FALSE; @@ -459,9 +467,7 @@ init_env(void) } if (f) { regulate_path(env); - buf = ALLOCA_N(WCHAR, 5 + lstrlenW(env) + 1); - wsprintfW(buf, L"HOME=%s", env); - _wputenv(buf); + set_env_val(L"HOME"); } } @@ -471,9 +477,7 @@ init_env(void) NTLoginName = ""; return; } - buf = ALLOCA_N(WCHAR, 5 + lstrlenW(env) + 1); - wsprintfW(buf, L"USER=%s", env); - _wputenv(buf); + set_env_val(L"USER"); } NTLoginName = strdup(rb_w32_getenv("USER")); @@ -487,11 +491,12 @@ init_env(void) if (*(p - 1) != L'/') *p++ = L'/'; if (p - env + numberof(temp) < numberof(env)) { memcpy(p, temp, sizeof(temp)); - buf = ALLOCA_N(WCHAR, lstrlenW(TMPDIR) + 1 + lstrlenW(env) + 1); - wsprintfW(buf, L"%s=%s", TMPDIR, env); - _wputenv(buf); + set_env_val(TMPDIR); } } + +#undef env +#undef set_env_val } -- cgit v1.2.3