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