summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2024-03-21 22:50:08 +0000
committermidipix <writeonce@midipix.org>2024-03-21 22:50:08 +0000
commit1b374915214356cdc76549012e48f7ec0def049c (patch)
treebda32e2573d4097c999a4aa03a78e07128ecb3db
parent91c40fd41b0cdca289a738d8bcf4e246bed18d0f (diff)
downloadtpax-1b374915214356cdc76549012e48f7ec0def049c.tar.bz2
tpax-1b374915214356cdc76549012e48f7ec0def049c.tar.xz
tpax_tmpfile(): refactor code and template, eliminate use of rand_r().
-rw-r--r--src/internal/tpax_tmpfile_impl.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/src/internal/tpax_tmpfile_impl.c b/src/internal/tpax_tmpfile_impl.c
index 56d5c92..9dd9d40 100644
--- a/src/internal/tpax_tmpfile_impl.c
+++ b/src/internal/tpax_tmpfile_impl.c
@@ -5,12 +5,16 @@
/**************************************************************/
#define _GNU_SOURCE
+#include <time.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
+#include <inttypes.h>
+
+#define PPRIX64 "%"PRIx64
/* mkostemp might be guarded by non-standard macros */
/* unless HAVE_NO_MKOSTEMP, assume it is available */
@@ -54,8 +58,8 @@ static int tpax_mkostemp(char * tmplate)
int tpax_tmpfile(void)
{
int fd;
- unsigned seed;
- char tmplate[64];
+ void * addr;
+ char tmplate[128];
/* try with __fs_tmpfile() */
if ((fd = tpax_tmpfile_by_framework()) >= 0)
@@ -66,10 +70,18 @@ int tpax_tmpfile(void)
return fd;
/* fallback to mk{o}stemp */
- seed = getpid();
+ addr = tmplate;
memset(tmplate,0,sizeof(tmplate));
- snprintf(tmplate,sizeof(tmplate),"/tmp/tpax_%d_%d_%d_XXXXXXXXXXXX",
- getppid(),getpid(),rand_r(&seed));
+ snprintf(tmplate,sizeof(tmplate),
+ "/tmp/"
+ ".tpax.tmpfile"
+ ".time."PPRIX64
+ ".salt.%p"
+ ".pid.%d"
+ ".XXXXXXXXXXXX",
+ time(0),
+ addr,
+ getpid());
return tpax_mkostemp(tmplate);
}