Commit 11f12da0 authored by Rob Pike's avatar Rob Pike

go.sys/unix: implement the environment functions by wrapping syscall

The environment is global state that is owned by the standard syscall package.
With this change, go test passes on darwin in the unix directory.

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/129900043
parent 279b3782
...@@ -8,112 +8,20 @@ ...@@ -8,112 +8,20 @@
package unix package unix
import "sync" import "syscall"
var (
// envOnce guards initialization by copyenv, which populates env.
envOnce sync.Once
// envLock guards env and envs.
envLock sync.RWMutex
// env maps from an environment variable to its first occurrence in envs.
env map[string]int
// envs is provided by the runtime. elements are expected to be
// of the form "key=value".
envs []string
)
// setenv_c is provided by the runtime, but is a no-op if cgo isn't
// loaded.
func setenv_c(k, v string)
func copyenv() {
env = make(map[string]int)
for i, s := range envs {
for j := 0; j < len(s); j++ {
if s[j] == '=' {
key := s[:j]
if _, ok := env[key]; !ok {
env[key] = i
}
break
}
}
}
}
func Getenv(key string) (value string, found bool) { func Getenv(key string) (value string, found bool) {
envOnce.Do(copyenv) return syscall.Getenv(key)
if len(key) == 0 {
return "", false
}
envLock.RLock()
defer envLock.RUnlock()
i, ok := env[key]
if !ok {
return "", false
}
s := envs[i]
for i := 0; i < len(s); i++ {
if s[i] == '=' {
return s[i+1:], true
}
}
return "", false
} }
func Setenv(key, value string) error { func Setenv(key, value string) error {
envOnce.Do(copyenv) return syscall.Setenv(key, value)
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(value); i++ {
if value[i] == 0 {
return EINVAL
}
}
envLock.Lock()
defer envLock.Unlock()
i, ok := env[key]
kv := key + "=" + value
if ok {
envs[i] = kv
} else {
i = len(envs)
envs = append(envs, kv)
}
env[key] = i
setenv_c(key, value)
return nil
} }
func Clearenv() { func Clearenv() {
envOnce.Do(copyenv) // prevent copyenv in Getenv/Setenv syscall.Clearenv()
envLock.Lock()
defer envLock.Unlock()
env = make(map[string]int)
envs = []string{}
// TODO(bradfitz): pass through to C
} }
func Environ() []string { func Environ() []string {
envOnce.Do(copyenv) return syscall.Environ()
envLock.RLock()
defer envLock.RUnlock()
a := make([]string, len(envs))
copy(a, envs)
return a
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment