1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Plan 9 environment variables.
15 // envOnce guards copyenv, which populates env.
18 // envLock guards env.
21 // env maps from an environment variable to its value.
22 env = make(map[string]string)
24 errZeroLengthKey = errors.New("zero length key")
25 errShortWrite = errors.New("i/o count too small")
28 func readenv(key string) (string, error) {
29 fd, err := Open("/env/"+key, O_RDONLY)
34 l, _ := Seek(fd, 0, 2)
36 buf := make([]byte, l)
37 n, err := Read(fd, buf)
41 if n > 0 && buf[n-1] == 0 {
44 return string(buf), nil
47 func writeenv(key, value string) error {
48 fd, err := Create("/env/"+key, O_RDWR, 0666)
54 n, err := Write(fd, b)
65 fd, err := Open("/env", O_RDONLY)
70 files, err := readdirnames(fd)
74 for _, key := range files {
75 v, err := readenv(key)
83 func Getenv(key string) (value string, found bool) {
89 defer envLock.RUnlock()
91 if v, ok := env[key]; ok {
94 v, err := readenv(key)
102 func Setenv(key, value string) error {
104 return errZeroLengthKey
108 defer envLock.Unlock()
110 err := writeenv(key, value)
120 defer envLock.Unlock()
122 env = make(map[string]string)
123 RawSyscall(SYS_RFORK, RFCENVG, 0, 0)
126 func Environ() []string {
128 defer envLock.RUnlock()
131 a := make([]string, len(env))
133 for k, v := range env {