OLD | NEW |
1 #define _GNU_SOURCE | 1 #define _GNU_SOURCE |
2 #include <string.h> | 2 #include <string.h> |
3 #include <stdlib.h> | 3 #include <stdlib.h> |
4 #include <errno.h> | 4 #include <errno.h> |
5 #include <sys/stat.h> | 5 #include <sys/stat.h> |
6 | 6 |
7 char *__randname(char *); | 7 char* __randname(char*); |
8 | 8 |
9 char *mktemp(char *template) | 9 char* mktemp(char* template) { |
10 { | 10 size_t l = strlen(template); |
11 » size_t l = strlen(template); | 11 int retries = 100; |
12 » int retries = 100; | 12 struct stat st; |
13 » struct stat st; | |
14 | 13 |
15 » if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) { | 14 if (l < 6 || memcmp(template + l - 6, "XXXXXX", 6)) { |
16 » » errno = EINVAL; | 15 errno = EINVAL; |
17 » » *template = 0; | 16 *template = 0; |
18 » » return template; | 17 return template; |
19 » } | 18 } |
20 | 19 |
21 » do { | 20 do { |
22 » » __randname(template+l-6); | 21 __randname(template + l - 6); |
23 » » if (stat(template, &st)) { | 22 if (stat(template, &st)) { |
24 » » » if (errno != ENOENT) *template = 0; | 23 if (errno != ENOENT) |
25 » » » return template; | 24 *template = 0; |
26 » » } | 25 return template; |
27 » } while (--retries); | 26 } |
| 27 } while (--retries); |
28 | 28 |
29 » *template = 0; | 29 *template = 0; |
30 » errno = EEXIST; | 30 errno = EEXIST; |
31 » return template; | 31 return template; |
32 } | 32 } |
OLD | NEW |