Skip to content

Commit f09db36

Browse files
committed
pty: fix cwd on windows
1 parent bff3a61 commit f09db36

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/pty.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,18 @@ static WCHAR *prep_env(char **envp) {
257257
return NULL;
258258
}
259259

260+
static WCHAR *to_utf16(char *str) {
261+
int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
262+
if (len <= 0) return NULL;
263+
WCHAR *wstr = xmalloc((len + 1) * sizeof(WCHAR));
264+
if (len != MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, len)) {
265+
free(wstr);
266+
return NULL;
267+
}
268+
wstr[len] = L'\0';
269+
return wstr;
270+
}
271+
260272
static bool conpty_setup(HPCON *hnd, COORD size, STARTUPINFOEXW *si_ex, char **in_name, char **out_name) {
261273
static int count = 0;
262274
char buf[256];
@@ -361,13 +373,15 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) {
361373
uv_pipe_connect(out_req, io->out, out_name, connect_cb);
362374

363375
PROCESS_INFORMATION pi = {0};
364-
WCHAR *cmdline, *env;
376+
WCHAR *cmdline, *env, *cwd;
365377
cmdline = join_args(process->argv);
366378
if (cmdline == NULL) goto cleanup;
367379
env = prep_env(process->envp);
368380
if (env == NULL) goto cleanup;
381+
cwd = to_utf16(process->cwd);
382+
if (cwd == NULL) goto cleanup;
369383

370-
if (!CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, flags, env, process->cwd, &process->si.StartupInfo, &pi)) {
384+
if (!CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, flags, env, cwd, &process->si.StartupInfo, &pi)) {
371385
print_error("CreateProcessW");
372386
goto cleanup;
373387
}
@@ -392,6 +406,7 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) {
392406
if (out_name != NULL) free(out_name);
393407
if (cmdline != NULL) free(cmdline);
394408
if (env != NULL) free(env);
409+
if (cwd != NULL) free(cwd);
395410
return status;
396411
}
397412
#else

0 commit comments

Comments
 (0)