summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorZuhaitz <zuhaitz.zechhub@gmail.com>2026-01-22 23:30:21 +0000
committerGitHub <noreply@github.com>2026-01-22 23:30:21 +0000
commit3a4a72a38675893c3a1854d05c72b957a6bd9364 (patch)
tree91c79a71830c72f8cedfa2b051e4bab4037dc5a4 /examples
parent03df9c337355dc45a14e2c2fbe3276dfe890c68d (diff)
parent2a4edc7f2c9241ec7845cecadef850a1db45b3dd (diff)
Merge pull request #97 from Burnett01/feat/std-env-for-environment-variables
feat: New std library "env" for accessing the process environment
Diffstat (limited to 'examples')
-rw-r--r--examples/process/env.zc49
1 files changed, 49 insertions, 0 deletions
diff --git a/examples/process/env.zc b/examples/process/env.zc
new file mode 100644
index 0000000..1506229
--- /dev/null
+++ b/examples/process/env.zc
@@ -0,0 +1,49 @@
+//> cflags: -Ofast
+
+import "std/env.zc"
+
+fn main() -> int {
+ // ----
+ // get: Retrieves the env-var as borrowed string (char *) (no alloc)
+ // ---
+ // @Example usage: On Linux (and some variants) PATH is already defined.
+ // ---
+ var path = Env::get("PATH");
+
+ if (path.is_some()) {
+ println "PATH is: {path.unwrap()}";
+ } else {
+ println "PATH is not set";
+ }
+
+ // ----
+ // set: Sets an env-var variable
+ // get_dup: Retrieves the env-var as caller-owned String() (heap alloc)
+ // ---
+ // @Example usage: In your terminal type "export HELLO=world" or use Env::set()
+ // ---
+ var res = Env::set("HELLO", "world");
+ //=> check res against EnvRes::OK()
+
+ var hello = Env::get_dup("HELLO");
+
+ if (hello.is_some()) {
+ var hello_str = hello.unwrap();
+ defer hello_str.free();
+
+ println "HELLO is: {hello_str.c_str()}";
+ } else {
+ println "HELLO is not set";
+ }
+
+ // ----
+ // unset: Unsets an existing env-var
+ // ---
+ Env::unset("HELLO");
+
+ var hello_now2 = Env::get("HELLO");
+
+ if (hello_now2.is_none()) {
+ println "HELLO is now unset";
+ }
+}