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