1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00

[win32] win_mkstemp copied from mingw

Original mkstemp behaved weird under Windows.
This commit is contained in:
Witold Filipczyk 2024-08-28 20:33:59 +02:00
parent b4fa9da2c6
commit 7ae3b46579

View File

@ -16,6 +16,11 @@
#include <unistd.h>
#endif
#ifdef CONFIG_OS_WIN32
#include <share.h>
#include <fcntl.h>
#endif
#include "elinks.h"
#include "config/options.h"
@ -26,6 +31,36 @@
#include "util/string.h"
#ifdef CONFIG_OS_WIN32
static int
win_mkstemp(char *template_name)
{
int i, j, fd, len, index;
static const char letters[] = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
if (template_name == NULL || (len = strlen(template_name)) < 6
|| memcmp(template_name + (len - 6), "XXXXXX", 6)) {
errno = EINVAL;
return -1;
}
for (index = len - 6; index > 0 && template_name[index - 1] == 'X'; index--);
for (i = 0; i >= 0; i++) {
for (j = index; j < len; j++) {
template_name[j] = letters[rand() % 62];
}
fd = _sopen(template_name,
_O_RDWR | _O_CREAT | _O_EXCL | _O_BINARY,
_SH_DENYRW, _S_IREAD | _S_IWRITE);
if (fd != -1) return fd;
if (fd == -1 && errno != EEXIST) return -1;
}
return -1;
}
#endif
/* If infofiles.secure_save is set:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
@ -160,8 +195,12 @@ secure_open_umask(char *file_name)
goto free_file_name;
}
#ifdef CONFIG_OS_WIN32
fd = win_mkstemp(randname);
#else
/* No need to use safe_mkstemp() here. --Zas */
fd = mkstemp(randname);
#endif
if (fd == -1) {
secsave_errno = SS_ERR_MKSTEMP;
mem_free(randname);