Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions subprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,17 @@ subprocess_weak int subprocess_create(const char *const command_line[],
/// 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 out_process The newly created process.
/// @param process_cwd The CWD of the newly created process. If null, will be
/// the same as the parent process.
/// @return On success zero is returned.
///
/// If `options` contains `subprocess_option_inherit_environment`, then
/// `environment` must be NULL.
subprocess_weak int
subprocess_create_ex(const char *const command_line[], int options,
const char *const environment[],
struct subprocess_s *const out_process);
struct subprocess_s *const out_process,
const char *const process_cwd);

/// @brief Get the standard input file for a process.
/// @param process The process to query.
Expand Down Expand Up @@ -482,12 +485,13 @@ 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);
out_process, SUBPROCESS_NULL);
}

int subprocess_create_ex(const char *const commandLine[], int options,
const char *const environment[],
struct subprocess_s *const out_process) {
struct subprocess_s *const out_process,
const char *const process_cwd) {
#if defined(_WIN32)
int fd;
void *rd, *wr;
Expand Down Expand Up @@ -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))) {
Expand Down Expand Up @@ -819,6 +823,12 @@ int subprocess_create_ex(const char *const commandLine[], int options,
return -1;
}

// Set working directory
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);
Expand Down Expand Up @@ -1196,4 +1206,4 @@ int subprocess_alive(struct subprocess_s *const process) {
} // extern "C"
#endif

#endif /* SHEREDOM_SUBPROCESS_H_INCLUDED */
#endif /* SHEREDOM_SUBPROCESS_H_INCLUDED */
18 changes: 11 additions & 7 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,8 @@ UTEST(environment, illegal_inherit_environment) {

ASSERT_NE(0, subprocess_create_ex(commandLine,
subprocess_option_inherit_environment,
environment, &process));
environment, &process,
SUBPROCESS_NULL));
}

UTEST(environment, illegal_empty_environment_with_inherit_environment) {
Expand All @@ -789,7 +790,8 @@ UTEST(environment, illegal_empty_environment_with_inherit_environment) {

ASSERT_NE(0, subprocess_create_ex(commandLine,
subprocess_option_inherit_environment,
environment, &process));
environment, &process,
SUBPROCESS_NULL));
}

UTEST(environment, null_environment_with_inherit_environment) {
Expand All @@ -798,7 +800,8 @@ UTEST(environment, null_environment_with_inherit_environment) {

ASSERT_EQ(0, subprocess_create_ex(commandLine,
subprocess_option_inherit_environment, 0,
&process));
&process,
SUBPROCESS_NULL));

ASSERT_EQ(0, subprocess_destroy(&process));
}
Expand All @@ -809,7 +812,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, &process, SUBPROCESS_NULL));

ASSERT_EQ(0, subprocess_join(&process, &ret));

Expand All @@ -825,7 +828,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, &process, SUBPROCESS_NULL));

ASSERT_EQ(0, subprocess_join(&process, &ret));

Expand All @@ -843,7 +846,8 @@ UTEST(executable_resolve, no_slashes_with_inherit) {

ASSERT_EQ(0, subprocess_create_ex(commandLine,
subprocess_option_inherit_environment, 0,
&process));
&process,
SUBPROCESS_NULL));

ASSERT_EQ(0, subprocess_join(&process, &ret));

Expand All @@ -866,7 +870,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, &process, SUBPROCESS_NULL));

ASSERT_EQ(0, subprocess_join(&process, &ret));

Expand Down