Added syncing and working directory param to execute

This commit is contained in:
2024-02-21 17:41:12 +01:00
parent 839b6ddabc
commit 6f47b7f274
6 changed files with 65 additions and 8 deletions

View File

@@ -222,7 +222,7 @@ result_t parse_size(uint64_t* _size, const char* str) {
}
}
result_t execute(int* _exit_code, char** _stdoutbuf, char** _stderrbuf, const char* executable, ...) {
result_t execute(int* _exit_code, char** _stdoutbuf, char** _stderrbuf, const char* working_directory, const char* executable, ...) {
// Initialize the output parameters
if (_exit_code != NULL)
*_exit_code = 0;
@@ -325,6 +325,15 @@ result_t execute(int* _exit_code, char** _stdoutbuf, char** _stderrbuf, const ch
close(stdout_pipe[1]);
close(stderr_pipe[1]);
// Change the working directory of the child process
if (working_directory != NULL) {
errno = 0;
if (chdir(working_directory) < 0) {
fprintf(stderr, "Failed to change the working directory of the child process (%s).\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
// Execute the child process
errno = 0;
execvp(executable, argv);