From eee8a51fe9107828de6076803d4c6432d28cd186 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 18 Aug 2004 02:22:59 +0000 Subject: * win32/win32.c (init_env): initialize HOME and USER environment variables unless set. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@6782 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- win32/win32.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 14 deletions(-) (limited to 'win32') diff --git a/win32/win32.c b/win32/win32.c index fa9136670..1f4b5472f 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; } -- cgit