aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/syscall/env_wasip2.go
blob: 8064d0d2810f1837e6eace785f103bca7a55d57f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//go:build wasip2

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 {
		env = append(env, k+"="+v)
	}
	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)
}