@@ -145,24 +145,24 @@ namespace platf {
145145 std::call_once (migration_flag, []() {
146146 bool found = false ;
147147 bool migrate_config = true ;
148- const char * dir;
149- const char * homedir;
150- const char * migrate_envvar;
148+ std::string dir;
149+ std::string homedir;
150+ std::string migrate_envvar;
151151
152152 // Get the home directory
153- if ((homedir = getenv ( " HOME" )) == nullptr || strlen ( homedir) == 0 ) {
153+ if (! get_env ( " HOME" , homedir) || homedir. empty () ) {
154154 // If HOME is empty or not set, use the current user's home directory
155155 homedir = getpwuid (geteuid ())->pw_dir ;
156156 }
157157
158158 // May be set if running under a systemd service with the ConfigurationDirectory= option set.
159- if ((dir = getenv ( " CONFIGURATION_DIRECTORY" )) != nullptr && strlen ( dir) > 0 ) {
159+ if (get_env ( " CONFIGURATION_DIRECTORY" , dir) && ! dir. empty () ) {
160160 found = true ;
161161 config_path = fs::path (dir) / " sunshine" sv;
162162 }
163163 // Otherwise, follow the XDG base directory specification:
164164 // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
165- if (!found && (dir = getenv ( " XDG_CONFIG_HOME" )) != nullptr && strlen ( dir) > 0 ) {
165+ if (!found && get_env ( " XDG_CONFIG_HOME" , dir) && ! dir. empty () ) {
166166 found = true ;
167167 config_path = fs::path (dir) / " sunshine" sv;
168168 }
@@ -173,8 +173,7 @@ namespace platf {
173173 }
174174
175175 // migrate from the old config location if necessary
176- migrate_envvar = getenv (" SUNSHINE_MIGRATE_CONFIG" );
177- if (migrate_config && found && migrate_envvar && strcmp (migrate_envvar, " 1" ) == 0 ) {
176+ if (migrate_config && found && get_env (" SUNSHINE_MIGRATE_CONFIG" , migrate_envvar) && (migrate_envvar == " 1" )) {
178177 std::error_code ec;
179178 fs::path old_config_path = fs::path (homedir) / " .config/sunshine" sv;
180179 if (old_config_path != config_path && fs::exists (old_config_path, ec)) {
@@ -322,7 +321,9 @@ namespace platf {
322321 */
323322 void open_url (const std::string &url) {
324323 // set working dir to user home directory
325- auto working_dir = boost::filesystem::path (std::getenv (" HOME" ));
324+ std::string homedir;
325+ get_env (" HOME" , homedir);
326+ auto working_dir = boost::filesystem::path (homedir);
326327 std::string cmd = R"( xdg-open ")" + url + R"( ")" ;
327328
328329 boost::process::v1::environment _env = boost::this_process::environment ();
@@ -445,24 +446,19 @@ namespace platf {
445446 lifetime::exit_sunshine (0 , true );
446447 }
447448
448- std::string get_env (const std::string &name) {
449- if (const auto value = getenv (name.c_str ()); value != nullptr ) {
450- return value;
449+ bool get_env (const std::string &name, std::string &value) {
450+ const auto value_ = getenv (name.c_str ()); // NOSONAR(cpp:S1874) - _dupenv_s is only available on WIN32
451+ if (value_ == nullptr ) {
452+ return false ;
451453 }
452- return " " ;
454+ value = std::string (value_);
455+ return true ;
453456 }
454457
455458 int set_env (const std::string &name, const std::string &value) {
456459 return setenv (name.c_str (), value.c_str (), 1 );
457460 }
458461
459- int append_env (const std::string &name, const std::string &value, const std::string &separator) {
460- if (const std::string old_value = get_env (name); !old_value.contains (value)) {
461- return set_env (name, old_value.empty () ? value : old_value + separator + value);
462- }
463- return 0 ;
464- }
465-
466462 int unset_env (const std::string &name) {
467463 return unsetenv (name.c_str ());
468464 }
@@ -1152,13 +1148,13 @@ namespace platf {
11521148
11531149 window_system = window_system_e::NONE ;
11541150#ifdef SUNSHINE_BUILD_WAYLAND
1155- if (std::getenv (" WAYLAND_DISPLAY" )) {
1151+ if (std::string v; get_env (" WAYLAND_DISPLAY" , v )) {
11561152 window_system = window_system_e::WAYLAND ;
11571153 }
11581154#endif
11591155#if defined(SUNSHINE_BUILD_X11) || defined(SUNSHINE_BUILD_CUDA)
1160- if (std::getenv (" DISPLAY" ) && window_system != window_system_e::WAYLAND ) {
1161- if (std::getenv (" WAYLAND_DISPLAY" )) {
1156+ if (std::string v; get_env (" DISPLAY" , v ) && window_system != window_system_e::WAYLAND ) {
1157+ if (get_env (" WAYLAND_DISPLAY" , v )) {
11621158 BOOST_LOG (warning) << " Wayland detected, yet sunshine will use X11 for screencasting, screencasting will only work on XWayland applications" sv;
11631159 }
11641160
0 commit comments