fix AS-safety of close when aio is in use and fd map is expanded
[musl.git] / src / temp / mkostemps.c
blob093d2380d86bdf60970931ba8899ac5442a8a66e
1 #define _BSD_SOURCE
2 #include <stdlib.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <errno.h>
8 int __mkostemps(char *template, int len, int flags)
10 size_t l = strlen(template);
11 if (l<6 || len>l-6 || memcmp(template+l-len-6, "XXXXXX", 6)) {
12 errno = EINVAL;
13 return -1;
16 flags -= flags & O_ACCMODE;
17 int fd, retries = 100;
18 do {
19 __randname(template+l-len-6);
20 if ((fd = open(template, flags | O_RDWR | O_CREAT | O_EXCL, 0600))>=0)
21 return fd;
22 } while (--retries && errno == EEXIST);
24 memcpy(template+l-len-6, "XXXXXX", 6);
25 return -1;
28 weak_alias(__mkostemps, mkostemps);