gcc:
[official-gcc.git] / libjava / win32.cc
blob6fc2de08760c8bca5e023823852faed1d5791d32
1 // win32.cc - Helper functions for Microsoft-flavored OSs.
3 /* Copyright (C) 2002, 2003 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
12 #include <platform.h>
13 #include <jvm.h>
14 #include <sys/timeb.h>
15 #include <stdlib.h>
17 #include <java/lang/ArithmeticException.h>
18 #include <java/util/Properties.h>
20 static LONG CALLBACK
21 win32_exception_handler (LPEXCEPTION_POINTERS e)
23 if (e->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
24 _Jv_ThrowNullPointerException();
25 else if (e->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO)
26 throw new java::lang::ArithmeticException;
27 else
28 return EXCEPTION_CONTINUE_SEARCH;
31 // Platform-specific executable name
32 static char exec_name[MAX_PATH];
33 // initialized in _Jv_platform_initialize()
35 const char *_Jv_ThisExecutable (void)
37 return exec_name;
40 // Platform-specific VM initialization.
41 void
42 _Jv_platform_initialize (void)
44 // Initialise winsock for networking
45 WSADATA data;
46 if (WSAStartup (MAKEWORD (1, 1), &data))
47 MessageBox (NULL, "Error initialising winsock library.", "Error",
48 MB_OK | MB_ICONEXCLAMATION);
50 // Install exception handler
51 SetUnhandledExceptionFilter (win32_exception_handler);
53 // Initialize our executable name
54 GetModuleFileName(NULL, exec_name, sizeof(exec_name));
57 // gettimeofday implementation.
58 jlong
59 _Jv_platform_gettimeofday ()
61 struct timeb t;
62 ftime (&t);
63 return t.time * 1000LL + t.millitm;
66 // The following definitions "fake out" mingw to think that -mthreads
67 // was enabled and that mingwthr.dll was linked. GCJ-compiled
68 // applications don't need this helper library because we can safely
69 // detect thread death (return from Thread.run()).
71 int _CRT_MT = 1;
73 extern "C" int
74 __mingwthr_key_dtor (DWORD, void (*) (void *))
76 // FIXME: for now we do nothing; this causes a memory leak of
77 // approximately 24 bytes per thread created.
78 return 0;
81 // Set platform-specific System properties.
82 void
83 _Jv_platform_initProperties (java::util::Properties* newprops)
85 // A convenience define.
86 #define SET(Prop,Val) \
87 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
89 SET ("file.separator", "\\");
90 SET ("path.separator", ";");
91 SET ("line.separator", "\r\n");
93 // Use GetCurrentDirectory to set 'user.dir'.
94 DWORD buflen = MAX_PATH;
95 char *buffer = (char *) _Jv_MallocUnchecked (buflen);
96 if (buffer != NULL)
98 if (GetCurrentDirectory (buflen, buffer))
99 SET ("user.dir", buffer);
101 if (GetTempPath (buflen, buffer))
102 SET ("java.io.tmpdir", buffer);
104 _Jv_Free (buffer);
107 // Use GetUserName to set 'user.name'.
108 buflen = 257; // UNLEN + 1
109 buffer = (char *) _Jv_MallocUnchecked (buflen);
110 if (buffer != NULL)
112 if (GetUserName (buffer, &buflen))
113 SET ("user.name", buffer);
114 _Jv_Free (buffer);
117 // According to the api documentation for 'GetWindowsDirectory()', the
118 // environmental variable HOMEPATH always specifies the user's home
119 // directory or a default directory. On the 3 windows machines I checked
120 // only 1 had it set. If it's not set, JDK1.3.1 seems to set it to
121 // the windows directory, so we'll do the same.
122 char *userHome = NULL;
123 if ((userHome = ::getenv ("HOMEPATH")) == NULL )
125 // Check HOME since it's what I use.
126 if ((userHome = ::getenv ("HOME")) == NULL )
128 // Not found - use the windows directory like JDK1.3.1 does.
129 char *winHome = (char *) _Jv_MallocUnchecked (MAX_PATH);
130 if (winHome != NULL)
132 if (GetWindowsDirectory (winHome, MAX_PATH))
133 SET ("user.home", winHome);
134 _Jv_Free (winHome);
138 if (userHome != NULL)
139 SET ("user.home", userHome);
141 // Get and set some OS info.
142 OSVERSIONINFO osvi;
143 ZeroMemory (&osvi, sizeof(OSVERSIONINFO));
144 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
145 if (GetVersionEx (&osvi))
147 char *buffer = (char *) _Jv_MallocUnchecked (30);
148 if (buffer != NULL)
150 sprintf (buffer, "%d.%d", (int) osvi.dwMajorVersion,
151 (int) osvi.dwMinorVersion);
152 SET ("os.version", buffer);
153 _Jv_Free (buffer);
156 switch (osvi.dwPlatformId)
158 case VER_PLATFORM_WIN32_WINDOWS:
159 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
160 SET ("os.name", "Windows 95");
161 else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
162 SET ("os.name", "Windows 98");
163 else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
164 SET ("os.name", "Windows Me");
165 else
166 SET ("os.name", "Windows ??");
167 break;
169 case VER_PLATFORM_WIN32_NT:
170 if (osvi.dwMajorVersion <= 4 )
171 SET ("os.name", "Windows NT");
172 else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
173 SET ("os.name", "Windows 2000");
174 else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
175 SET ("os.name", "Windows XP");
176 else
177 SET ("os.name", "Windows NT ??");
178 break;
180 default:
181 SET ("os.name", "Windows UNKNOWN");
182 break;
186 // Set the OS architecture.
187 SYSTEM_INFO si;
188 GetSystemInfo (&si);
189 switch (si.dwProcessorType)
191 case PROCESSOR_INTEL_386:
192 SET ("os.arch", "i386");
193 break;
194 case PROCESSOR_INTEL_486:
195 SET ("os.arch", "i486");
196 break;
197 case PROCESSOR_INTEL_PENTIUM:
198 SET ("os.arch", "i586");
199 break;
200 case PROCESSOR_MIPS_R4000:
201 SET ("os.arch", "MIPS4000");
202 break;
203 case PROCESSOR_ALPHA_21064:
204 SET ("os.arch", "ALPHA");
205 break;
206 default:
207 SET ("os.arch", "unknown");
208 break;
212 /* Store up to SIZE return address of the current program state in
213 ARRAY and return the exact number of values stored. */
215 backtrace (void **__array, int __size)
217 register void *_ebp __asm__ ("ebp");
218 register void *_esp __asm__ ("esp");
219 unsigned int *rfp;
221 int i=0;
222 for (rfp = *(unsigned int**)_ebp;
223 rfp && i < __size;
224 rfp = *(unsigned int **)rfp)
226 int diff = *rfp - (unsigned int)rfp;
227 if ((void*)rfp < _esp || diff > 4 * 1024 || diff < 0) break;
229 __array[i++] = (void*)(rfp[1]-4);
231 return i;