fixed function to always return a string copy (and never a string constant)
[rocksndiamonds.git] / src / libgame / setup.c
index 22fd408408e767508e4e336cd0cdf39781adb864..5cdaa87471a3c55f3b4dd5a5b18e94494fd5b27d 100644 (file)
@@ -457,7 +457,7 @@ char *getProgramMainDataPath(char *command_filename, char *base_path)
 {
   // check if the program's main data base directory is configured
   if (!strEqual(base_path, "."))
-    return base_path;
+    return getStringCopy(base_path);
 
   /* if the program is configured to start from current directory (default),
      determine program package directory from program binary (some versions
@@ -494,22 +494,41 @@ char *getProgramMainDataPath(char *command_filename, char *base_path)
 
 char *getProgramConfigFilename(char *command_filename)
 {
-  char *command_filename_1 = getStringCopy(command_filename);
+  static char *config_filename_1 = NULL;
+  static char *config_filename_2 = NULL;
+  static char *config_filename_3 = NULL;
+  static boolean initialized = FALSE;
 
-  // strip trailing executable suffix from command filename
-  if (strSuffix(command_filename_1, ".exe"))
-    command_filename_1[strlen(command_filename_1) - 4] = '\0';
+  if (!initialized)
+  {
+    char *command_filename_1 = getStringCopy(command_filename);
+
+    // strip trailing executable suffix from command filename
+    if (strSuffix(command_filename_1, ".exe"))
+      command_filename_1[strlen(command_filename_1) - 4] = '\0';
+
+    char *ro_base_path = getProgramMainDataPath(command_filename, RO_BASE_PATH);
+    char *conf_directory = getPath2(ro_base_path, CONF_DIRECTORY);
+
+    char *command_basepath = getBasePath(command_filename);
+    char *command_basename = getBaseNameNoSuffix(command_filename);
+    char *command_filename_2 = getPath2(command_basepath, command_basename);
 
-  char *ro_base_path = getProgramMainDataPath(command_filename, RO_BASE_PATH);
-  char *conf_directory = getPath2(ro_base_path, CONF_DIRECTORY);
+    config_filename_1 = getStringCat2(command_filename_1, ".conf");
+    config_filename_2 = getStringCat2(command_filename_2, ".conf");
+    config_filename_3 = getPath2(conf_directory, SETUP_FILENAME);
 
-  char *command_basepath = getBasePath(command_filename);
-  char *command_basename = getBaseNameNoSuffix(command_filename);
-  char *command_filename_2 = getPath2(command_basepath, command_basename);
+    checked_free(ro_base_path);
+    checked_free(conf_directory);
 
-  char *config_filename_1 = getStringCat2(command_filename_1, ".conf");
-  char *config_filename_2 = getStringCat2(command_filename_2, ".conf");
-  char *config_filename_3 = getPath2(conf_directory, SETUP_FILENAME);
+    checked_free(command_basepath);
+    checked_free(command_basename);
+
+    checked_free(command_filename_1);
+    checked_free(command_filename_2);
+
+    initialized = TRUE;
+  }
 
   // 1st try: look for config file that exactly matches the binary filename
   if (fileExists(config_filename_1))