ref: 275c62e9be90ff03a115bce70a014b3975c637ce
parent: 8c548eb4584df2e4ba12124491628397f74d4b44
author: Ori Bernstein <[email protected]>
date: Mon Dec 29 18:12:45 EST 2014
Implement getenv on plan9.
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -56,6 +56,7 @@
slurp.myr \
sort.myr \
spork.myr \
+ cstrconv.myr \
strfind.myr \
strjoin.myr \
strsplit.myr \
--- a/libstd/env+plan9.myr
+++ b/libstd/env+plan9.myr
@@ -1,8 +1,16 @@
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"
pkg std =
const getenv : (name : byte[:] -> option(byte[:]))
@@ -9,10 +17,38 @@
const getenvv : (name : byte[:], default : byte[:] -> byte[:])
;;
-const getenv = {name
+var envkey : byte[:][:]
+var envval : byte[:][:]
+
+const envfind = {key
+ var i
+ for i = 0; i < envkey.len; i++
+ if std.sleq(envkey[i], key)
+ -> `Some envval[i]
+ ;;
+ ;;
-> `None
}
+const getenv = {name
+ var buf : byte[128]
+ match envfind(name)
+ | `Some val: -> `Some val
+ | `None:
+ bfmt(buf[:], "/env/%s", name)
+ match std.slurp(buf[:])
+ | `Fail m: -> `None
+ | `Ok data:
+ slpush(envkey, sldup(name))
+ slpush(envval, data)
+ -> `Some data
+ ;;
+ ;;
+}
+
const getenvv = {name, default
- -> default
+ match getenv(name)
+ | `Some val: -> val
+ | `None: -> default
+ ;;
}
--- a/libstd/syswrap+posixy.myr
+++ b/libstd/syswrap+posixy.myr
@@ -1,5 +1,6 @@
use sys
use "types.use"
+use "strconv.use"
use "option.use"
pkg std =
@@ -8,6 +9,14 @@
type fdopt = sys.fdopt
type whence = sys.whence
+ type sysinfo = struct
+ system : byte[:]
+ version : byte[:]
+ release : byte[:]
+ arch : byte[:]
+ uname : sys.utsname /* storage */
+ ;;
+
const Failmem : byte# = -1 castto(byte#)
const Seekset : whence = sys.Seekset castto(whence)
@@ -38,6 +47,7 @@
const fsize : (f : byte[:] -> option(off))
/* useful/portable bits of uname */
+ const getsysinfo : (si : sysinfo# -> void)
/* path manipulation */
const mkdir : (path : byte[:], mode : int64 -> int64)
@@ -71,6 +81,17 @@
/* path manipulation */
const mkdir = {path, mode; -> sys.mkdir(path, mode)}
const remove = {path; -> sys.unlink(path) == 0}
+
+/* useful/portable bits of uname */
+const getsysinfo = {si
+ var si
+
+ sys.uname(&si)
+ si.system = cstrconv(si.uname.sysname[:])
+ si.version = cstrconv(si.uname.version[:])
+ si.relsease = cstrconv(si.uname.release[:])
+ si.arch = cstconv(si.uname.machine[:])
+}
/* process stuff */
const getpid = {; -> sys.getpid() castto(pid)}