fixed env and cwd for windows

This commit is contained in:
Alexander Treml 2025-07-01 19:41:04 +02:00
parent c7f04f1f9e
commit 73df29a8bd
3 changed files with 22 additions and 31 deletions

View file

@ -458,7 +458,6 @@ Dictionary PTY::_get_fork_env() const
#endif #endif
// TODO This might need windows specific adjustment // TODO This might need windows specific adjustment
return env;
Dictionary os_env; Dictionary os_env;
uv_env_item_t *uv_env; uv_env_item_t *uv_env;
int count; int count;

View file

@ -73,19 +73,26 @@ Dictionary PTYWin::fork(
// args // args
PackedStringArray argv_ = p_args; PackedStringArray argv_ = p_args;
// env // env block (see: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa)
PackedStringArray env_ = p_env; PackedStringArray env_ = p_env;
int envc = env_.size(); int envc = env_.size();
std::unique_ptr<char *, DelBuf> env_unique_ptr(new char *[envc + 1], DelBuf(envc + 1)); // Calculate total size needed
char **env = env_unique_ptr.get(); size_t total_size = 1; // 1 for the block null terminator
env[envc] = NULL; for (int i = 0; i < envc; i++) {
for (int i = 0; i < envc; i++)
{
std::string pair = env_[i].utf8().get_data(); std::string pair = env_[i].utf8().get_data();
env[i] = strdup(pair.c_str()); total_size += pair.size() + 1; // +1 for string null terminator
} }
std::unique_ptr<char[]> env_block(new char[total_size]);
size_t offset = 0;
for (int i = 0; i < envc; i++) {
std::string pair = env_[i].utf8().get_data();
memcpy(env_block.get() + offset, pair.c_str(), pair.size() + 1); // +1 for string null terminator
offset += pair.size() + 1;
}
env_block.get()[offset++] = '\0'; // block null terminator
char *env = env_block.get();
std::string cwd_ = p_cwd.utf8().get_data(); const char *cwd_ = p_cwd.utf8().get_data();
// Determine required size of Pseudo Console // Determine required size of Pseudo Console
COORD winp{}; COORD winp{};
@ -97,26 +104,12 @@ Dictionary PTYWin::fork(
std::string helper_path = p_helper_path.utf8().get_data(); std::string helper_path = p_helper_path.utf8().get_data();
// Build argv // Aggregate file and args into command string
int argc = argv_.size(); std::string cmd = file;
int argl = argc + 2; for (int i = 0; i < argv_.size(); i++)
std::unique_ptr<char *, DelBuf> argv_unique_ptr(new char *[argl], DelBuf(argl));
char **argv = argv_unique_ptr.get();
argv[0] = strdup(file.c_str());
argv[argl - 1] = NULL;
for (int i = 0; i < argc; i++)
{ {
std::string arg = argv_[i].utf8().get_data(); cmd += " ";
argv[i + 1] = strdup(arg.c_str()); cmd += argv_[i].utf8().get_data();
}
// Aggregate argv into command string
std::string cmd;
for (int i = 0; i < argl - 1; i++)
{
if (i > 0)
cmd += " ";
cmd += argv[i];
} }
LPSTR lpcmd = const_cast<char *>(cmd.c_str()); LPSTR lpcmd = const_cast<char *>(cmd.c_str());
@ -150,8 +143,8 @@ Dictionary PTYWin::fork(
NULL, // Thread handle not inheritable NULL, // Thread handle not inheritable
FALSE, // Inherit handles FALSE, // Inherit handles
EXTENDED_STARTUPINFO_PRESENT, // Creation flags EXTENDED_STARTUPINFO_PRESENT, // Creation flags
NULL, // Use parent's environment block env,
NULL, // Use parent's starting directory cwd_,
&startupInfo.StartupInfo, // Pointer to STARTUPINFO &startupInfo.StartupInfo, // Pointer to STARTUPINFO
&pi) // Pointer to PROCESS_INFORMATION &pi) // Pointer to PROCESS_INFORMATION
? S_OK ? S_OK

View file

@ -2,6 +2,5 @@ extends Terminal
@onready var pty = $PTY @onready var pty = $PTY
func _ready(): func _ready():
pty.fork() pty.fork()