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

[win32] That weird mkstemp was our implementation

This commit is contained in:
Witold Filipczyk 2024-08-28 21:04:33 +02:00
parent 7ae3b46579
commit 2dfcc60be9
2 changed files with 24 additions and 43 deletions

View File

@ -27,6 +27,8 @@
#include <stdlib.h>
#include <unistd.h>
#include <locale.h>
#include <share.h>
#include <sys/stat.h>
#include "elinks.h"
@ -279,19 +281,32 @@ gettimeofday(struct timeval* p, void* tz)
return 0;
}
int
mkstemp(char *template_)
mkstemp(char *template_name)
{
char pathname[MAX_PATH];
int i, j, fd, len, index;
/* Get the directory for temp files */
GetTempPath(MAX_PATH, pathname);
static const char letters[] = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
/* Create a temporary file. */
GetTempFileName(pathname, "ABC", 0, template_);
if (template_name == NULL || (len = strlen(template_name)) < 6
|| memcmp(template_name + (len - 6), "XXXXXX", 6)) {
errno = EINVAL;
return -1;
}
return open(template_, O_WRONLY | O_BINARY | O_EXCL);
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;
}
int

View File

@ -30,37 +30,6 @@
#include "util/secsave.h"
#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:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
@ -195,12 +164,9 @@ 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);