aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-04-17 14:57:09 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-04-17 14:57:09 -0400
commit8c97575c8fb93671e4ac51fa8232a1a2cfb7ad22 (patch)
treef65add361a5799158a60a89c9fe53c86b603a288 /src
parent9ee87d57670ef0bdf360bdb99e71ef278d770755 (diff)
Add getenv()/setenv()
Diffstat (limited to 'src')
-rw-r--r--src/environment.c2
-rw-r--r--src/stdlib/stdlib.c11
-rw-r--r--src/stdlib/stdlib.h2
3 files changed, 15 insertions, 0 deletions
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