summaryrefslogtreecommitdiff
path: root/win32
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-10-02 12:18:35 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-10-02 12:18:35 +0000
commitb5aaf376b0d78253ef510ea3438c7babf4b9a71d (patch)
treed14b49a13ef00101896ac054a18d1d72322087e3 /win32
parent02914c8a16194cb8afd89dc93ba6eca2512d8e2c (diff)
* 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@13601 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'win32')
-rw-r--r--win32/Makefile.sub6
-rw-r--r--win32/win32.c80
2 files changed, 64 insertions, 22 deletions
diff --git a/win32/Makefile.sub b/win32/Makefile.sub
index e555399b2c..a6df99dd1f 100644
--- a/win32/Makefile.sub
+++ b/win32/Makefile.sub
@@ -130,11 +130,13 @@ RFLAGS = -r
!if !defined(EXTLIBS)
EXTLIBS =
!endif
+LIBS = oldnames.lib user32.lib advapi32.lib shell32.lib
!if !defined(USE_WINSOCK2)
-LIBS = oldnames.lib user32.lib advapi32.lib wsock32.lib $(EXTLIBS)
+LIBS = $(LIBS) wsock32.lib
!else
-LIBS = oldnames.lib user32.lib advapi32.lib ws2_32.lib $(EXTLIBS)
+LIBS = $(LIBS) ws2_32.lib
!endif
+LIBS = $(LIBS) $(EXTLIBS)
MISSING = acosh.obj crypt.obj erf.obj win32.obj
ARFLAGS = -machine:$(MACHINE) -out:
diff --git a/win32/win32.c b/win32/win32.c
index 98fb96e908..5dd3583e8a 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -25,6 +25,7 @@
#include <windows.h>
#include <winbase.h>
#include <wincon.h>
+#include <shlobj.h>
#ifdef __MINGW32__
#include <mswsock.h>
#include <mbstring.h>
@@ -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 = "<Unknown>";
+ 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 = "<Unknown>";
- }
- }
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");
}
}