-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path_tmpnam.c
39 lines (35 loc) · 928 Bytes
/
_tmpnam.c
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
#include "_stdio.h"
#include "_stdlib.h"
#include "_unistd.h"
#include "sys/_time.h"
#define L_tmpfnam 15
static int seed_initialized = 0;
static unsigned long int seed;
static int random_int(void) {
int size = sizeof(int);
int ret;
char *p = (char *) &ret;
struct timeval tv;
if (!seed_initialized) {
gettimeofday(&tv, NULL);
seed = (unsigned long int) (tv.tv_usec ^ tv.tv_sec);
seed ^= seed << 24 | seed >> 8;
seed_initialized = 1;
}
while (size-- > 0)
*p++ = (char) _rand(&seed);
return ret;
}
char *_tmpnam(char *s, size_t size) {
int i;
int dlen = _tmpdir(s, size - L_tmpfnam);
int value = random_int();
if (dlen <= 0)
return NULL;
for (i = 0; i < TMP_MAX; i++) {
sprintf(s + dlen, "tmp%04x%08x", getpid() & 0xffff, value + i);
if (access(s, F_OK) != 0)
return s;
}
return NULL;
}