X-Git-Url: http://nsz.repo.hu/git/?p=musl;a=blobdiff_plain;f=src%2Fstdio%2Ftempnam.c;h=f73ca9f9027f4cec78819da8b954fb7d932a837f;hp=2cbcb8649f1fd885d145ba1d8b97b8c3263a9586;hb=6d861ac87491a207e4599c44b61d142f0a601c2d;hpb=0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 diff --git a/src/stdio/tempnam.c b/src/stdio/tempnam.c index 2cbcb864..f73ca9f9 100644 --- a/src/stdio/tempnam.c +++ b/src/stdio/tempnam.c @@ -1,19 +1,23 @@ #include #include #include -#include +#include #include -#include -#include +#include #include "libc.h" +#include "atomic.h" + +#define MAXTRIES 100 char *tempnam(const char *dir, const char *pfx) { - static int lock; static int index; char *s; + struct timespec ts; int pid = getpid(); - int l; + size_t l; + int n; + int try=0; if (!dir) dir = P_tmpdir; if (!pfx) pfx = "temp"; @@ -21,22 +25,18 @@ char *tempnam(const char *dir, const char *pfx) if (access(dir, R_OK|W_OK|X_OK) != 0) return NULL; - l = strlen(dir) + 1 + strlen(pfx) + 2 + sizeof(int)*3*2 + 1; + l = strlen(dir) + 1 + strlen(pfx) + 3*(sizeof(int)*3+2) + 1; s = malloc(l); - if (!s) { - errno = ENOMEM; - return NULL; - } + if (!s) return s; - LOCK(&lock); - for (; index < TMP_MAX; index++) { - snprintf(s, l, "%s/%s-%d-%d", dir, pfx, pid, index); - if (access(s, F_OK) != 0) { - UNLOCK(&lock); - return s; - } + do { + clock_gettime(CLOCK_REALTIME, &ts); + n = ts.tv_nsec ^ (uintptr_t)&s ^ (uintptr_t)s; + snprintf(s, l, "%s/%s-%d-%d-%x", dir, pfx, pid, a_fetch_add(&index, 1), n); + } while (!access(s, F_OK) && try++=MAXTRIES) { + free(s); + return 0; } - UNLOCK(&lock); - free(s); - return NULL; + return s; }