Szabolcs Nagy | 5717444 | 2013-12-12 05:09:18 +0000 | [diff] [blame] | 1 | #define _GNU_SOURCE |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 2 | #include <string.h> |
Szabolcs Nagy | 5717444 | 2013-12-12 05:09:18 +0000 | [diff] [blame] | 3 | #include <stdlib.h> |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 4 | #include <errno.h> |
Rich Felker | 38f44d6 | 2013-08-02 01:06:53 -0400 | [diff] [blame] | 5 | #include <sys/stat.h> |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 6 | |
Rich Felker | 2cc6335 | 2013-02-20 22:43:23 -0500 | [diff] [blame] | 7 | char *__randname(char *); |
8 | |||||
Rich Felker | 9a97d10 | 2013-08-02 00:52:50 -0400 | [diff] [blame] | 9 | char *mktemp(char *template) |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 10 | { |
Rich Felker | 2cc6335 | 2013-02-20 22:43:23 -0500 | [diff] [blame] | 11 | size_t l = strlen(template); |
Rich Felker | 38f44d6 | 2013-08-02 01:06:53 -0400 | [diff] [blame] | 12 | int retries = 100; |
13 | struct stat st; | ||||
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 14 | |
Rich Felker | 8872e4e | 2013-02-20 23:01:22 -0500 | [diff] [blame] | 15 | if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) { |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 16 | errno = EINVAL; |
Rich Felker | 3825847 | 2011-06-12 10:25:29 -0400 | [diff] [blame] | 17 | *template = 0; |
18 | return template; | ||||
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 19 | } |
Rich Felker | 38f44d6 | 2013-08-02 01:06:53 -0400 | [diff] [blame] | 20 | |
21 | do { | ||||
Rich Felker | 2cc6335 | 2013-02-20 22:43:23 -0500 | [diff] [blame] | 22 | __randname(template+l-6); |
Rich Felker | 38f44d6 | 2013-08-02 01:06:53 -0400 | [diff] [blame] | 23 | if (stat(template, &st)) { |
24 | if (errno != ENOENT) *template = 0; | ||||
25 | return template; | ||||
26 | } | ||||
27 | } while (--retries); | ||||
28 | |||||
Rich Felker | 69ecbd0 | 2011-02-19 09:40:07 -0500 | [diff] [blame] | 29 | *template = 0; |
Rich Felker | 446b420 | 2011-02-18 17:04:56 -0500 | [diff] [blame] | 30 | errno = EEXIST; |
Rich Felker | 69ecbd0 | 2011-02-19 09:40:07 -0500 | [diff] [blame] | 31 | return template; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 32 | } |