Skip to content

Commit

Permalink
pty: fix cwd on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Aug 16, 2021
1 parent bff3a61 commit f09db36
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,18 @@ static WCHAR *prep_env(char **envp) {
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];
Expand Down Expand Up @@ -361,13 +373,15 @@ 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;
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;
}
Expand All @@ -392,6 +406,7 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) {
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
Expand Down

0 comments on commit f09db36

Please sign in to comment.