cb030ee4de074938b58c1e231ef97993341557e3
[musl] / src / temp / mkdtemp.c
1 #define _GNU_SOURCE
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <limits.h>
8 #include <errno.h>
9 #include <sys/stat.h>
10 #include "libc.h"
11
12 char *mkdtemp(char *template)
13 {
14         for (;;) {
15                 if (!mktemp(template)) return 0;
16                 if (!mkdir(template, 0700)) return template;
17                 if (errno != EEXIST) return 0;
18                 /* this is safe because mktemp verified
19                  * that we have a valid template string */
20                 strcpy(template+strlen(template)-6, "XXXXXX");
21         }
22 }