use memcmp instead of str[n]cmp for temp function XXXXXX checking
[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
14         if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) {
15                 errno = EINVAL;
16                 *template = 0;
17                 return template;
18         }
19         while (retries--) {
20                 __randname(template+l-6);
21                 if (access(template, F_OK) < 0) return template;
22         }
23         *template = 0;
24         errno = EEXIST;
25         return template;
26 }
27
28 weak_alias(__mktemp, mktemp);