reduce some ridiculously large spin counts
[musl] / src / stdio / tmpnam.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <time.h>
5 #include "libc.h"
6 #include "syscall.h"
7 #include "atomic.h"
8
9 #define MAXTRIES 100
10
11 char *tmpnam(char *s)
12 {
13         static int index;
14         static char s2[L_tmpnam];
15         struct timespec ts;
16         int try = 0;
17         unsigned n;
18
19         if (!s) s = s2;
20
21         if (__syscall(SYS_access, P_tmpdir, R_OK|W_OK|X_OK) != 0)
22                 return NULL;
23
24         do {
25                 __syscall(SYS_clock_gettime, CLOCK_REALTIME, &ts, 0);
26                 n = ts.tv_nsec ^ (unsigned)&s ^ (unsigned)s;
27                 snprintf(s, L_tmpnam, "/tmp/t%x-%x", a_fetch_add(&index, 1), n);
28         } while (!__syscall(SYS_access, s, F_OK) && try++<MAXTRIES);
29         return try>=MAXTRIES ? 0 : s;
30 }