From 9a62f8d6a6f8148deaea89e73d866439b588babb Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 25 Feb 2025 00:59:31 -0500 Subject: Add $Shell.execute() --- stdlib/shell.c | 10 +++++++--- stdlib/shell.h | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'stdlib') diff --git a/stdlib/shell.c b/stdlib/shell.c index 08898acc..7c54950c 100644 --- a/stdlib/shell.c +++ b/stdlib/shell.c @@ -50,7 +50,7 @@ public OptionalArray_t Shell$run_bytes(Shell_t command) if (len + (size_t)just_read >= capacity) content = GC_REALLOC(content, (capacity *= 2)); - memcpy(&content[len], chunk, (size_t)just_read); + memcpy(content + len, chunk, (size_t)just_read); len += (size_t)just_read; } while (just_read == sizeof(chunk)); @@ -75,10 +75,14 @@ public OptionalText_t Shell$run(Shell_t command) return Text$from_bytes(bytes); } -public int32_t Shell$execute(Shell_t command) +public OptionalInt32_t Shell$execute(Shell_t command) { const char *cmd_str = Text$as_c_string(command); - return system(cmd_str); + int status = system(cmd_str); + if (WIFEXITED(status)) + return (OptionalInt32_t){.i=WEXITSTATUS(status)}; + else + return (OptionalInt32_t){.is_none=true}; } static void _line_reader_cleanup(FILE **f) diff --git a/stdlib/shell.h b/stdlib/shell.h index a8f35f65..500bb043 100644 --- a/stdlib/shell.h +++ b/stdlib/shell.h @@ -21,7 +21,7 @@ Shell_t Shell$escape_text(Text_t text); Shell_t Shell$escape_text_array(Array_t texts); OptionalArray_t Shell$run_bytes(Shell_t command); OptionalText_t Shell$run(Shell_t command); -int32_t Shell$execute(Shell_t command); +OptionalInt32_t Shell$execute(Shell_t command); #define Shell$hash Text$hash #define Shell$compare Text$compare -- cgit v1.2.3