1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
From: Norbert Buchmuller <norbi@nix.hu>
Date: Sun, 8 Oct 2006 10:53:48 +0200
Subject: honor TMPDIR
Bug-Debian: http://bugs.debian.org/259386
---
src/edit.c | 47 +++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/src/edit.c b/src/edit.c
index 59d196d..050ed0a 100644
--- a/src/edit.c
+++ b/src/edit.c
@@ -24,6 +24,7 @@
#include <stdio.h>
#include <string.h>
+#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
@@ -43,6 +44,48 @@
#include "edit.h"
#include "format.h"
+int
+is_directory(const char *entity)
+{
+ struct stat stat_buf;
+
+ if (stat(entity, &stat_buf))
+ return 0;
+
+ return S_ISDIR(stat_buf.st_mode);
+}
+
+FILE *
+create_tmpfile(void)
+{
+ char *tmpdir;
+ size_t template_len;
+ char *template;
+ const char *filename_template = "sweepXXXXXX";
+ int fd;
+
+ tmpdir = getenv("TMPDIR");
+ if (!tmpdir || !is_directory(tmpdir)) {
+ tmpdir = "/tmp";
+ }
+
+ template_len = strlen(tmpdir) + strlen("/") + strlen(filename_template) + 1;
+ template = (char *) malloc(template_len);
+ if (!template)
+ return NULL;
+ snprintf(template, template_len, "%s/%s", tmpdir, filename_template);
+
+ fd = mkstemp(template);
+
+ unlink(template);
+
+ free(template);
+
+ if (fd < 0)
+ return NULL;
+
+ return fdopen(fd, "w+");
+}
sw_edit_buffer * ebuf = NULL;
@@ -55,8 +98,8 @@ sweep_large_alloc_data (size_t len, void * data, int prot)
FILE * f;
int fd;
- if ((f = tmpfile ()) == NULL) {
- perror ("tmpfile failed in sweep_large_alloc_data");
+ if ((f = create_tmpfile ()) == NULL) {
+ perror ("create_tmpfile failed in sweep_large_alloc_data");
return NULL;
}
|