summaryrefslogtreecommitdiff
path: root/05/musl-0.6.0/src/temp
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-02-20 13:18:21 -0800
committerpommicket <pommicket@gmail.com>2022-02-20 13:18:21 -0800
commit9bc8a11afeed3569736b89754012e3ca22ee10f6 (patch)
tree5f0ec0d5c05f879b1ee86adfa654ed3ef2178d5f /05/musl-0.6.0/src/temp
parent0f97a589b800bdb71dda05984192f0f66a52edaa (diff)
conclusion
Diffstat (limited to '05/musl-0.6.0/src/temp')
-rw-r--r--05/musl-0.6.0/src/temp/mkdtemp.c23
-rw-r--r--05/musl-0.6.0/src/temp/mkstemp.c28
-rw-r--r--05/musl-0.6.0/src/temp/mktemp.c31
3 files changed, 82 insertions, 0 deletions
diff --git a/05/musl-0.6.0/src/temp/mkdtemp.c b/05/musl-0.6.0/src/temp/mkdtemp.c
new file mode 100644
index 0000000..162d98b
--- /dev/null
+++ b/05/musl-0.6.0/src/temp/mkdtemp.c
@@ -0,0 +1,23 @@
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <limits.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include "libc.h"
+
+char *__mktemp(char *);
+
+char *mkdtemp(char *template)
+{
+ for (;;) {
+ if (!__mktemp(template)) return 0;
+ if (!mkdir(template, 0700)) return template;
+ if (errno != EEXIST) return 0;
+ /* this is safe because mktemp verified
+ * that we have a valid template string */
+ strcpy(template+strlen(template)-6, "XXXXXX");
+ }
+}
diff --git a/05/musl-0.6.0/src/temp/mkstemp.c b/05/musl-0.6.0/src/temp/mkstemp.c
new file mode 100644
index 0000000..5e8bb93
--- /dev/null
+++ b/05/musl-0.6.0/src/temp/mkstemp.c
@@ -0,0 +1,28 @@
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <limits.h>
+#include <errno.h>
+#include "libc.h"
+
+char *__mktemp(char *);
+
+int mkstemp(char *template)
+{
+ int fd;
+retry:
+ if (!__mktemp(template)) return -1;
+ fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
+ if (fd >= 0) return fd;
+ if (errno == EEXIST) {
+ /* this is safe because mktemp verified
+ * that we have a valid template string */
+ strcpy(template+strlen(template)-6, "XXXXXX");
+ goto retry;
+ }
+ return -1;
+}
+
+LFS64(mkstemp);
diff --git a/05/musl-0.6.0/src/temp/mktemp.c b/05/musl-0.6.0/src/temp/mktemp.c
new file mode 100644
index 0000000..1078b9d
--- /dev/null
+++ b/05/musl-0.6.0/src/temp/mktemp.c
@@ -0,0 +1,31 @@
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include "libc.h"
+
+char *__mktemp(char *template)
+{
+ static int lock;
+ static int index;
+ int l = strlen(template);
+
+ if (l < 6 || strcmp(template+l-6, "XXXXXX")) {
+ errno = EINVAL;
+ return NULL;
+ }
+ LOCK(&lock);
+ for (; index < 1000000; index++) {
+ snprintf(template+l-6, 6, "%06d", index);
+ if (access(template, F_OK) != 0) {
+ UNLOCK(&lock);
+ return template;
+ }
+ }
+ UNLOCK(&lock);
+ return NULL;
+}
+
+weak_alias(__mktemp, mktemp);