From b07db61938359c1c1cabcb6efe574377924d8e15 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sun, 1 Oct 2023 10:40:42 -0500 Subject: [PATCH] Add Mac implementation of oc_run_cmd so much easier than windows, oh my goodness --- src/orca.c | 1 + src/platform/osx_subprocess.c | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/platform/osx_subprocess.c diff --git a/src/orca.c b/src/orca.c index 0c7db8d..8382efb 100644 --- a/src/orca.c +++ b/src/orca.c @@ -29,6 +29,7 @@ #include "platform/posix_io.c" #include "platform/posix_thread.c" #include "platform/osx_platform.c" + #include "platform/osx_subprocess.c" /* #include"platform/unix_rng.c" diff --git a/src/platform/osx_subprocess.c b/src/platform/osx_subprocess.c new file mode 100644 index 0000000..11df03a --- /dev/null +++ b/src/platform/osx_subprocess.c @@ -0,0 +1,60 @@ +/************************************************************************* +* +* Orca +* Copyright 2023 Martin Fouilleul and the Orca project contributors +* See LICENSE.txt for licensing information +* +**************************************************************************/ +#include "platform_subprocess.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +oc_str8 oc_run_cmd(oc_arena* arena, oc_str8 cmd) +{ + const char* cmdC = oc_str8_to_cstring(arena, cmd); + FILE* p = popen(cmdC, "r+"); + if(!p) + { + oc_log_error("failed to launch process"); + return OC_STR8(""); + } + + oc_str8_list chunks = { 0 }; + char buffer[1024]; + while(true) + { + ssize_t nRead = read(fileno(p), buffer, sizeof(buffer)); + if(nRead < 0) + { + oc_log_error("failed to read from process's stdout\n"); + return OC_STR8(""); + } + if(nRead == 0) + { + break; + } + + oc_str8 chunk = oc_str8_from_buffer(nRead, buffer); + oc_str8_list_push(arena, &chunks, oc_str8_push_copy(arena, chunk)); + } + + int status = pclose(p); + if(status == -1) + { + oc_log_warning("failed to close subprocess\n"); + } + else if(!WIFEXITED(status)) + { + oc_log_warning("subprocess did not exit correctly\n"); + } + + return oc_str8_list_join(arena, chunks); +} + +#ifdef __cplusplus +} // extern "C" +#endif