From 1415e5c56ba4ef9b28f1b42a9a7fef3103a9c557 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Mon, 16 Aug 2021 23:28:57 +0800 Subject: [PATCH] pty: fix cwd on windows --- src/pty.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/pty.c b/src/pty.c index dd10bfd..77c9872 100644 --- a/src/pty.c +++ b/src/pty.c @@ -257,6 +257,18 @@ failed: return NULL; } +static WCHAR *to_utf16(char *str) { + int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); + if (len <= 0) return NULL; + WCHAR *wstr = xmalloc((len + 1) * sizeof(WCHAR)); + if (len != MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, len)) { + free(wstr); + return NULL; + } + wstr[len] = L'\0'; + return wstr; +} + static bool conpty_setup(HPCON *hnd, COORD size, STARTUPINFOEXW *si_ex, char **in_name, char **out_name) { static int count = 0; char buf[256]; @@ -361,13 +373,17 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) { uv_pipe_connect(out_req, io->out, out_name, connect_cb); PROCESS_INFORMATION pi = {0}; - WCHAR *cmdline, *env; + WCHAR *cmdline, *env, *cwd; cmdline = join_args(process->argv); if (cmdline == NULL) goto cleanup; env = prep_env(process->envp); if (env == NULL) goto cleanup; + if (process->cwd != NULL) { + cwd = to_utf16(process->cwd); + if (cwd == NULL) goto cleanup; + } - if (!CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, flags, env, process->cwd, &process->si.StartupInfo, &pi)) { + if (!CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, flags, env, cwd, &process->si.StartupInfo, &pi)) { print_error("CreateProcessW"); goto cleanup; } @@ -392,6 +408,7 @@ cleanup: if (out_name != NULL) free(out_name); if (cmdline != NULL) free(cmdline); if (env != NULL) free(env); + if (cwd != NULL) free(cwd); return status; } #else -- 2.43.4