summaryrefslogtreecommitdiffstats
path: root/libc-top-half/musl/src/temp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 13:54:38 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 13:54:38 +0000
commit8c1ab65c0f548d20b7f177bdb736daaf603340e1 (patch)
treedf55b7e75bf43f2bf500845b105afe3ac3a5157e /libc-top-half/musl/src/temp
parentInitial commit. (diff)
downloadwasi-libc-upstream/0.0_git20221206.8b7148f.tar.xz
wasi-libc-upstream/0.0_git20221206.8b7148f.zip
Adding upstream version 0.0~git20221206.8b7148f.upstream/0.0_git20221206.8b7148f
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libc-top-half/musl/src/temp')
-rw-r--r--libc-top-half/musl/src/temp/__randname.c18
-rw-r--r--libc-top-half/musl/src/temp/mkdtemp.c23
-rw-r--r--libc-top-half/musl/src/temp/mkostemp.c9
-rw-r--r--libc-top-half/musl/src/temp/mkostemps.c29
-rw-r--r--libc-top-half/musl/src/temp/mkstemp.c8
-rw-r--r--libc-top-half/musl/src/temp/mkstemps.c9
-rw-r--r--libc-top-half/musl/src/temp/mktemp.c30
7 files changed, 126 insertions, 0 deletions
diff --git a/libc-top-half/musl/src/temp/__randname.c b/libc-top-half/musl/src/temp/__randname.c
new file mode 100644
index 0000000..2bce37a
--- /dev/null
+++ b/libc-top-half/musl/src/temp/__randname.c
@@ -0,0 +1,18 @@
+#include <time.h>
+#include <stdint.h>
+
+/* This assumes that a check for the
+ template size has already been made */
+char *__randname(char *template)
+{
+ int i;
+ struct timespec ts;
+ unsigned long r;
+
+ __clock_gettime(CLOCK_REALTIME, &ts);
+ r = ts.tv_nsec*65537 ^ (uintptr_t)&ts / 16 + (uintptr_t)template;
+ for (i=0; i<6; i++, r>>=5)
+ template[i] = 'A'+(r&15)+(r&16)*2;
+
+ return template;
+}
diff --git a/libc-top-half/musl/src/temp/mkdtemp.c b/libc-top-half/musl/src/temp/mkdtemp.c
new file mode 100644
index 0000000..5708257
--- /dev/null
+++ b/libc-top-half/musl/src/temp/mkdtemp.c
@@ -0,0 +1,23 @@
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+char *mkdtemp(char *template)
+{
+ size_t l = strlen(template);
+ int retries = 100;
+
+ if (l<6 || memcmp(template+l-6, "XXXXXX", 6)) {
+ errno = EINVAL;
+ return 0;
+ }
+
+ do {
+ __randname(template+l-6);
+ if (!mkdir(template, 0700)) return template;
+ } while (--retries && errno == EEXIST);
+
+ memcpy(template+l-6, "XXXXXX", 6);
+ return 0;
+}
diff --git a/libc-top-half/musl/src/temp/mkostemp.c b/libc-top-half/musl/src/temp/mkostemp.c
new file mode 100644
index 0000000..d8dcb80
--- /dev/null
+++ b/libc-top-half/musl/src/temp/mkostemp.c
@@ -0,0 +1,9 @@
+#define _BSD_SOURCE
+#include <stdlib.h>
+
+int mkostemp(char *template, int flags)
+{
+ return __mkostemps(template, 0, flags);
+}
+
+weak_alias(mkostemp, mkostemp64);
diff --git a/libc-top-half/musl/src/temp/mkostemps.c b/libc-top-half/musl/src/temp/mkostemps.c
new file mode 100644
index 0000000..ef24eea
--- /dev/null
+++ b/libc-top-half/musl/src/temp/mkostemps.c
@@ -0,0 +1,29 @@
+#define _BSD_SOURCE
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+
+int __mkostemps(char *template, int len, int flags)
+{
+ size_t l = strlen(template);
+ if (l<6 || len>l-6 || memcmp(template+l-len-6, "XXXXXX", 6)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ flags -= flags & O_ACCMODE;
+ int fd, retries = 100;
+ do {
+ __randname(template+l-len-6);
+ if ((fd = open(template, flags | O_RDWR | O_CREAT | O_EXCL, 0600))>=0)
+ return fd;
+ } while (--retries && errno == EEXIST);
+
+ memcpy(template+l-len-6, "XXXXXX", 6);
+ return -1;
+}
+
+weak_alias(__mkostemps, mkostemps);
+weak_alias(__mkostemps, mkostemps64);
diff --git a/libc-top-half/musl/src/temp/mkstemp.c b/libc-top-half/musl/src/temp/mkstemp.c
new file mode 100644
index 0000000..166b8af
--- /dev/null
+++ b/libc-top-half/musl/src/temp/mkstemp.c
@@ -0,0 +1,8 @@
+#include <stdlib.h>
+
+int mkstemp(char *template)
+{
+ return __mkostemps(template, 0, 0);
+}
+
+weak_alias(mkstemp, mkstemp64);
diff --git a/libc-top-half/musl/src/temp/mkstemps.c b/libc-top-half/musl/src/temp/mkstemps.c
new file mode 100644
index 0000000..6b7531b
--- /dev/null
+++ b/libc-top-half/musl/src/temp/mkstemps.c
@@ -0,0 +1,9 @@
+#define _BSD_SOURCE
+#include <stdlib.h>
+
+int mkstemps(char *template, int len)
+{
+ return __mkostemps(template, len, 0);
+}
+
+weak_alias(mkstemps, mkstemps64);
diff --git a/libc-top-half/musl/src/temp/mktemp.c b/libc-top-half/musl/src/temp/mktemp.c
new file mode 100644
index 0000000..7b3d264
--- /dev/null
+++ b/libc-top-half/musl/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;
+}