]> git.mxchange.org Git - flightgear.git/commitdiff
Interpret environment variables and hostname on Windows
authorTorsten Dreyer <Torsten@t3r.de>
Fri, 6 Aug 2010 07:06:32 +0000 (09:06 +0200)
committerTorsten Dreyer <Torsten@t3r.de>
Fri, 6 Aug 2010 07:06:32 +0000 (09:06 +0200)
- Point the homedir variable to %APPDATA%/flightgear.org. This
enables .fgfsrc in this path for windows users.
- Evaluate the returncode of gethostname() to not strdup() bogus
characters on failure. Also mainly on windows.

src/Main/bootstrap.cxx
src/Main/fg_init.cxx

index 877712d359f920f2ef0dd2c3338796ba6af2cc7e..8a360206e533ad8d67bc298b94a6c44705bb5ebc 100644 (file)
@@ -57,8 +57,14 @@ using std::endl;
 
 #include "fg_os.hxx"
 
+#ifdef _MSC_VER
+char homepath[256] = "";
+char * homedir = homepath;
+char *hostname = ::getenv( "COMPUTERNAME" );
+#else
 char *homedir = ::getenv( "HOME" );
 char *hostname = ::getenv( "HOSTNAME" );
+#endif
 bool free_hostname = false;
 
 // foreward declaration.
@@ -163,6 +169,14 @@ int _bootstrap_OSInit;
 
 // Main entry point; catch any exceptions that have made it this far.
 int main ( int argc, char **argv ) {
+#if _MSC_VER
+  // Windows has no $HOME aka %HOME%, so we have to construct the full path.
+  // make sure it fits into the buffer. Max. path length is 255, but who knows
+  // what's in these environment variables?
+  homepath[sizeof(homepath)-1] = 0;
+  strncpy( homepath, ::getenv("APPDATA"), sizeof(homepath)-1 );
+  strncat( homepath, "\\flightgear.org", sizeof(homepath)-strlen(homepath)-1 );
+#endif
 
 #ifdef PTW32_STATIC_LIB
     // Initialise static pthread win32 lib
index ad8640c966a435af28d16662ab546fb75dc3fd05..2db7f32e298ba291c8b6c34c250ba833c60b1915 100644 (file)
@@ -141,9 +141,10 @@ static string fgScanForOption( const string& option, int argc, char **argv ) {
     if (hostname == NULL)
     {
         char _hostname[256];
-        gethostname(_hostname, 256);
-        hostname = strdup(_hostname);
-        free_hostname = true;
+        if( gethostname(_hostname, 256) >= 0 ) {
+            hostname = strdup(_hostname);
+            free_hostname = true;
+        }
     }
 
     SG_LOG(SG_GENERAL, SG_INFO, "Scanning command line for: " << option );
@@ -202,10 +203,10 @@ static string fgScanForOption( const string& option, const string& path ) {
 static string fgScanForOption( const string& option ) {
     string arg("");
 
-#if defined( unix ) || defined( __CYGWIN__ )
+#if defined( unix ) || defined( __CYGWIN__ ) || defined(_MSC_VER)
     // Next check home directory for .fgfsrc.hostname file
     if ( arg.empty() ) {
-        if ( homedir != NULL ) {
+        if ( homedir != NULL && hostname != NULL && strlen(hostname) > 0) {
             SGPath config( homedir );
             config.append( ".fgfsrc" );
             config.concat( "." );
@@ -488,10 +489,12 @@ do_options (int argc, char ** argv)
     config.append( "system.fgfsrc" );
     fgParseOptions(config.str());
 
-#if defined( unix ) || defined( __CYGWIN__ )
-    config.concat( "." );
-    config.concat( hostname );
-    fgParseOptions(config.str());
+#if defined( unix ) || defined( __CYGWIN__ ) || defined(_MSC_VER)
+    if( hostname != NULL && strlen(hostname) > 0 ) {
+        config.concat( "." );
+        config.concat( hostname );
+        fgParseOptions(config.str());
+    }
 #endif
 
     // Check for ~/.fgfsrc
@@ -501,11 +504,13 @@ do_options (int argc, char ** argv)
         fgParseOptions(config.str());
     }
 
-#if defined( unix ) || defined( __CYGWIN__ )
-    // Check for ~/.fgfsrc.hostname
-    config.concat( "." );
-    config.concat( hostname );
-    fgParseOptions(config.str());
+#if defined( unix ) || defined( __CYGWIN__ ) || defined(_MSC_VER)
+    if( hostname != NULL && strlen(hostname) > 0 ) {
+        // Check for ~/.fgfsrc.hostname
+        config.concat( "." );
+        config.concat( hostname );
+        fgParseOptions(config.str());
+    }
 #endif
 
     // Parse remaining command line options
@@ -1623,7 +1628,7 @@ private:
     SGPropertyNode root;
     try {
        readProperties(path.str(), &root);
-    } catch (sg_exception& e) {
+    } catch (sg_exception& ) {
        return false;
     }