X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Ftemp%2Fmkdtemp.c;h=5708257bfecf6ae3e05a5e9fdd0e7054a96d0a7c;hb=0fbd7d6683d04dcaa636df53e7fce69ece746241;hp=cb030ee4de074938b58c1e231ef97993341557e3;hpb=80695b1d1e64a026c473a33965e680444e863e34;p=musl diff --git a/src/temp/mkdtemp.c b/src/temp/mkdtemp.c index cb030ee4..5708257b 100644 --- a/src/temp/mkdtemp.c +++ b/src/temp/mkdtemp.c @@ -1,22 +1,23 @@ -#define _GNU_SOURCE #include -#include #include -#include -#include -#include #include #include -#include "libc.h" 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"); + 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; }