ensure standard functions mk[sd]temp don't depend on removed function mktemp
[musl] / src / temp / mktemp.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include "libc.h"
8
9 char *__mktemp(char *template)
10 {
11         static int lock;
12         static int index;
13         int l = strlen(template);
14
15         if (l < 6 || strcmp(template+l-6, "XXXXXX")) {
16                 errno = EINVAL;
17                 return NULL;
18         }
19         LOCK(&lock);
20         for (; index < 1000000; index++) {
21                 snprintf(template+l-6, 6, "%06d", index);
22                 if (access(template, F_OK) != 0) {
23                         UNLOCK(&lock);
24                         return template;
25                 }
26         }
27         UNLOCK(&lock);
28         return NULL;    
29 }
30
31 weak_alias(__mktemp, mktemp);