From 8c97575c8fb93671e4ac51fa8232a1a2cfb7ad22 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 17 Apr 2025 14:57:09 -0400 Subject: Add getenv()/setenv() --- src/environment.c | 2 ++ src/stdlib/stdlib.c | 11 +++++++++++ src/stdlib/stdlib.h | 2 ++ 3 files changed, 15 insertions(+) (limited to 'src') diff --git a/src/environment.c b/src/environment.c index bf012286..35c76fe5 100644 --- a/src/environment.c +++ b/src/environment.c @@ -518,6 +518,8 @@ env_t *global_env(bool source_mapping) {"USE_COLOR", "USE_COLOR", "Bool"}, {"say", "say", "func(text:Text, newline=yes)"}, {"print", "say", "func(text:Text, newline=yes)"}, + {"getenv", "getenv_text", "func(name:Text -> Text?)"}, + {"setenv", "setenv_text", "func(name:Text, value:Text -> Text?)"}, {"ask", "ask", "func(prompt:Text, bold=yes, force_tty=yes -> Text?)"}, {"exit", "tomo_exit", "func(message:Text?=none, code=Int32(1) -> Abort)"}, {"fail", "fail_text", "func(message:Text -> Abort)"}, diff --git a/src/stdlib/stdlib.c b/src/stdlib/stdlib.c index 88553f54..a7eeb745 100644 --- a/src/stdlib/stdlib.c +++ b/src/stdlib/stdlib.c @@ -634,4 +634,15 @@ public void sleep_num(double seconds) nanosleep(&ts, NULL); } +public OptionalText_t getenv_text(Text_t name) +{ + const char *val = getenv(Text$as_c_string(name)); + return val ? Text$from_str(val) : NONE_TEXT; +} + +public void setenv_text(Text_t name, Text_t value) +{ + setenv(Text$as_c_string(name), Text$as_c_string(value), 1); +} + // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/src/stdlib/stdlib.h b/src/stdlib/stdlib.h index a452a04b..c4bf83e0 100644 --- a/src/stdlib/stdlib.h +++ b/src/stdlib/stdlib.h @@ -93,5 +93,7 @@ _Noreturn void tomo_exit(Text_t text, int32_t status); Closure_t spawn(Closure_t fn); bool pop_flag(char **argv, int *i, const char *flag, Text_t *result); void sleep_num(double seconds); +OptionalText_t getenv_text(Text_t name); +void setenv_text(Text_t name, Text_t value); // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 -- cgit v1.2.3