diff --git a/subprocess.h b/subprocess.h index a98fc75..e3ba7c4 100644 --- a/subprocess.h +++ b/subprocess.h @@ -117,6 +117,8 @@ subprocess_weak int subprocess_create(const char *const command_line[], /// @param environment An optional array of strings for the environment to use /// for a child process (each element of the form FOO=BAR). The last element /// must be NULL to signify the end of the array. +/// @param process_cwd The current working directory of the newly created +/// process. If NULL, will be the same as the parent process. /// @param out_process The newly created process. /// @return On success zero is returned. /// @@ -125,6 +127,7 @@ subprocess_weak int subprocess_create(const char *const command_line[], subprocess_weak int subprocess_create_ex(const char *const command_line[], int options, const char *const environment[], + const char *const process_cwd, struct subprocess_s *const out_process); /// @brief Get the standard input file for a process. @@ -482,11 +485,12 @@ int subprocess_create_named_pipe_helper(void **rd, void **wr) { int subprocess_create(const char *const commandLine[], int options, struct subprocess_s *const out_process) { return subprocess_create_ex(commandLine, options, SUBPROCESS_NULL, - out_process); + SUBPROCESS_NULL, out_process); } int subprocess_create_ex(const char *const commandLine[], int options, const char *const environment[], + const char *const process_cwd, struct subprocess_s *const out_process) { #if defined(_WIN32) int fd; @@ -741,7 +745,7 @@ int subprocess_create_ex(const char *const commandLine[], int options, 1, // handles are inherited flags, // creation flags used_environment, // used environment - SUBPROCESS_NULL, // use parent's current directory + process_cwd, // use specified current directory SUBPROCESS_PTR_CAST(LPSTARTUPINFOA, &startInfo), // STARTUPINFO pointer SUBPROCESS_PTR_CAST(LPPROCESS_INFORMATION, &processInfo))) { @@ -819,6 +823,14 @@ int subprocess_create_ex(const char *const commandLine[], int options, return -1; } + // Set working directory + if (process_cwd) { + if (0 != posix_spawn_file_actions_addchdir_np(&actions, process_cwd)) { + posix_spawn_file_actions_destroy(&actions); + return -1; + } + } + // Close the stdin write end if (0 != posix_spawn_file_actions_addclose(&actions, stdinfd[1])) { posix_spawn_file_actions_destroy(&actions); @@ -1196,4 +1208,4 @@ int subprocess_alive(struct subprocess_s *const process) { } // extern "C" #endif -#endif /* SHEREDOM_SUBPROCESS_H_INCLUDED */ +#endif /* SHEREDOM_SUBPROCESS_H_INCLUDED */ \ No newline at end of file diff --git a/test/main.c b/test/main.c index 788adfb..a3bf0c7 100644 --- a/test/main.c +++ b/test/main.c @@ -779,7 +779,9 @@ UTEST(environment, illegal_inherit_environment) { ASSERT_NE(0, subprocess_create_ex(commandLine, subprocess_option_inherit_environment, - environment, &process)); + environment, + SUBPROCESS_NULL, + &process)); } UTEST(environment, illegal_empty_environment_with_inherit_environment) { @@ -789,7 +791,9 @@ UTEST(environment, illegal_empty_environment_with_inherit_environment) { ASSERT_NE(0, subprocess_create_ex(commandLine, subprocess_option_inherit_environment, - environment, &process)); + environment, + SUBPROCESS_NULL, + &process)); } UTEST(environment, null_environment_with_inherit_environment) { @@ -797,7 +801,9 @@ UTEST(environment, null_environment_with_inherit_environment) { struct subprocess_s process; ASSERT_EQ(0, subprocess_create_ex(commandLine, - subprocess_option_inherit_environment, 0, + subprocess_option_inherit_environment, + 0, + SUBPROCESS_NULL, &process)); ASSERT_EQ(0, subprocess_destroy(&process)); @@ -809,7 +815,7 @@ UTEST(environment, specify_environment) { struct subprocess_s process; int ret = -1; - ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, &process)); + ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, SUBPROCESS_NULL, &process)); ASSERT_EQ(0, subprocess_join(&process, &ret)); @@ -825,7 +831,7 @@ UTEST(executable_resolve, no_slashes_with_environment) { struct subprocess_s process; int ret = -1; - ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, &process)); + ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, SUBPROCESS_NULL, &process)); ASSERT_EQ(0, subprocess_join(&process, &ret)); @@ -842,7 +848,9 @@ UTEST(executable_resolve, no_slashes_with_inherit) { int ret = -1; ASSERT_EQ(0, subprocess_create_ex(commandLine, - subprocess_option_inherit_environment, 0, + subprocess_option_inherit_environment, + 0, + SUBPROCESS_NULL, &process)); ASSERT_EQ(0, subprocess_join(&process, &ret)); @@ -866,7 +874,7 @@ UTEST(executable_resolve, custom_search_path) { snprintf(path_var, sizeof(path_var), "PATH=%s", current_path); environment[0] = path_var; - ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, &process)); + ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, SUBPROCESS_NULL, &process)); ASSERT_EQ(0, subprocess_join(&process, &ret));