From 9bc8a11afeed3569736b89754012e3ca22ee10f6 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sun, 20 Feb 2022 13:18:21 -0800 Subject: conclusion --- 05/musl-0.6.0/src/temp/mkdtemp.c | 23 +++++++++++++++++++++++ 05/musl-0.6.0/src/temp/mkstemp.c | 28 ++++++++++++++++++++++++++++ 05/musl-0.6.0/src/temp/mktemp.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 05/musl-0.6.0/src/temp/mkdtemp.c create mode 100644 05/musl-0.6.0/src/temp/mkstemp.c create mode 100644 05/musl-0.6.0/src/temp/mktemp.c (limited to '05/musl-0.6.0/src/temp') 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 +#include +#include +#include +#include +#include +#include +#include +#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 +#include +#include +#include +#include +#include +#include +#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 +#include +#include +#include +#include +#include +#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); -- cgit v1.2.3