From 01602578fdf19a2a0a7a833db004596a6364d02c Mon Sep 17 00:00:00 2001 From: Riley King-Saunders Date: Tue, 4 Mar 2025 13:26:30 +1100 Subject: [PATCH] Set struct default initialization values Modified uniproc_create_process signature to take a command and a list of arguments Updated uniproc_win64.c to terminate the process before closing the handles Updated uniproc_win64.c to convert the command and list of arguments into a command-line string --- include/uniproc.h | 10 +++++----- src/uniproc_win64.c | 22 +++++++++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/include/uniproc.h b/include/uniproc.h index 5b35ed3..ab3a58e 100644 --- a/include/uniproc.h +++ b/include/uniproc.h @@ -4,15 +4,15 @@ #include #include typedef struct { - FILE* in; - FILE* out; - FILE* err; + FILE* in = NULL; + FILE* out= NULL; + FILE* err= NULL; // OS-dependent handle to the process - uint32_t _proc_hdl; + uint32_t _proc_hdl = -1; } uniproc_process; -uniproc_process uniproc_create_process(const char* cmd); +uniproc_process uniproc_create_process(const char* cmd, const size_t num_args, const char** argv); void uniproc_await_processes(uniproc_process* p, int* return_codes, size_t num_processes); void uniproc_close_process(uniproc_process* p); diff --git a/src/uniproc_win64.c b/src/uniproc_win64.c index 5054c4e..5eb5f68 100644 --- a/src/uniproc_win64.c +++ b/src/uniproc_win64.c @@ -4,7 +4,7 @@ #include #include -uniproc_process uniproc_create_process(const char* cmd) +uniproc_process uniproc_create_process(const char* cmd, const size_t num_args, const char** argv) { // Create pipes HANDLE child_in; @@ -38,9 +38,21 @@ uniproc_process uniproc_create_process(const char* cmd) // Create child process uniproc_process p; - size_t cmd_len = strlen(cmd); + // Create p_cmd by appending all argv's to cmd + size_t x = strlen(cmd); + size_t cmd_len = x + 1; + for (size_t i = 0; i < num_args; ++i) + cmd_len += strlen(argv[i]) + 1; char* p_cmd = malloc((cmd_len+1) * sizeof(char)); - memcpy(p_cmd, cmd, cmd_len * sizeof(char)); + memcpy(p_cmd, cmd, x * sizeof(char)); + x += 1; // Account for space after cmd + for (size_t i = 0; i < num_args; ++i) + { + size_t arglen = strlen(argv[i]); + memcpy(p_cmd + x, argv[i], arglen); + p_cmd[x + arglen] = ' '; + x += arglen + 1 + } p_cmd[cmd_len] = '\0'; PROCESS_INFORMATION p_info; @@ -100,11 +112,11 @@ void uniproc_await_processes(uniproc_process* p, int* return_codes, size_t num_p void uniproc_close_process(uniproc_process* p) { + TerminateProcess((HANDLE)p->_proc_hdl, 2); + CloseHandle((HANDLE)p->_proc_hdl); fclose(p->in); fclose(p->out); fclose(p->err); - - CloseHandle((HANDLE)p->_proc_hdl); } #endif \ No newline at end of file