summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Howard2007-01-06 00:34:50 +0000
committerSimon Howard2007-01-06 00:34:50 +0000
commitcb7cf979369b5b3b4db0154368e379ae8ab8aa25 (patch)
tree60ec72d798bb044af479cf0edcf00ae7f77f3c55
parenta4fec80d22871128288632ec47e21f5f4b99f27b (diff)
downloadchocolate-doom-cb7cf979369b5b3b4db0154368e379ae8ab8aa25.tar.gz
chocolate-doom-cb7cf979369b5b3b4db0154368e379ae8ab8aa25.tar.bz2
chocolate-doom-cb7cf979369b5b3b4db0154368e379ae8ab8aa25.zip
Choose the locations for temporary files more intelligently.
Subversion-branch: /trunk/chocolate-doom Subversion-revision: 821
-rw-r--r--setup/execute.c57
-rw-r--r--src/i_sound.c10
-rw-r--r--src/m_misc.c31
-rw-r--r--src/m_misc.h2
4 files changed, 81 insertions, 19 deletions
diff --git a/setup/execute.c b/setup/execute.c
index 1ebd7c55..6d00f6a0 100644
--- a/setup/execute.c
+++ b/setup/execute.c
@@ -47,24 +47,57 @@
#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;
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 @@ int ExecuteDoom(execute_context_t *context)
// Destroy context
remove(context->response_file);
+ free(context->response_file);
free(context);
if (WIFEXITED(result))
@@ -156,13 +190,8 @@ static void TestCallback(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(data))
// 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 @@ static void TestCallback(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(data))
remove(main_cfg);
remove(extra_cfg);
+ free(main_cfg);
+ free(extra_cfg);
}
txt_window_action_t *TestConfigAction(void)
diff --git a/src/i_sound.c b/src/i_sound.c
index ec3a68ad..ee0ad57b 100644
--- a/src/i_sound.c
+++ b/src/i_sound.c
@@ -677,7 +677,7 @@ static boolean ConvertMus(byte *musdata, int len, char *filename)
void *I_RegisterSong(void *data, int len)
{
- char filename[64];
+ char *filename;
Mix_Music *music;
if (!music_initialised)
@@ -686,11 +686,7 @@ void *I_RegisterSong(void *data, int len)
// 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)
{
@@ -718,6 +714,8 @@ void *I_RegisterSong(void *data, int len)
remove(filename);
+ Z_Free(filename);
+
return music;
}
diff --git a/src/m_misc.c b/src/m_misc.c
index 5b98960a..13ca6768 100644
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -198,6 +198,37 @@ int M_ReadFile(char const *name, byte **buffer)
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
diff --git a/src/m_misc.h b/src/m_misc.h
index 41ba96ae..b3412e9c 100644
--- a/src/m_misc.h
+++ b/src/m_misc.h
@@ -57,6 +57,8 @@ void M_SetConfigDir(void);
void M_MakeDirectory(char *dir);
+char *M_TempFile(char *s);
+
boolean M_FileExists(char *file);