1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-19 11:10:25 +02:00

Win32: add replacement for mkdtemp

* libguile/posix-w32.c (mkdtemp): new win32 replacement procedure
* libguile/posix-w32.h: add declaration for mkdtemp
  (HAVE_MKDTEMP): new define
* libguile/filesys.c: include posix-w32.h
This commit is contained in:
Michael Gran 2025-03-28 20:33:55 -07:00
parent c08debbd39
commit 0b70769247
3 changed files with 181 additions and 85 deletions

View file

@ -89,6 +89,9 @@
#include "pairs.h"
#include "ports-internal.h"
#include "ports.h"
#ifdef __MINGW32__
#include "posix-w32.h"
#endif
#include "posix.h"
#if __MINGW32__
#include "posix-w32.h"

View file

@ -18,10 +18,11 @@
<https://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#include <config.h>
#endif
#define WIN32_LEAN_AND_MEAN
#define _CRT_RAND_S
#include <windows.h>
#include <c-strcase.h>
#include <ctype.h>
@ -34,9 +35,14 @@
#include <io.h>
#include <fcntl.h>
<<<<<<< HEAD
#include "gc.h" /* for scm_*alloc, scm_strdup */
#include "filename.h"
#include "threads.h" /* for scm_i_scm_pthread_mutex_lock */
=======
#include "gc.h" /* for scm_*alloc, scm_strdup */
#include "threads.h" /* for scm_i_scm_pthread_mutex_lock */
>>>>>>> bbd90294d (Win32: add replacement for mkdtemp)
#include "posix-w32.h"
@ -46,7 +52,8 @@
int
uname (struct utsname *uts)
{
enum { WinNT, Win95, Win98, WinUnknown };
enum
{ WinNT, Win95, Win98, WinUnknown };
OSVERSIONINFO osver;
SYSTEM_INFO sysinfo;
DWORD sLength;
@ -175,13 +182,14 @@ uname (struct utsname *uts)
GetComputerName (uts->nodename, &sLength);
return 0;
}
/* Translate abnormal exit status of Windows programs into the signal
that terminated the program. This is required to support scm_kill
and WTERMSIG. */
struct signal_and_status {
struct signal_and_status
{
int sig;
DWORD status;
};
@ -216,7 +224,7 @@ w32_signal_to_status (int sig)
if (sig == sigtbl[i].sig)
return sigtbl[i].status;
return (int)0xC000013A;
return (int) 0xC000013A;
}
int
@ -325,9 +333,9 @@ console_has_return_keyevent_w32 (int fdes)
for (i = 0; i < avail; i++)
if (irbuffer[i].EventType == KEY_EVENT)
{
n_chars ++;
n_chars++;
if (irbuffer[i].Event.KeyEvent.uChar.AsciiChar == 13)
n_returns ++;
n_returns++;
}
if (avail < NBUFFER)
break;
@ -382,3 +390,86 @@ canonicalize_file_name_mingw (const char *name)
slashify_file_name (canon);
return canon;
}
// Generate a random string of specified length using character set safe
// for filenames
static void
generate_random_string (char *str, size_t len)
{
const char charset[] = "abcdefghijklmnopqrstuvwxyz0123456789";
const size_t charset_size = sizeof (charset) - 1;
for (size_t i = 0; i < len; i++)
{
unsigned int r;
if (rand_s (&r) != 0)
{
// Handle rand_s failure (rare, but possible)
str[i] = charset[0]; // Fallback to first character
continue;
}
str[i] = charset[r % charset_size];
}
str[len] = '\0';
}
char *
mkdtemp (char *template)
{
const int MAX_TRIES = 20;
if (template == NULL)
{
return NULL;
}
// Check if template ends with "XXXXXX"
size_t len = strlen (template);
if (len < 6 || strcmp (template + len - 6, "XXXXXX") != 0)
{
return NULL;
}
char *working_template = _strdup (template);
if (working_template == NULL)
{
return NULL;
}
// Locate the position of "XXXXXX" in the working copy
char *pattern_pos = working_template + len - 6;
// Try up to MAX_TRIES times to create a unique directory
for (int try = 0; try < MAX_TRIES; try++)
{
// Replace "XXXXXX" with a random 6-character string
generate_random_string (pattern_pos, 6);
if (CreateDirectoryA (working_template, NULL))
{
// Success: copy the modified name back to the original template
strcpy (template, working_template);
free (working_template);
return template;
}
else
{
DWORD error = GetLastError ();
if (error == ERROR_ALREADY_EXISTS)
{
// Name is taken, try again with a new random string
continue;
}
else
{
// Other error (e.g., invalid path, permission denied), give up
free (working_template);
return NULL;
}
}
}
// Failed to find a unique name after MAX_TRIES
free (working_template);
return NULL;
}

View file

@ -76,9 +76,11 @@ SCM_INTERNAL int dlclose_w32 (void *handle);
SCM_INTERNAL char *dlerror_w32 (void);
SCM_INTERNAL int console_has_return_keyevent_w32 (int fdes);
SCM_INTERNAL int getpagesize_w32 (void);
SCM_INTERNAL char* mkdtemp (char* template);
#define HAVE_UNAME 1
#define HAVE_WAITPID 1
#define HAVE_MKDTEMP 1
#define RTLD_NOW 1
#define RTLD_LAZY 2