summaryrefslogtreecommitdiff
path: root/libc/stdlib/mktemp.c
blob: 9d65d04415078ab3c1ad2b85599813042969b757 (plain)
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
41
#include <string.h>
#include <features.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

char *mktemp(template)
char *template;
{
	int i;
	int num __attribute__ ((unused));	/* UNINITIALIZED */
	int n2;
	int l = strlen(template);
	struct stat stbuf;

	if (l < 6) {
		__set_errno(EINVAL);
		return 0;
	}

	for (i = l - 6; i < l; i++)
		if (template[i] != 'X') {
			__set_errno(EINVAL);
			return 0;
		}

  again:
	n2 = num;
	for (i = l - 1; i >= l - 6; i--) {
		template[i] = '0' + n2 % 10;
		n2 /= 10;
	}

	if (stat(template, &stbuf) == 0) {
		num++;
		goto again;
	}

	return template;
}