Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CWD option to subprocess_create_ex #83

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 15 additions & 3 deletions subprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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.
Expand Down Expand Up @@ -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;
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,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);
Expand Down Expand Up @@ -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 */
22 changes: 15 additions & 7 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -789,15 +791,19 @@ 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) {
const char *const commandLine[] = {"./process_return_zero", 0};
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));
Expand All @@ -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));

Expand All @@ -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));

Expand All @@ -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));
Expand All @@ -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));

Expand Down
Loading