[PATCH]: adds missing System properties for gcj under mingw

aking@dreammechanics.com aking@dreammechanics.com
Fri Apr 5 21:47:00 GMT 2002


The attached patch sets six System properties ( os.name, os.arch, 
os.version, user.name, user.home, user.dir ) that are currently
null under the mingw32 port.  I have several java programs that
crash since they assume that these properties are valid because the 
JDK docs specifies that these properties always have a value. I'm
hoping that this patch can make it into the 3.1 branch :)

I wrote a small java program that prints out the values.  Here is
the output before the patch:

os.name: null
os.arch: null
os.version: null
user.name: null
user.home: null
user.dir: null

After the patch on a WinME machine:

os.name: Windows Me
os.arch: i586
os.version: 4.90
user.name: aking
user.home: C:\home\aking
user.dir: T:\

And on a Win2k system (under vmware):

os.name: Windows 2000
os.arch: i586
os.version: 5.0
user.name: aking
user.home: \
user.dir: C:\WINNT

2002-04-05  Adam King <aking@dreammechanics.com>

    * java/lang/natSystem.cc (init_properties): Add Win32 support for
      the System properties os.name, os.arch, os.version, user.name, 
      user.home, and user.dir

Thanks!
      
    Adam

Index: natSystem.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/java/lang/natSystem.cc,v
retrieving revision 1.48.2.3
diff -u -r1.48.2.3 natSystem.cc
--- natSystem.cc	18 Mar 2002 06:30:24 -0000	1.48.2.3
+++ natSystem.cc	6 Apr 2002 04:46:40 -0000
@@ -423,6 +423,121 @@
 #endif /* HAVE_UNISTD_H */
 #endif /* HAVE_GETCWD */
 
+#ifdef WIN32
+  // Use GetCurrentDirectory to set 'user.dir'
+  DWORD buflen = MAX_PATH;
+  char* buffer = (char *) malloc (buflen);
+  if (buffer != NULL)
+    {
+      if (GetCurrentDirectory (buflen, buffer))
+          SET ("user.dir", buffer);
+      free (buffer);
+    }
+  
+  // Use GetUserName to set 'user.name'
+  buflen = 257;  // UNLEN + 1
+  buffer = (char *) malloc (buflen);
+  if (buffer != NULL)
+    {
+      if (GetUserName (buffer, &buflen))
+        SET ("user.name", buffer);
+      free (buffer);
+    }
+
+  // According to the api documentation for 'GetWindowsDirectory()', the 
+  // environmental variable HOMEPATH always specifies the user's home 
+  // directory or a default directory.  On the 3 windows machines I checked
+  // only 1 had it set.  If it's not set, JDK1.3.1 seems to set it to
+  // the windows directory, so we'll do the same.
+  char* userHome = NULL;
+  if ((userHome = ::getenv( "HOMEPATH" )) == NULL )
+    {
+      // check HOME since it's what I use
+      if ((userHome = ::getenv( "HOME" )) == NULL )
+        {
+          // not found - use the windows directory like JDK1.3.1 does
+          char* winHome = (char *)malloc (MAX_PATH);
+          if ( winHome != NULL )
+            {
+              if (GetWindowsDirectory (winHome, MAX_PATH))
+                  SET ("user.home", winHome);
+              free (winHome);
+            }
+        }
+     }
+  if( userHome != NULL )
+    SET ("user.home", userHome);
+
+  // get and set some OS info
+  OSVERSIONINFO osvi;
+  ZeroMemory (&osvi, sizeof(OSVERSIONINFO));
+  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+  if (GetVersionEx (&osvi))
+    {
+      char *buffer = (char *) malloc (30);
+      if (buffer != NULL)
+        {
+          sprintf (buffer, "%d.%d", (int)osvi.dwMajorVersion, (int)osvi.dwMinorVersion);
+          SET ("os.version", buffer);
+          free (buffer);
+        }
+
+      switch (osvi.dwPlatformId)
+        {
+          case VER_PLATFORM_WIN32_WINDOWS:
+            if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
+              SET ("os.name", "Windows 95");
+            else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
+              SET ("os.name", "Windows 98");
+            else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
+              SET ("os.name", "Windows Me");
+            else
+              SET ("os.name", "Windows ??"); 
+            break;
+
+          case VER_PLATFORM_WIN32_NT:
+            if (osvi.dwMajorVersion <= 4 )
+              SET ("os.name", "Windows NT");
+            else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
+              SET ("os.name", "Windows 2000");
+            else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
+              SET ("os.name", "Windows XP");
+            else
+              SET ("os.name", "Windows NT ??");
+            break;
+
+          default:
+            SET ("os.name", "Windows UNKNOWN");
+            break;
+       }
+  }
+
+  // set the OS architecture
+  SYSTEM_INFO si;
+  GetSystemInfo (&si);
+  switch( si.dwProcessorType )
+    {
+      case PROCESSOR_INTEL_386:
+        SET ("os.arch", "i386");
+        break;
+      case PROCESSOR_INTEL_486:
+        SET ("os.arch", "i486");
+        break;
+      case PROCESSOR_INTEL_PENTIUM:
+        SET ("os.arch", "i586");
+        break;
+      case PROCESSOR_MIPS_R4000:	
+        SET ("os.arch", "MIPS4000");
+        break;
+      case PROCESSOR_ALPHA_21064:
+        SET ("os.arch", "ALPHA");
+        break;
+      default:
+        SET ("os.arch", "unknown");
+        break;
+    }
+#endif
+
   // Set user locale properties based on setlocale()
 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
   // We let the user choose the locale.  However, since Java differs



More information about the Java-patches mailing list