Plug memory leak, filenames relative to installDir
[xboard.git] / winboard / woptions.c
index 801fe04..9aa820f 100644 (file)
@@ -135,6 +135,28 @@ VOID SetLoadOptionEnables(HWND hDlg);
 VOID SetSaveOptionEnables(HWND hDlg);\r
 VOID SetTimeControlEnables(HWND hDlg);\r
 \r
+char *\r
+InterpretFileName(char *buf, char *homeDir)\r
+{ // [HGM] file name relative to homeDir. (Taken out of SafeOptionsDialog, because it is generally useful)\r
+  char *result = NULL;\r
+  if ((isalpha(buf[0]) && buf[1] == ':') ||\r
+    (buf[0] == '\\' && buf[1] == '\\')) {\r
+    return strdup(buf);\r
+  } else {\r
+    char buf2[MSG_SIZ], buf3[MSG_SIZ];\r
+    char *dummy;\r
+    GetCurrentDirectory(MSG_SIZ, buf3);\r
+    SetCurrentDirectory(homeDir);\r
+    if (GetFullPathName(buf, MSG_SIZ, buf2, &dummy)) {\r
+      result = strdup(buf2);\r
+    } else {\r
+      result = strdup(buf);\r
+    }\r
+    SetCurrentDirectory(buf3);\r
+  }\r
+  return result;\r
+}\r
+\r
 /*---------------------------------------------------------------------------*\\r
  *\r
  * General Options Dialog functions\r
@@ -2494,7 +2516,7 @@ SaveOptionsDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
       if (IsDlgButtonChecked(hDlg, OPT_Autosave)) {\r
        appData.autoSaveGames = TRUE;\r
        if (IsDlgButtonChecked(hDlg, OPT_AVPrompt)) {\r
-         appData.saveGameFile = "";\r
+         ASSIGN(appData.saveGameFile, ""); // [HGM] make sure value is ALWAYS in allocated memory\r
        } else /*if (IsDlgButtonChecked(hDlg, OPT_AVToFile))*/ {\r
          GetDlgItemText(hDlg, OPT_AVFilename, buf, MSG_SIZ);\r
          if (*buf == NULLCHAR) {\r
@@ -2502,25 +2524,12 @@ SaveOptionsDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
                       _("Option Error"), MB_OK|MB_ICONEXCLAMATION);\r
            return FALSE;\r
          }\r
-         if ((isalpha(buf[0]) && buf[1] == ':') ||\r
-           (buf[0] == '\\' && buf[1] == '\\')) {\r
-           appData.saveGameFile = strdup(buf);\r
-         } else {\r
-           char buf2[MSG_SIZ], buf3[MSG_SIZ];\r
-           char *dummy;\r
-           GetCurrentDirectory(MSG_SIZ, buf3);\r
-           SetCurrentDirectory(installDir);\r
-           if (GetFullPathName(buf, MSG_SIZ, buf2, &dummy)) {\r
-             appData.saveGameFile = strdup(buf2);\r
-           } else {\r
-             appData.saveGameFile = strdup(buf);\r
-           }\r
-           SetCurrentDirectory(buf3);\r
-         }\r
+         FREE(appData.saveGameFile);\r
+         appData.saveGameFile = InterpretFileName(buf, homeDir);\r
        }\r
       } else {\r
        appData.autoSaveGames = FALSE;\r
-       appData.saveGameFile = "";\r
+       ASSIGN(appData.saveGameFile, "");\r
       }\r
       appData.oldSaveStyle = IsDlgButtonChecked(hDlg, OPT_Old);\r
       appData.saveOutOfBookInfo = IsDlgButtonChecked( hDlg, OPT_OutOfBookInfo );\r