d87d4b667b752b75a0908ac3280f3065ae0f7d7b
[musl] / src / temp / mkostemps.c
1 #define _BSD_SOURCE
2 #include <string.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include "libc.h"
7
8 char *__randname(char *);
9
10 int __mkostemps(char *template, int len, int flags)
11 {
12         size_t l = strlen(template);
13         if (l<6 || len>l-6 || strncmp(template+l-len-6, "XXXXXX", 6)) {
14                 errno = EINVAL;
15                 return -1;
16         }
17
18         int fd, retries = 100;
19         while (retries--) {
20                 __randname(template+l-len-6);
21                 if ((fd = open(template, flags | O_RDWR | O_CREAT | O_EXCL, 0600))>=0)
22                         return fd;
23                 if (errno != EEXIST) return -1;
24         }
25         return -1;
26 }
27
28 weak_alias(__mkostemps, mkostemps);
29 weak_alias(__mkostemps, mkostemps64);