From bb2d1f55e9912d3b9b81b6d5e12900f77eef4365 Mon Sep 17 00:00:00 2001 From: Torsten Dreyer Date: Fri, 6 Aug 2010 09:06:32 +0200 Subject: [PATCH] Interpret environment variables and hostname on Windows - 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 | 14 ++++++++++++++ src/Main/fg_init.cxx | 35 ++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/Main/bootstrap.cxx b/src/Main/bootstrap.cxx index 877712d35..8a360206e 100644 --- a/src/Main/bootstrap.cxx +++ b/src/Main/bootstrap.cxx @@ -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 diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index ad8640c96..2db7f32e2 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -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; } -- 2.39.5