shithub: choc

Download patch

ref: cb7cf979369b5b3b4db0154368e379ae8ab8aa25
parent: a4fec80d22871128288632ec47e21f5f4b99f27b
author: Simon Howard <[email protected]>
date: Fri Jan 5 19:34:50 EST 2007

Choose the locations for temporary files more intelligently.

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 821

--- a/setup/execute.c
+++ b/setup/execute.c
@@ -47,6 +47,14 @@
 #define DOOM_BINARY INSTALL_DIR "/chocolate-doom"
 #endif
 
+#ifdef _WIN32
+#define DIR_SEPARATOR '\\'
+#define PATH_SEPARATOR ';'
+#else
+#define DIR_SEPARATOR '/'
+#define PATH_SEPARATOR ':'
+#endif
+
 struct execute_context_s
 {
     char *response_file;
@@ -53,18 +61,43 @@
     FILE *stream;
 };
 
-execute_context_t *NewExecuteContext(void)
+// Returns the path to a temporary file of the given name, stored
+// inside the system temporary directory.
+
+static char *TempFile(char *s)
 {
-    execute_context_t *result;
+    char *result;
+    char *tempdir;
 
-    result = malloc(sizeof(execute_context_t));
-    
 #ifdef _WIN32
-    result->response_file = "chocolat.rsp";
+
+    // Check the TEMP environment variable to find the location.
+
+    temp = getenv("TEMP");
+
+    if (temp == NULL)
+    {
+        tempdir = ".";
+    }
 #else
-    result->response_file = "/tmp/chocolate.rsp";
+    // In Unix, just use /tmp.
+
+    tempdir = "/tmp";
 #endif
 
+    result = malloc(strlen(tempdir) + strlen(s) + 2);
+    sprintf(result, "%s%c%s", tempdir, DIR_SEPARATOR, s);
+
+    return result;
+}
+
+execute_context_t *NewExecuteContext(void)
+{
+    execute_context_t *result;
+
+    result = malloc(sizeof(execute_context_t));
+    
+    result->response_file = TempFile("chocolat.rsp");
     result->stream = fopen(result->response_file, "w");
 
     if (result->stream == NULL)
@@ -126,6 +159,7 @@
     
     // Destroy context 
     remove(context->response_file);
+    free(context->response_file);
     free(context);
 
     if (WIFEXITED(result))
@@ -156,13 +190,8 @@
 
     // Save temporary configuration files with the current configuration
 
-#ifdef _WIN32
-    main_cfg = "tmp.cfg";
-    extra_cfg = "extratmp.cfg";
-#else
-    main_cfg = "/tmp/tmp.cfg";
-    extra_cfg = "/tmp/extratmp.cfg";
-#endif
+    main_cfg = TempFile("tmp.cfg");
+    extra_cfg = TempFile("extratmp.cfg");
 
     M_SaveMainDefaults(main_cfg);
     M_SaveExtraDefaults(extra_cfg);
@@ -181,6 +210,8 @@
 
     remove(main_cfg);
     remove(extra_cfg);
+    free(main_cfg);
+    free(extra_cfg);
 }
 
 txt_window_action_t *TestConfigAction(void)
--- a/src/i_sound.c
+++ b/src/i_sound.c
@@ -677,7 +677,7 @@
 
 void *I_RegisterSong(void *data, int len)
 {
-    char filename[64];
+    char *filename;
     Mix_Music *music;
 
     if (!music_initialised)
@@ -686,11 +686,7 @@
     // MUS files begin with "MUS"
     // Reject anything which doesnt have this signature
     
-#ifdef _WIN32
-    sprintf(filename, "doom.mid");
-#else
-    sprintf(filename, "/tmp/doom-%i.mid", getpid());
-#endif
+    filename = M_TempFile("doom.mid");
 
     if (IsMid(data, len) && len < MAXMIDLENGTH)
     {
@@ -717,6 +713,8 @@
     // remove file now
 
     remove(filename);
+
+    Z_Free(filename);
 
     return music;
 }
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -198,6 +198,37 @@
     return length;
 }
 
+// Returns the path to a temporary file of the given name, stored
+// inside the system temporary directory.
+//
+// The returned value must be freed with Z_Free after use.
+
+char *M_TempFile(char *s)
+{
+    char *result;
+    char *tempdir;
+
+#ifdef _WIN32
+
+    // Check the TEMP environment variable to find the location.
+
+    temp = getenv("TEMP");
+
+    if (temp == NULL)
+    {
+        tempdir = ".";
+    }
+#else
+    // In Unix, just use /tmp.
+
+    tempdir = "/tmp";
+#endif
+
+    result = Z_Malloc(strlen(tempdir) + strlen(s) + 2, PU_STATIC, 0);
+    sprintf(result, "%s%c%s", tempdir, DIR_SEPARATOR, s);
+
+    return result;
+}
 
 //
 // DEFAULTS
--- a/src/m_misc.h
+++ b/src/m_misc.h
@@ -57,6 +57,8 @@
 
 void M_MakeDirectory(char *dir);
 
+char *M_TempFile(char *s);
+
 boolean M_FileExists(char *file);