32c6be5778f84bcc64ad843c6d4299445dd257ae
[musl] / src / temp / mkstemp.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 "libc.h"
10
11 int mkstemp(char *template)
12 {
13         int fd;
14 retry:
15         if (!mktemp(template)) return -1;
16         fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
17         if (fd >= 0) return fd;
18         if (errno == EEXIST) {
19                 /* this is safe because mktemp verified
20                  * that we have a valid template string */
21                 strcpy(template+strlen(template)-6, "XXXXXX");
22                 goto retry;
23         }
24         return -1;
25 }
26
27 LFS64(mkstemp);