1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| | #include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <stdint.h>
#include "libc.h"
void __randname(char *x6)
{
size_t i = 0;
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
unsigned long r = ts.tv_nsec + (uintptr_t)&ts / 16 + (uintptr_t)x6;
for (; i<6; i++, r>>=4) x6[i] = 'A'+(r&15);
}
char *__mktemp(char *template)
{
int retries = 10000;
size_t l = strlen(template);
char *x6 = template +l -6;
if(l < 6 || memcmp(x6, "XXXXXX", 6)) {
errno = EINVAL;
*template = 0;
return template;
}
while (retries--) {
__randname(x6);
if (access(template, F_OK) < 0) return template;
}
*template = 0;
errno = EEXIST;
return template;
}
weak_alias(__mktemp, mktemp);
|