]> gcc.gnu.org Git - gcc.git/blame - libjava/win32.cc
DatagramSocket.java (getLocalAddress): Renamed result variable to localAddr.
[gcc.git] / libjava / win32.cc
CommitLineData
b8fe3c1e
AM
1// win32.cc - Helper functions for Microsoft-flavored OSs.
2
a191802c 3/* Copyright (C) 2002, 2003 Free Software Foundation
b8fe3c1e
AM
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
10
11#include <config.h>
a191802c 12#include <platform.h>
8eeda6e0 13#include <sys/timeb.h>
455cd615 14#include <stdlib.h>
b8fe3c1e 15
1febeb40 16#include <java/lang/ArithmeticException.h>
5c144158
ME
17#include <java/lang/UnsupportedOperationException.h>
18#include <java/io/IOException.h>
19#include <java/net/SocketException.h>
455cd615 20#include <java/util/Properties.h>
73272ce6
TT
21
22static LONG CALLBACK
b8fe3c1e
AM
23win32_exception_handler (LPEXCEPTION_POINTERS e)
24{
25 if (e->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
26 _Jv_ThrowNullPointerException();
27 else if (e->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO)
28 throw new java::lang::ArithmeticException;
29 else
30 return EXCEPTION_CONTINUE_SEARCH;
31}
73272ce6 32
c068c638
AH
33// Platform-specific executable name
34static char exec_name[MAX_PATH];
35 // initialized in _Jv_platform_initialize()
36
37const char *_Jv_ThisExecutable (void)
38{
39 return exec_name;
40}
41
5c144158
ME
42// Helper classes and methods implementation
43
44// class WSAEventWrapper
45WSAEventWrapper::WSAEventWrapper (int fd, DWORD dwSelFlags):
46 m_hEvent(0),
47 m_fd(fd),
48 m_dwSelFlags(dwSelFlags)
49{
50 m_hEvent = WSACreateEvent ();
51 if (dwSelFlags)
52 WSAEventSelect(fd, m_hEvent, dwSelFlags);
53}
54
55WSAEventWrapper::~WSAEventWrapper ()
56{
57 if (m_dwSelFlags)
58 {
59 WSAEventSelect(m_fd, m_hEvent, 0);
60 if (m_dwSelFlags & (FD_ACCEPT | FD_CONNECT))
61 {
62 // Set the socket back to non-blocking mode.
63 // Ignore any error since we're in a destructor.
64 unsigned long lSockOpt = 0L;
65 // blocking mode
66 ::ioctlsocket (m_fd, FIONBIO, &lSockOpt);
67 }
68 }
69 WSACloseEvent (m_hEvent);
70}
71
72// Error string text.
73jstring
74_Jv_WinStrError (LPCTSTR lpszPrologue, int nErrorCode)
75{
76 LPTSTR lpMsgBuf = 0;
77
78 DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
79 FORMAT_MESSAGE_FROM_SYSTEM |
80 FORMAT_MESSAGE_IGNORE_INSERTS;
81
82 FormatMessage (dwFlags,
83 NULL,
84 (DWORD) nErrorCode,
85 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
86 (LPTSTR) &lpMsgBuf,
87 0,
88 NULL);
89
90 jstring ret;
91 if (lpszPrologue)
92 {
93 LPTSTR lpszTemp =
94 (LPTSTR) _Jv_Malloc (strlen (lpszPrologue) +
95 strlen (lpMsgBuf) + 3);
96 strcpy (lpszTemp, lpszPrologue);
97 strcat (lpszTemp, ": ");
98 strcat (lpszTemp, lpMsgBuf);
99 ret = JvNewStringLatin1 (lpszTemp);
100 }
101 else
102 {
103 ret = JvNewStringLatin1 (lpMsgBuf);
104 }
105
106 LocalFree(lpMsgBuf);
107 return ret;
108}
109
110jstring
111_Jv_WinStrError (int nErrorCode)
112{
113 return _Jv_WinStrError (0, nErrorCode);
114}
115
116void _Jv_ThrowIOException (DWORD dwErrorCode)
117{
118 throw new java::io::IOException (_Jv_WinStrError (dwErrorCode));
119}
120
121void _Jv_ThrowIOException()
122{
123 DWORD dwErrorCode = WSAGetLastError ();
124 _Jv_ThrowIOException (dwErrorCode);
125}
126
127void _Jv_ThrowSocketException (DWORD dwErrorCode)
128{
129 throw new java::net::SocketException (_Jv_WinStrError (dwErrorCode));
130}
131
132void _Jv_ThrowSocketException()
133{
134 DWORD dwErrorCode = WSAGetLastError ();
135 _Jv_ThrowSocketException (dwErrorCode);
136}
137
73272ce6
TT
138// Platform-specific VM initialization.
139void
140_Jv_platform_initialize (void)
141{
142 // Initialise winsock for networking
143 WSADATA data;
144 if (WSAStartup (MAKEWORD (1, 1), &data))
145 MessageBox (NULL, "Error initialising winsock library.", "Error",
5c144158
ME
146 MB_OK | MB_ICONEXCLAMATION);
147
73272ce6
TT
148 // Install exception handler
149 SetUnhandledExceptionFilter (win32_exception_handler);
5c144158 150
c068c638
AH
151 // Initialize our executable name
152 GetModuleFileName(NULL, exec_name, sizeof(exec_name));
73272ce6 153}
b32d2321
AM
154
155// gettimeofday implementation.
8eeda6e0
AM
156jlong
157_Jv_platform_gettimeofday ()
b32d2321 158{
8eeda6e0
AM
159 struct timeb t;
160 ftime (&t);
bb0774f3 161 return t.time * 1000LL + t.millitm;
b32d2321
AM
162}
163
9268f1c0
AM
164// The following definitions "fake out" mingw to think that -mthreads
165// was enabled and that mingwthr.dll was linked. GCJ-compiled
166// applications don't need this helper library because we can safely
167// detect thread death (return from Thread.run()).
168
169int _CRT_MT = 1;
170
171extern "C" int
172__mingwthr_key_dtor (DWORD, void (*) (void *))
173{
174 // FIXME: for now we do nothing; this causes a memory leak of
175 // approximately 24 bytes per thread created.
176 return 0;
177}
455cd615
AK
178
179// Set platform-specific System properties.
180void
181_Jv_platform_initProperties (java::util::Properties* newprops)
182{
183 // A convenience define.
184#define SET(Prop,Val) \
185 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
186
187 SET ("file.separator", "\\");
188 SET ("path.separator", ";");
189 SET ("line.separator", "\r\n");
455cd615
AK
190
191 // Use GetCurrentDirectory to set 'user.dir'.
192 DWORD buflen = MAX_PATH;
94ed0002 193 char *buffer = (char *) _Jv_MallocUnchecked (buflen);
455cd615
AK
194 if (buffer != NULL)
195 {
196 if (GetCurrentDirectory (buflen, buffer))
5c144158 197 SET ("user.dir", buffer);
94ed0002
AK
198
199 if (GetTempPath (buflen, buffer))
5c144158 200 SET ("java.io.tmpdir", buffer);
94ed0002 201
c646bade 202 _Jv_Free (buffer);
455cd615 203 }
5c144158 204
455cd615
AK
205 // Use GetUserName to set 'user.name'.
206 buflen = 257; // UNLEN + 1
94ed0002 207 buffer = (char *) _Jv_MallocUnchecked (buflen);
455cd615
AK
208 if (buffer != NULL)
209 {
210 if (GetUserName (buffer, &buflen))
211 SET ("user.name", buffer);
c646bade 212 _Jv_Free (buffer);
455cd615
AK
213 }
214
5c144158
ME
215 // According to the api documentation for 'GetWindowsDirectory()', the
216 // environmental variable HOMEPATH always specifies the user's home
455cd615
AK
217 // directory or a default directory. On the 3 windows machines I checked
218 // only 1 had it set. If it's not set, JDK1.3.1 seems to set it to
219 // the windows directory, so we'll do the same.
94ed0002
AK
220 char *userHome = NULL;
221 if ((userHome = ::getenv ("HOMEPATH")) == NULL )
455cd615
AK
222 {
223 // Check HOME since it's what I use.
94ed0002 224 if ((userHome = ::getenv ("HOME")) == NULL )
455cd615
AK
225 {
226 // Not found - use the windows directory like JDK1.3.1 does.
94ed0002
AK
227 char *winHome = (char *) _Jv_MallocUnchecked (MAX_PATH);
228 if (winHome != NULL)
455cd615
AK
229 {
230 if (GetWindowsDirectory (winHome, MAX_PATH))
5c144158 231 SET ("user.home", winHome);
c646bade 232 _Jv_Free (winHome);
455cd615
AK
233 }
234 }
235 }
94ed0002 236 if (userHome != NULL)
455cd615
AK
237 SET ("user.home", userHome);
238
239 // Get and set some OS info.
240 OSVERSIONINFO osvi;
241 ZeroMemory (&osvi, sizeof(OSVERSIONINFO));
242 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
243 if (GetVersionEx (&osvi))
244 {
94ed0002 245 char *buffer = (char *) _Jv_MallocUnchecked (30);
455cd615
AK
246 if (buffer != NULL)
247 {
94ed0002 248 sprintf (buffer, "%d.%d", (int) osvi.dwMajorVersion,
5c144158 249 (int) osvi.dwMinorVersion);
455cd615 250 SET ("os.version", buffer);
c646bade 251 _Jv_Free (buffer);
455cd615
AK
252 }
253
254 switch (osvi.dwPlatformId)
255 {
256 case VER_PLATFORM_WIN32_WINDOWS:
257 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
258 SET ("os.name", "Windows 95");
259 else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
260 SET ("os.name", "Windows 98");
261 else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
262 SET ("os.name", "Windows Me");
263 else
5c144158 264 SET ("os.name", "Windows ??");
455cd615
AK
265 break;
266
267 case VER_PLATFORM_WIN32_NT:
268 if (osvi.dwMajorVersion <= 4 )
269 SET ("os.name", "Windows NT");
270 else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
271 SET ("os.name", "Windows 2000");
272 else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
273 SET ("os.name", "Windows XP");
274 else
275 SET ("os.name", "Windows NT ??");
276 break;
277
278 default:
279 SET ("os.name", "Windows UNKNOWN");
280 break;
281 }
282 }
283
284 // Set the OS architecture.
285 SYSTEM_INFO si;
286 GetSystemInfo (&si);
14a5a676 287 switch (si.wProcessorArchitecture)
455cd615 288 {
14a5a676
RM
289 case PROCESSOR_ARCHITECTURE_INTEL:
290 SET ("os.arch", "x86");
455cd615 291 break;
14a5a676
RM
292 case PROCESSOR_ARCHITECTURE_MIPS:
293 SET ("os.arch", "mips");
455cd615 294 break;
14a5a676
RM
295 case PROCESSOR_ARCHITECTURE_ALPHA:
296 SET ("os.arch", "alpha");
455cd615 297 break;
14a5a676
RM
298 case PROCESSOR_ARCHITECTURE_PPC:
299 SET ("os.arch", "ppc");
455cd615 300 break;
14a5a676
RM
301 case PROCESSOR_ARCHITECTURE_IA64:
302 SET ("os.arch", "ia64");
455cd615 303 break;
14a5a676 304 case PROCESSOR_ARCHITECTURE_UNKNOWN:
455cd615
AK
305 default:
306 SET ("os.arch", "unknown");
307 break;
308 }
309}
c2a6704f
AM
310
311/* Store up to SIZE return address of the current program state in
312 ARRAY and return the exact number of values stored. */
313int
314backtrace (void **__array, int __size)
315{
316 register void *_ebp __asm__ ("ebp");
317 register void *_esp __asm__ ("esp");
318 unsigned int *rfp;
319
320 int i=0;
321 for (rfp = *(unsigned int**)_ebp;
322 rfp && i < __size;
323 rfp = *(unsigned int **)rfp)
324 {
325 int diff = *rfp - (unsigned int)rfp;
326 if ((void*)rfp < _esp || diff > 4 * 1024 || diff < 0) break;
327
328 __array[i++] = (void*)(rfp[1]-4);
329 }
330 return i;
331}
5c144158
ME
332
333int
334_Jv_select (int n, fd_set *readfds, fd_set *writefds,
335 fd_set *exceptfds, struct timeval *timeout)
336{
337 int r = ::select (n, readfds, writefds, exceptfds, timeout);
338 if (r == SOCKET_ERROR)
339 {
340 DWORD dwErrorCode = WSAGetLastError ();
341 throw new java::io::IOException (_Jv_WinStrError (dwErrorCode));
342 }
343 return r;
344}
This page took 0.203304 seconds and 5 git commands to generate.