diff --git a/src/qt/test/test_main.cpp b/src/qt/test/test_main.cpp --- a/src/qt/test/test_main.cpp +++ b/src/qt/test/test_main.cpp @@ -63,7 +63,7 @@ // Prefer the "minimal" platform for the test instead of the normal default // platform ("xcb", "windows", or "cocoa") so tests can't unintentially // interfere with any background GUIs and don't require extra resources. - setenv("QT_QPA_PLATFORM", "minimal", 0); + SetEnvironmentalVariable("QT_QPA_PLATFORM", "minimal"); // Don't remove this, it's needed to access // QApplication:: and QCoreApplication:: in the tests diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -713,4 +713,9 @@ 0x15, 0x0f, 0x06, 0x1e, 0x1e}); } +BOOST_AUTO_TEST_CASE(util_EnvironmentalVariables) { + BOOST_CHECK_EQUAL(SetEnvironmentalVariable("BOOSTTEST", "TestValue"), 0); + BOOST_CHECK_EQUAL(GetEnvironmentalVariable("BOOSTTEST"), "TestValue"); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/src/util.h b/src/util.h --- a/src/util.h +++ b/src/util.h @@ -219,6 +219,14 @@ } } +/** + * Platform independent function to set environmental variable. + */ +int SetEnvironmentalVariable(const char *variableName, + const char *variableValue); + +const char *GetEnvironmentalVariable(const char *variableName); + std::string CopyrightHolders(const std::string &strPrefix); #endif // BITCOIN_UTIL_H diff --git a/src/util.cpp b/src/util.cpp --- a/src/util.cpp +++ b/src/util.cpp @@ -645,6 +645,25 @@ strprintf(_(COPYRIGHT_HOLDERS), _(COPYRIGHT_HOLDERS_SUBSTITUTION)); } +/** + * Platform independent function to set environmental variable. + */ +int SetEnvironmentalVariable(const char *variableName, + const char *variableValue) { +#if defined(WIN32) + return _putenv_s(variableName, variableValue); +#else + return setenv(variableName, variableValue, 0); +#endif +} + +/** + * Platform independent function to get environmental variable. + */ +const char *GetEnvironmentalVariable(const char *variableName) { + return getenv(variableName); +} + // Obtain the application startup time (used for uptime calculation) int64_t GetStartupTime() { return nStartupTime;