X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Ftemp%2Fmktemp.c;h=4ab0df2069ac6d0c685ea2265f15d40596392217;hb=7e222f46a832cc7c148c376084c69d7493b11d6c;hp=1078b9df805e433ebd2d0f4fbe2ab87553e80df9;hpb=5377715ce07269131f011e6f4e978fb5dbf0b294;p=musl diff --git a/src/temp/mktemp.c b/src/temp/mktemp.c index 1078b9df..4ab0df20 100644 --- a/src/temp/mktemp.c +++ b/src/temp/mktemp.c @@ -1,31 +1,32 @@ +#define _GNU_SOURCE #include -#include #include -#include -#include #include -#include "libc.h" +#include -char *__mktemp(char *template) +char *__randname(char *); + +char *mktemp(char *template) { - static int lock; - static int index; - int l = strlen(template); + size_t l = strlen(template); + int retries = 100; + struct stat st; - if (l < 6 || strcmp(template+l-6, "XXXXXX")) { + if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) { errno = EINVAL; - return NULL; + *template = 0; + return template; } - LOCK(&lock); - for (; index < 1000000; index++) { - snprintf(template+l-6, 6, "%06d", index); - if (access(template, F_OK) != 0) { - UNLOCK(&lock); + + do { + __randname(template+l-6); + if (stat(template, &st)) { + if (errno != ENOENT) *template = 0; return template; } - } - UNLOCK(&lock); - return NULL; -} + } while (--retries); -weak_alias(__mktemp, mktemp); + *template = 0; + errno = EEXIST; + return template; +}