From c41cefd4929b73536789f3dc7a9e093fcedec29d Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 18 Aug 2004 02:22:59 +0000 Subject: [PATCH] * win32/win32.c (init_env): initialize HOME and USER environment variables unless set. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6782 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 ++++- win32/win32.c | 75 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 67 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index a7dab6842b..5202388d28 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed Aug 18 11:22:52 2004 Nobuyoshi Nakada + + * win32/win32.c (init_env): initialize HOME and USER environment + variables unless set. + Tue Aug 17 01:36:32 2004 Dave Thomas * lib/rdoc/usage.rb: Remove extra indent. Tidy 'ri' option @@ -109,7 +114,7 @@ Mon Aug 2 11:48:29 2004 Dave Thomas Sat Jul 31 23:08:00 2004 Yukihiro Matsumoto * eval.c (is_defined): stupid mistakes fixed. [ruby-dev:24006] - + Sat Jul 31 17:39:47 2004 Nobuyoshi Nakada * misc/ruby-mode.el (ruby-expr-beg, ruby-parse-partial, diff --git a/win32/win32.c b/win32/win32.c index fa91366703..1f4b5472fe 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -25,6 +25,7 @@ #include #include #include +#include #ifdef __MINGW32__ #include #endif @@ -349,6 +350,64 @@ flock(int fd, int oper) (DWORD)-1); } +static void init_env(void) +{ + char env[_MAX_PATH]; + DWORD len; + BOOL f; + LPITEMIDLIST pidl; + + if (!GetEnvironmentVariable("HOME", env, sizeof(env))) { + f = FALSE; + if (GetEnvironmentVariable("HOMEDRIVE", env, sizeof(env))) + len = strlen(env); + else + len = 0; + if (GetEnvironmentVariable("HOMEPATH", env + len, sizeof(env) - len) || len) { + f = TRUE; + } + else if (GetEnvironmentVariable("USERPROFILE", env, sizeof(env))) { + f = TRUE; + } + else if (SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl) == 0) { + LPMALLOC alloc; + f = SHGetPathFromIDList(pidl, env); + SHGetMalloc(&alloc); + alloc->lpVtbl->Free(alloc, pidl); + alloc->lpVtbl->Release(alloc); + } + if (f) { + char *p = env; + while (*p) { + if (*p == '\\') *p = '/'; + p = CharNext(p); + } + if (p - env == 2 && env[1] == ':') { + *p++ = '/'; + *p = 0; + } + SetEnvironmentVariable("HOME", env); + } + } + if (GetEnvironmentVariable("USER", env, sizeof env)) { + len = strlen(env); + } + if (GetEnvironmentVariable("USERNAME", env, sizeof env)) { + len = strlen(env); + SetEnvironmentVariable("USER", env); + } + else if (GetUserName(env, (len = sizeof env, &len))) { + SetEnvironmentVariable("USER", env); + } + else { + NTLoginName = ""; + return; + } + NTLoginName = ALLOC_N(char, len+1); + strncpy(NTLoginName, env, len); + NTLoginName[len] = '\0'; +} + // // Initialization stuff // @@ -374,6 +433,8 @@ NtInitialize(int *argc, char ***argv) tzset(); + init_env(); + // Initialize Winsock StartSockets(); @@ -386,20 +447,6 @@ NtInitialize(int *argc, char ***argv) char * getlogin() { - char buffer[200]; - DWORD len = 200; - extern char *NTLoginName; - - if (NTLoginName == NULL) { - if (GetUserName(buffer, &len)) { - NTLoginName = ALLOC_N(char, len+1); - strncpy(NTLoginName, buffer, len); - NTLoginName[len] = '\0'; - } - else { - NTLoginName = ""; - } - } return NTLoginName; }