X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Ftemp%2Fmktemp.c;h=7b3d2648b20676eb25fd451ea26db93ef8ca8b65;hb=0fbd7d6683d04dcaa636df53e7fce69ece746241;hp=8638e087091395eaf0dcdde1e669fe91e19c5eb9;hpb=0b44a0315b47dd8eced9f3b7f31580cf14bbfc01;p=musl diff --git a/src/temp/mktemp.c b/src/temp/mktemp.c index 8638e087..7b3d2648 100644 --- a/src/temp/mktemp.c +++ b/src/temp/mktemp.c @@ -1,29 +1,30 @@ +#define _GNU_SOURCE #include -#include #include -#include -#include #include -#include "libc.h" +#include 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); + + *template = 0; + errno = EEXIST; + return template; }