ref: 8a23ecf0ceb8172e779d2557bd797f6c562a7d88
parent: a74746cf0a22bd451ae9a57f350cbf1c0d7fd3f9
author: Ori Bernstein <[email protected]>
date: Tue Aug 27 08:00:44 EDT 2013
Add non-broken getenv() function
--- a/libstd/env.myr
+++ b/libstd/env.myr
@@ -1,6 +1,22 @@
use "sleq.use"
+use "extremum.use"
pkg std =
extern var _environment : byte[:][:]
+
+ const getenv : (name : byte[:] -> [byte[:], bool]) /* FIXME: should return an option, not a tuple, but we have a compiler bug. */
;;
+
+const getenv = {name
+ var i
+
+ var n
+ for i = 0; i < _environment.len; i++
+ n = min(name.len, _environment[i].len)
+ if sleq(name, _environment[i][:n]) && sleq(_environment[i][n:n+1], "=")
+ -> (_environment[i][n+1:], true)
+ ;;
+ ;;
+ -> ("", false)
+}
--- a/libstd/test.myr
+++ b/libstd/test.myr
@@ -31,6 +31,7 @@
for i = 0; i < ctx.args.len; i++
std.put("arg %s\n", ctx.args[i])
;;
+ printenv("SHELL")
/* try the byte allocator for large variety of sizes. */
@@ -100,4 +101,16 @@
std.put("Pokes a bit at the standard library.\n")
std.put("Option string is asdf:g?h\n")
std.exit(0)
+}
+
+const printenv = {name
+ var env
+ var ok
+
+ (env, ok) = std.getenv(name)
+ if !ok
+ std.put("No env var %s is set\n", name)
+ else
+ std.put("Value of %s is %s\n", name, env)
+ ;;
}