|
@@ -2,23 +2,30 @@
|
|
#include <fcntl.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <errno.h>
|
|
|
|
+#include <sys/stat.h>
|
|
|
|
|
|
char *__randname(char *);
|
|
char *__randname(char *);
|
|
|
|
|
|
char *mktemp(char *template)
|
|
char *mktemp(char *template)
|
|
{
|
|
{
|
|
size_t l = strlen(template);
|
|
size_t l = strlen(template);
|
|
- int retries = 10000;
|
|
|
|
|
|
+ int retries = 100;
|
|
|
|
+ struct stat st;
|
|
|
|
|
|
if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) {
|
|
if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) {
|
|
errno = EINVAL;
|
|
errno = EINVAL;
|
|
*template = 0;
|
|
*template = 0;
|
|
return template;
|
|
return template;
|
|
}
|
|
}
|
|
- while (retries--) {
|
|
|
|
|
|
+
|
|
|
|
+ do {
|
|
__randname(template+l-6);
|
|
__randname(template+l-6);
|
|
- if (access(template, F_OK) < 0) return template;
|
|
|
|
- }
|
|
|
|
|
|
+ if (stat(template, &st)) {
|
|
|
|
+ if (errno != ENOENT) *template = 0;
|
|
|
|
+ return template;
|
|
|
|
+ }
|
|
|
|
+ } while (--retries);
|
|
|
|
+
|
|
*template = 0;
|
|
*template = 0;
|
|
errno = EEXIST;
|
|
errno = EEXIST;
|
|
return template;
|
|
return template;
|