5e8bb93426e1d5e0d3f1e1deec2b16d364a329af
[musl] / src / temp / mkstemp.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <limits.h>
7 #include <errno.h>
8 #include "libc.h"
9
10 char *__mktemp(char *);
11
12 int mkstemp(char *template)
13 {
14         int fd;
15 retry:
16         if (!__mktemp(template)) return -1;
17         fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
18         if (fd >= 0) return fd;
19         if (errno == EEXIST) {
20                 /* this is safe because mktemp verified
21                  * that we have a valid template string */
22                 strcpy(template+strlen(template)-6, "XXXXXX");
23                 goto retry;
24         }
25         return -1;
26 }
27
28 LFS64(mkstemp);