Added SDL_PROP_PROCESS_CREATE_WORKING_DIRECTORY_STRING

Fixes https://github.com/libsdl-org/SDL/issues/12654
This commit is contained in:
Sam Lantinga
2025-03-27 08:17:49 -07:00
parent 289f5cfdbb
commit 0f27c3aabd
5 changed files with 48 additions and 1 deletions

View File

@@ -37,6 +37,12 @@
#include "../../io/SDL_iostream_c.h"
#if defined(HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP) && \
!defined(HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR)
#define HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR
#define posix_spawn_file_actions_addchdir posix_spawn_file_actions_addchdir_np
#endif
#define READ_END 0
#define WRITE_END 1
@@ -156,6 +162,7 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
char * const *args = SDL_GetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, NULL);
SDL_Environment *env = SDL_GetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER, SDL_GetEnvironment());
char **envp = NULL;
const char *working_directory = SDL_GetStringProperty(props, SDL_PROP_PROCESS_CREATE_WORKING_DIRECTORY_STRING, NULL);
SDL_ProcessIO stdin_option = (SDL_ProcessIO)SDL_GetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_NULL);
SDL_ProcessIO stdout_option = (SDL_ProcessIO)SDL_GetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_INHERITED);
SDL_ProcessIO stderr_option = (SDL_ProcessIO)SDL_GetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_NUMBER, SDL_PROCESS_STDIO_INHERITED);
@@ -192,6 +199,30 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
goto posix_spawn_fail_attr;
}
if (working_directory) {
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR
#ifdef SDL_PLATFORM_APPLE
if (__builtin_available(macOS 10.15, *)) {
if (posix_spawn_file_actions_addchdir(&fa, working_directory) != 0) {
SDL_SetError("posix_spawn_file_actions_addchdir failed: %s", strerror(errno));
goto posix_spawn_fail_all;
}
} else {
SDL_SetError("Setting the working directory is only supported on macOS 10.15 and newer");
goto posix_spawn_fail_all;
}
#else
if (posix_spawn_file_actions_addchdir(&fa, working_directory) != 0) {
SDL_SetError("posix_spawn_file_actions_addchdir failed: %s", strerror(errno));
goto posix_spawn_fail_all;
}
#endif // SDL_PLATFORM_APPLE
#else
SDL_SetError("Setting the working directory is not supported");
goto posix_spawn_fail_all;
#endif
}
// Background processes don't have access to the terminal
if (process->background) {
if (stdin_option == SDL_PROCESS_STDIO_INHERITED) {

View File

@@ -239,6 +239,7 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
const char * const *args = SDL_GetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, NULL);
SDL_Environment *env = SDL_GetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER, SDL_GetEnvironment());
char **envp = NULL;
const char *working_directory = SDL_GetStringProperty(props, SDL_PROP_PROCESS_CREATE_WORKING_DIRECTORY_STRING, NULL);
SDL_ProcessIO stdin_option = (SDL_ProcessIO)SDL_GetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_NULL);
SDL_ProcessIO stdout_option = (SDL_ProcessIO)SDL_GetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_INHERITED);
SDL_ProcessIO stderr_option = (SDL_ProcessIO)SDL_GetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_NUMBER, SDL_PROCESS_STDIO_INHERITED);
@@ -246,6 +247,7 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
!SDL_HasProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_NUMBER);
LPWSTR createprocess_cmdline = NULL;
LPWSTR createprocess_env = NULL;
LPWSTR createprocess_cwd = NULL;
STARTUPINFOW startup_info;
DWORD creation_flags;
SECURITY_ATTRIBUTES security_attributes;
@@ -292,6 +294,13 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
goto done;
}
if (working_directory) {
createprocess_cwd = WIN_UTF8ToStringW(working_directory);
if (!createprocess_cwd) {
goto done;
}
}
// Background processes don't have access to the terminal
// This isn't necessary on Windows, but we keep the same behavior as the POSIX implementation.
if (process->background) {
@@ -427,7 +436,7 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
}
}
if (!CreateProcessW(NULL, createprocess_cmdline, NULL, NULL, TRUE, creation_flags, createprocess_env, NULL, &startup_info, &data->process_information)) {
if (!CreateProcessW(NULL, createprocess_cmdline, NULL, NULL, TRUE, creation_flags, createprocess_env, createprocess_cwd, &startup_info, &data->process_information)) {
WIN_SetError("CreateProcess");
goto done;
}
@@ -479,6 +488,7 @@ done:
}
SDL_free(createprocess_cmdline);
SDL_free(createprocess_env);
SDL_free(createprocess_cwd);
SDL_free(envp);
if (!result) {