summaryrefslogtreecommitdiff
path: root/src/temp/mktemp.c
blob: a1c89a6cc322ea5f4158e64ba713b7b41b116772 (plain) (blame)
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
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

char *__randname(char *);

char *mktemp(char *template)
{
	size_t l = strlen(template);
	int retries = 10000;

	if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) {
		errno = EINVAL;
		*template = 0;
		return template;
	}
	while (retries--) {
		__randname(template+l-6);
		if (access(template, F_OK) < 0) return template;
	}
	*template = 0;
	errno = EEXIST;
	return template;
}