move msghdr and cmsghdr out of bits/socket.h
[musl.git] / src / temp / mkostemps.c
blobef24eeae2ce9263ec4f328ebf34ef346da297ea9
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);
29 weak_alias(__mkostemps, mkostemps64);