1 Index: b/lib/routines.c
2 ===================================================================
6 /* Don't complain if you can't unlink. Who cares of a tmp file? */
11 + * Securely generate a temp file, and make sure it gets
12 + * deleted upon exit.
14 +static char ** tempfiles;
15 +static unsigned ntempfiles;
20 + while (ntempfiles--)
21 + unlink(tempfiles[ntempfiles]);
25 +safe_tempnam(const char *pfx)
27 + char *dirname, *filename;
30 + if (!(dirname = getenv("TMPDIR")))
33 + tempfiles = (char **) realloc(tempfiles,
34 + (ntempfiles+1) * sizeof(char *));
35 + if (tempfiles == NULL)
38 + filename = malloc(strlen(dirname) + strlen(pfx) + sizeof("/XXXXXX"));
42 + sprintf(filename, "%s/%sXXXXXX", dirname, pfx);
44 + if ((fd = mkstemp(filename)) < 0) {
50 + if (ntempfiles == 0)
51 + atexit(cleanup_tempfiles);
52 + tempfiles[ntempfiles++] = filename;
56 Index: b/lib/routines.h
57 ===================================================================
61 /* If _STR_ is not defined, give it a tempname in _TMPDIR_ */
62 #define tempname_ensure(Str) \
64 - (Str) = (Str) ? (Str) : tempnam (NULL, "a2_"); \
65 + (Str) = (Str) ? (Str) : safe_tempnam("a2_"); \
67 +char * safe_tempnam(const char *);