blob: 4ab0df2069ac6d0c685ea2265f15d40596392217 [file] [log] [blame]
Szabolcs Nagy57174442013-12-12 05:09:18 +00001#define _GNU_SOURCE
Rich Felker0b44a032011-02-12 00:22:29 -05002#include <string.h>
Szabolcs Nagy57174442013-12-12 05:09:18 +00003#include <stdlib.h>
Rich Felker0b44a032011-02-12 00:22:29 -05004#include <errno.h>
Rich Felker38f44d62013-08-02 01:06:53 -04005#include <sys/stat.h>
Rich Felker0b44a032011-02-12 00:22:29 -05006
Rich Felker2cc63352013-02-20 22:43:23 -05007char *__randname(char *);
8
Rich Felker9a97d102013-08-02 00:52:50 -04009char *mktemp(char *template)
Rich Felker0b44a032011-02-12 00:22:29 -050010{
Rich Felker2cc63352013-02-20 22:43:23 -050011 size_t l = strlen(template);
Rich Felker38f44d62013-08-02 01:06:53 -040012 int retries = 100;
13 struct stat st;
Rich Felker0b44a032011-02-12 00:22:29 -050014
Rich Felker8872e4e2013-02-20 23:01:22 -050015 if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) {
Rich Felker0b44a032011-02-12 00:22:29 -050016 errno = EINVAL;
Rich Felker38258472011-06-12 10:25:29 -040017 *template = 0;
18 return template;
Rich Felker0b44a032011-02-12 00:22:29 -050019 }
Rich Felker38f44d62013-08-02 01:06:53 -040020
21 do {
Rich Felker2cc63352013-02-20 22:43:23 -050022 __randname(template+l-6);
Rich Felker38f44d62013-08-02 01:06:53 -040023 if (stat(template, &st)) {
24 if (errno != ENOENT) *template = 0;
25 return template;
26 }
27 } while (--retries);
28
Rich Felker69ecbd02011-02-19 09:40:07 -050029 *template = 0;
Rich Felker446b4202011-02-18 17:04:56 -050030 errno = EEXIST;
Rich Felker69ecbd02011-02-19 09:40:07 -050031 return template;
Rich Felker0b44a032011-02-12 00:22:29 -050032}