mkdtemp.c 582 字节
Newer Older
R
Rich Felker 已提交
1 2 3 4 5 6 7 8 9 10
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <sys/stat.h>
#include "libc.h"

11 12
char *__mktemp(char *);

R
Rich Felker 已提交
13 14
char *mkdtemp(char *template)
{
15
	int retries = 100, t0 = *template;
16
	while (retries--) {
17
		if (!*__mktemp(template)) return 0;
R
Rich Felker 已提交
18 19 20 21
		if (!mkdir(template, 0700)) return template;
		if (errno != EEXIST) return 0;
		/* this is safe because mktemp verified
		 * that we have a valid template string */
22
		template[0] = t0;
R
Rich Felker 已提交
23 24
		strcpy(template+strlen(template)-6, "XXXXXX");
	}
25
	return 0;
R
Rich Felker 已提交
26
}