X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Ftemp%2Fmktemp.c;h=7b3d2648b20676eb25fd451ea26db93ef8ca8b65;hb=59b64ff686cef2a87e9552658b2c8d2531f87176;hp=1078b9df805e433ebd2d0f4fbe2ab87553e80df9;hpb=5377715ce07269131f011e6f4e978fb5dbf0b294;p=musl diff --git a/src/temp/mktemp.c b/src/temp/mktemp.c index 1078b9df..7b3d2648 100644 --- a/src/temp/mktemp.c +++ b/src/temp/mktemp.c @@ -1,31 +1,30 @@ +#define _GNU_SOURCE #include -#include #include -#include -#include #include -#include "libc.h" +#include -char *__mktemp(char *template) +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; +}