OSDN Git Service

* java/lang/String.java: Reordered to follow Classpath; merged in
[pf3gnuchains/gcc-fork.git] / libjava / win32.cc
1 // win32.cc - Helper functions for Microsoft-flavored OSs.
2
3 /* Copyright (C) 2002, 2003  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12 #include <platform.h>
13 #include <jvm.h>
14 #include <sys/timeb.h>
15 #include <stdlib.h>
16
17 #include <java/lang/ArithmeticException.h>
18 #include <java/util/Properties.h>
19
20 static LONG CALLBACK
21 win32_exception_handler (LPEXCEPTION_POINTERS e)
22 {
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;
29 }
30
31 // Platform-specific executable name
32 static char exec_name[MAX_PATH];
33   // initialized in _Jv_platform_initialize()
34
35 const char *_Jv_ThisExecutable (void)
36 {
37   return exec_name;
38 }
39
40 // Platform-specific VM initialization.
41 void
42 _Jv_platform_initialize (void)
43 {
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);
49   
50   // Install exception handler
51   SetUnhandledExceptionFilter (win32_exception_handler);
52   
53   // Initialize our executable name
54   GetModuleFileName(NULL, exec_name, sizeof(exec_name));
55 }
56
57 // gettimeofday implementation.
58 jlong
59 _Jv_platform_gettimeofday ()
60 {
61   struct timeb t;
62   ftime (&t);
63   return t.time * 1000LL + t.millitm;
64 }
65
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()).
70
71 int _CRT_MT = 1;
72
73 extern "C" int
74 __mingwthr_key_dtor (DWORD, void (*) (void *))
75 {
76   // FIXME: for now we do nothing; this causes a memory leak of
77   //        approximately 24 bytes per thread created.
78   return 0;
79 }
80
81 // Set platform-specific System properties.
82 void
83 _Jv_platform_initProperties (java::util::Properties* newprops)
84 {
85   // A convenience define.
86 #define SET(Prop,Val) \
87   newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
88
89   SET ("file.separator", "\\");
90   SET ("path.separator", ";");
91   SET ("line.separator", "\r\n");
92
93   // Use GetCurrentDirectory to set 'user.dir'.
94   DWORD buflen = MAX_PATH;
95   char *buffer = (char *) _Jv_MallocUnchecked (buflen);
96   if (buffer != NULL)
97     {
98       if (GetCurrentDirectory (buflen, buffer))
99         SET ("user.dir", buffer);
100
101       if (GetTempPath (buflen, buffer))
102         SET ("java.io.tmpdir", buffer);
103
104       _Jv_Free (buffer);
105     }
106   
107   // Use GetUserName to set 'user.name'.
108   buflen = 257;  // UNLEN + 1
109   buffer = (char *) _Jv_MallocUnchecked (buflen);
110   if (buffer != NULL)
111     {
112       if (GetUserName (buffer, &buflen))
113         SET ("user.name", buffer);
114       _Jv_Free (buffer);
115     }
116
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 )
124     {
125       // Check HOME since it's what I use.
126       if ((userHome = ::getenv ("HOME")) == NULL )
127         {
128           // Not found - use the windows directory like JDK1.3.1 does.
129           char *winHome = (char *) _Jv_MallocUnchecked (MAX_PATH);
130           if (winHome != NULL)
131             {
132               if (GetWindowsDirectory (winHome, MAX_PATH))
133                 SET ("user.home", winHome);
134               _Jv_Free (winHome);
135             }
136         }
137      }
138   if (userHome != NULL)
139     SET ("user.home", userHome);
140
141   // Get and set some OS info.
142   OSVERSIONINFO osvi;
143   ZeroMemory (&osvi, sizeof(OSVERSIONINFO));
144   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
145   if (GetVersionEx (&osvi))
146     {
147       char *buffer = (char *) _Jv_MallocUnchecked (30);
148       if (buffer != NULL)
149         {
150           sprintf (buffer, "%d.%d", (int) osvi.dwMajorVersion,
151                    (int) osvi.dwMinorVersion);
152           SET ("os.version", buffer);
153           _Jv_Free (buffer);
154         }
155
156       switch (osvi.dwPlatformId)
157         {
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;
168
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;
179
180           default:
181             SET ("os.name", "Windows UNKNOWN");
182             break;
183        }
184   }
185
186   // Set the OS architecture.
187   SYSTEM_INFO si;
188   GetSystemInfo (&si);
189   switch (si.dwProcessorType)
190     {
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;
209     }
210 }
211
212 /* Store up to SIZE return address of the current program state in
213    ARRAY and return the exact number of values stored.  */
214 int
215 backtrace (void **__array, int __size)
216 {
217   register void *_ebp __asm__ ("ebp");
218   register void *_esp __asm__ ("esp");
219   unsigned int *rfp;
220
221   int i=0;
222   for (rfp = *(unsigned int**)_ebp;
223        rfp && i < __size;
224        rfp = *(unsigned int **)rfp)
225     {
226       int diff = *rfp - (unsigned int)rfp;
227       if ((void*)rfp < _esp || diff > 4 * 1024 || diff < 0) break;
228
229     __array[i++] = (void*)(rfp[1]-4);
230   }
231   return i;
232 }