fixed function to always return a string copy (and never a string constant)
[rocksndiamonds.git] / src / libgame / setup.c
index c64c95c8ee84c6d5fdb17d6b110eaded193a01a7..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);
+
+    config_filename_1 = getStringCat2(command_filename_1, ".conf");
+    config_filename_2 = getStringCat2(command_filename_2, ".conf");
+    config_filename_3 = getPath2(conf_directory, SETUP_FILENAME);
+
+    checked_free(ro_base_path);
+    checked_free(conf_directory);
 
-  char *ro_base_path = getProgramMainDataPath(command_filename, RO_BASE_PATH);
-  char *conf_directory = getPath2(ro_base_path, CONF_DIRECTORY);
+    checked_free(command_basepath);
+    checked_free(command_basename);
 
-  char *command_basepath = getBasePath(command_filename);
-  char *command_basename = getBaseNameNoSuffix(command_filename);
-  char *command_filename_2 = getPath2(command_basepath, command_basename);
+    checked_free(command_filename_1);
+    checked_free(command_filename_2);
 
-  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);
+    initialized = TRUE;
+  }
 
   // 1st try: look for config file that exactly matches the binary filename
   if (fileExists(config_filename_1))
@@ -3967,8 +3986,17 @@ TreeInfo *getArtworkTreeInfoForUserLevelSet(int type)
 {
   char *artwork_set = getArtworkIdentifierForUserLevelSet(type);
   TreeInfo *artwork_first_node = ARTWORK_FIRST_NODE(artwork, type);
+  TreeInfo *ti = getTreeInfoFromIdentifier(artwork_first_node, artwork_set);
+
+  if (ti == NULL)
+  {
+    ti = getTreeInfoFromIdentifier(artwork_first_node,
+                                  ARTWORK_DEFAULT_SUBDIR(type));
+    if (ti == NULL)
+      Error(ERR_EXIT, "cannot find default graphics -- should not happen");
+  }
 
-  return getTreeInfoFromIdentifier(artwork_first_node, artwork_set);
+  return ti;
 }
 
 boolean checkIfCustomArtworkExistsForCurrentLevelSet(void)