mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-15 00:13:43 +00:00
syscall: refactor environment handling
* Move environment functions to their own files.
* Rewrite the WASIp2 version of environment variables to be much
simpler (don't go through C functions).
This commit is contained in:
@@ -2,6 +2,19 @@
|
||||
|
||||
package syscall
|
||||
|
||||
import (
|
||||
"internal/wasi/cli/v0.2.0/environment"
|
||||
)
|
||||
|
||||
var libc_envs map[string]string
|
||||
|
||||
func populateEnvironment() {
|
||||
libc_envs = make(map[string]string)
|
||||
for _, kv := range environment.GetEnvironment().Slice() {
|
||||
libc_envs[kv[0]] = kv[1]
|
||||
}
|
||||
}
|
||||
|
||||
func Environ() []string {
|
||||
var env []string
|
||||
for k, v := range libc_envs {
|
||||
@@ -9,3 +22,35 @@ func Environ() []string {
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
func Getenv(key string) (value string, found bool) {
|
||||
value, found = libc_envs[key]
|
||||
return
|
||||
}
|
||||
|
||||
func Setenv(key, val string) (err error) {
|
||||
if len(key) == 0 {
|
||||
return EINVAL
|
||||
}
|
||||
for i := 0; i < len(key); i++ {
|
||||
if key[i] == '=' || key[i] == 0 {
|
||||
return EINVAL
|
||||
}
|
||||
}
|
||||
for i := 0; i < len(val); i++ {
|
||||
if val[i] == 0 {
|
||||
return EINVAL
|
||||
}
|
||||
}
|
||||
libc_envs[key] = val
|
||||
return nil
|
||||
}
|
||||
|
||||
func Unsetenv(key string) (err error) {
|
||||
delete(libc_envs, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Clearenv() {
|
||||
clear(libc_envs)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user