From 82c551e974b58489f82268bda749939dc40b0c65 Mon Sep 17 00:00:00 2001 From: shyouhei Date: Thu, 5 Jun 2008 03:28:21 +0000 Subject: merge revision(s) 13601: * win32/win32.c (init_env): initialize HOME and USER environment variables unless set. [ruby-core:12328] (merge from trunk) * win32/win32.c (NtInitialize, getlogin): ditto. * configure.in, win32/Makefile.sub (LIBS): need to link shell32 library for SH* functions on mswin32 and mingw32. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_6@16831 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- win32/win32.c | 80 ++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 20 deletions(-) (limited to 'win32/win32.c') diff --git a/win32/win32.c b/win32/win32.c index 0989735faf..aa46011e8a 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -25,6 +25,7 @@ #include #include #include +#include #ifdef __MINGW32__ #include #include @@ -383,6 +384,60 @@ exit_handler(void) } } +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)) { + if (GetEnvironmentVariable("USERNAME", env, sizeof env) || + GetUserName(env, (len = sizeof env, &len))) { + SetEnvironmentVariable("USER", env); + } + else { + NTLoginName = ""; + return; + } + } + NTLoginName = strdup(env); +} + // // Initialization stuff // @@ -411,6 +466,8 @@ NtInitialize(int *argc, char ***argv) tzset(); + init_env(); + init_stdhandle(); atexit(exit_handler); @@ -427,21 +484,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 = (char *)malloc(len+1); - if (!NTLoginName) return NULL; - strncpy(NTLoginName, buffer, len); - NTLoginName[len] = '\0'; - } - else { - NTLoginName = ""; - } - } return NTLoginName; } @@ -1064,10 +1106,9 @@ insert(const char *path, VALUE vinfo) if (!tmpcurr) return -1; MEMZERO(tmpcurr, NtCmdLineElement, 1); tmpcurr->len = strlen(path); - tmpcurr->str = (char *)malloc(tmpcurr->len + 1); + tmpcurr->str = strdup(path); if (!tmpcurr->str) return -1; tmpcurr->flags |= NTMALLOC; - strcpy(tmpcurr->str, path); **tail = tmpcurr; *tail = &tmpcurr->next; @@ -1384,8 +1425,7 @@ rb_w32_cmdvector(const char *cmd, char ***vec) ptr = buffer + (elements+1) * sizeof(char *); while (curr = cmdhead) { - strncpy (ptr, curr->str, curr->len); - ptr[curr->len] = '\0'; + memcpy(ptr, curr->str, curr->len + 1); *vptr++ = ptr; ptr += curr->len + 1; cmdhead = curr->next; @@ -1870,7 +1910,7 @@ rb_w32_strerror(int e) e = GetLastError(); if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, &source, e, 0, - buffer, 512, NULL) == 0) { + buffer, sizeof(buffer), NULL) == 0) { strcpy(buffer, "Unknown Error"); } } -- cgit v1.2.3