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 initialization by copyenv, which populates env.
18 // envLock guards env.
21 // env maps from an environment variable to its value.
25 func readenv(key string) (string, error) {
26 fd, err := Open("/env/"+key, O_RDONLY)
31 l, _ := Seek(fd, 0, 2)
33 buf := make([]byte, l)
34 n, err := Read(fd, buf)
38 if n > 0 && buf[n-1] == 0 {
41 return string(buf), nil
44 func writeenv(key, value string) error {
45 fd, err := Create("/env/"+key, O_RDWR, 0666)
50 _, err = Write(fd, []byte(value))
55 env = make(map[string]string)
56 fd, err := Open("/env", O_RDONLY)
61 files, err := readdirnames(fd)
65 for _, key := range files {
66 v, err := readenv(key)
74 func Getenv(key string) (value string, found bool) {
81 defer envLock.RUnlock()
90 func Setenv(key, value string) error {
93 return errors.New("zero length key")
97 defer envLock.Unlock()
99 err := writeenv(key, value)
108 envOnce.Do(copyenv) // prevent copyenv in Getenv/Setenv
111 defer envLock.Unlock()
113 env = make(map[string]string)
114 RawSyscall(SYS_RFORK, RFCENVG, 0, 0)
117 func Environ() []string {
120 defer envLock.RUnlock()
121 a := make([]string, len(env))
123 for k, v := range env {