ed2c103e776ea4fa84edc3f198b33aec30bfbc4e
[musl] / src / temp / mktemp.c
1 #include <string.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include "libc.h"
6
7 char *__randname(char *);
8
9 char *__mktemp(char *template)
10 {
11         size_t l = strlen(template);
12         int retries = 10000;
13         unsigned long r;
14
15         if (l < 6 || strcmp(template+l-6, "XXXXXX")) {
16                 errno = EINVAL;
17                 *template = 0;
18                 return template;
19         }
20         while (retries--) {
21                 __randname(template+l-6);
22                 if (access(template, F_OK) < 0) return template;
23         }
24         *template = 0;
25         errno = EEXIST;
26         return template;
27 }
28
29 weak_alias(__mktemp, mktemp);