summaryrefslogtreecommitdiff
path: root/src/temp/mktemp.c
diff options
context:
space:
mode:
authorVladimir Azarov <avm@intermediate-node.net>2024-10-01 15:47:05 +0200
committerVladimir Azarov <avm@intermediate-node.net>2024-10-01 15:47:05 +0200
commit4abab5ad6c8465a7528ccdd5f49367da05f78bbd (patch)
treeebf009bf1376a5a223a915bc27cbbd791a1316bc /src/temp/mktemp.c
Initial version
Diffstat (limited to 'src/temp/mktemp.c')
-rw-r--r--src/temp/mktemp.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/temp/mktemp.c b/src/temp/mktemp.c
new file mode 100644
index 0000000..7b3d264
--- /dev/null
+++ b/src/temp/mktemp.c
@@ -0,0 +1,30 @@
+#define _GNU_SOURCE
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+char *mktemp(char *template)
+{
+ size_t l = strlen(template);
+ int retries = 100;
+ struct stat st;
+
+ if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) {
+ errno = EINVAL;
+ *template = 0;
+ return template;
+ }
+
+ do {
+ __randname(template+l-6);
+ if (stat(template, &st)) {
+ if (errno != ENOENT) *template = 0;
+ return template;
+ }
+ } while (--retries);
+
+ *template = 0;
+ errno = EEXIST;
+ return template;
+}