shithub: mc

ref: bef630973910df30c0647daa2c6c8cbae4c6a50e
dir: /lib/std/env+plan9.myr/

View raw version
use sys

use "alloc.use"
use "die.use"
use "extremum.use"
use "fmt.use"
use "option.use"
use "result.use"
use "slcp.use"
use "sldup.use"
use "sleq.use"
use "slpush.use"
use "slurp.use"
use "cstrconv.use"

pkg std =
	const getenv :	(name : byte[:] -> option(byte[:]))
	const getenvv :	(name : byte[:], default : byte[:] -> byte[:])
;;

var envkey 	: byte[:][:]
var envval	: byte[:][:]

const envfind = {key
	for var i = 0; i < envkey.len; i++
		if std.sleq(envkey[i], key)
			-> `Some envval[i]
		;;
	;;
	-> `None
}

const getenv = {name
	var buf : byte[128]
	var s

	match envfind(name)
	| `Some val:	-> `Some val
	| `None:
		s = bfmt(buf[:], "/env/{}", name)
		match std.slurp(s)
		| `Fail m: -> `None
		| `Ok data:
			data = cstrconv(data)
			envkey = slpush(envkey, sldup(data))
			envval = slpush(envval, data)
			-> `Some data
		;;
	;;
}

const getenvv = {name, default
	match getenv(name)
	| `Some val:	-> val
	| `None:	-> default
	;;
}