Skip to content

Commit fcec9a6

Browse files
committed
pty: inherit env from parent process
1 parent 1415e5c commit fcec9a6

File tree

1 file changed

+16
-43
lines changed

1 file changed

+16
-43
lines changed

src/pty.c

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -233,30 +233,6 @@ static WCHAR *join_args(char **argv) {
233233
return NULL;
234234
}
235235

236-
static WCHAR *prep_env(char **envp) {
237-
WCHAR *env = NULL;
238-
size_t env_len = 0;
239-
char **ptr = envp;
240-
for (; *ptr; ptr++) {
241-
int len = MultiByteToWideChar(CP_UTF8, 0, *ptr, -1, NULL, 0);
242-
if (len <= 0) goto failed;
243-
env_len += (len + 1);
244-
245-
env = xrealloc(env, env_len * sizeof(WCHAR));
246-
if (len != MultiByteToWideChar(CP_UTF8, 0, *ptr, -1, env+(env_len-len-1), len)) goto failed;
247-
env[env_len-1] = L'\0';
248-
}
249-
250-
env_len++;
251-
env = xrealloc(env, env_len * sizeof(WCHAR));
252-
env[env_len-1] = L'\0';
253-
return env;
254-
255-
failed:
256-
if (env != NULL) free(env);
257-
return NULL;
258-
}
259-
260236
static WCHAR *to_utf16(char *str) {
261237
int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
262238
if (len <= 0) return NULL;
@@ -373,17 +349,24 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) {
373349
uv_pipe_connect(out_req, io->out, out_name, connect_cb);
374350

375351
PROCESS_INFORMATION pi = {0};
376-
WCHAR *cmdline, *env, *cwd;
352+
WCHAR *cmdline, *cwd;
377353
cmdline = join_args(process->argv);
378354
if (cmdline == NULL) goto cleanup;
379-
env = prep_env(process->envp);
380-
if (env == NULL) goto cleanup;
355+
if (process->envp != NULL) {
356+
char **p = process->envp;
357+
for (; *p; p++) {
358+
WCHAR *env = to_utf16(*p);
359+
if (env == NULL) goto cleanup;
360+
_wputenv(env);
361+
free(env);
362+
}
363+
}
381364
if (process->cwd != NULL) {
382365
cwd = to_utf16(process->cwd);
383366
if (cwd == NULL) goto cleanup;
384367
}
385368

386-
if (!CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, flags, env, cwd, &process->si.StartupInfo, &pi)) {
369+
if (!CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, flags, NULL, cwd, &process->si.StartupInfo, &pi)) {
387370
print_error("CreateProcessW");
388371
goto cleanup;
389372
}
@@ -407,7 +390,6 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) {
407390
if (in_name != NULL) free(in_name);
408391
if (out_name != NULL) free(out_name);
409392
if (cmdline != NULL) free(cmdline);
410-
if (env != NULL) free(env);
411393
if (cwd != NULL) free(cwd);
412394
return status;
413395
}
@@ -458,14 +440,6 @@ static void async_cb(uv_async_t *async) {
458440
process_free(process);
459441
}
460442

461-
static int pty_execvpe(const char *file, char **argv, char **envp) {
462-
char **old = environ;
463-
environ = envp;
464-
int ret = execvp(file, argv);
465-
environ = old;
466-
return ret;
467-
}
468-
469443
int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) {
470444
int status = 0;
471445

@@ -479,13 +453,12 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) {
479453
return status;
480454
} else if (pid == 0) {
481455
setsid();
482-
if (process->cwd != NULL) {
483-
if (chdir(process->cwd) == -1) {
484-
perror("chdir failed\n");
485-
_exit(1);
486-
}
456+
if (process->cwd != NULL) chdir(process->cwd);
457+
if (process->envp != NULL) {
458+
char **p = process->envp;
459+
for (; *p; p++) putenv(*p);
487460
}
488-
int ret = pty_execvpe(process->argv[0], process->argv, process->envp);
461+
int ret = execvp(process->argv[0], process->argv);
489462
if (ret < 0) {
490463
perror("execvp failed\n");
491464
_exit(-errno);

0 commit comments

Comments
 (0)