2012-03-27 23:13:14 +00:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
// Unix environment variables.
|
|
|
|
|
|
|
|
package syscall
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
// envs is provided by the runtime. elements are expected to
|
|
|
|
// be of the form "key=value". An empty string means deleted
|
|
|
|
// (or a duplicate to be ignored).
|
|
|
|
envs []string = runtime_envs()
|
2012-03-27 23:13:14 +00:00
|
|
|
)
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
func runtime_envs() []string // in package runtime
|
|
|
|
|
|
|
|
// setenv_c and unsetenv_c are provided by the runtime but are no-ops
|
|
|
|
// if cgo isn't loaded.
|
2012-03-27 23:13:14 +00:00
|
|
|
func setenv_c(k, v string)
|
2015-08-28 15:33:40 +00:00
|
|
|
func unsetenv_c(k string)
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
func copyenv() {
|
|
|
|
env = make(map[string]int)
|
2015-08-28 15:33:40 +00:00
|
|
|
for i, s := range envs {
|
2012-03-27 23:13:14 +00:00
|
|
|
for j := 0; j < len(s); j++ {
|
|
|
|
if s[j] == '=' {
|
|
|
|
key := s[:j]
|
|
|
|
if _, ok := env[key]; !ok {
|
2015-08-28 15:33:40 +00:00
|
|
|
env[key] = i // first mention of key
|
|
|
|
} else {
|
|
|
|
// Clear duplicate keys. This permits Unsetenv to
|
|
|
|
// safely delete only the first item without
|
|
|
|
// worrying about unshadowing a later one,
|
|
|
|
// which might be a security problem.
|
|
|
|
envs[i] = ""
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
func Unsetenv(key string) error {
|
|
|
|
envOnce.Do(copyenv)
|
|
|
|
|
|
|
|
envLock.Lock()
|
|
|
|
defer envLock.Unlock()
|
|
|
|
|
|
|
|
if i, ok := env[key]; ok {
|
|
|
|
envs[i] = ""
|
|
|
|
delete(env, key)
|
|
|
|
}
|
|
|
|
unsetenv_c(key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
func Getenv(key string) (value string, found bool) {
|
|
|
|
envOnce.Do(copyenv)
|
|
|
|
if len(key) == 0 {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
envLock.RLock()
|
|
|
|
defer envLock.RUnlock()
|
|
|
|
|
|
|
|
i, ok := env[key]
|
|
|
|
if !ok {
|
|
|
|
return "", false
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
s := envs[i]
|
2012-03-27 23:13:14 +00:00
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if s[i] == '=' {
|
|
|
|
return s[i+1:], true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func Setenv(key, value string) error {
|
|
|
|
envOnce.Do(copyenv)
|
|
|
|
if len(key) == 0 {
|
|
|
|
return EINVAL
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
envLock.Lock()
|
|
|
|
defer envLock.Unlock()
|
|
|
|
|
|
|
|
i, ok := env[key]
|
|
|
|
kv := key + "=" + value
|
|
|
|
if ok {
|
2015-08-28 15:33:40 +00:00
|
|
|
envs[i] = kv
|
2012-03-27 23:13:14 +00:00
|
|
|
} else {
|
2015-08-28 15:33:40 +00:00
|
|
|
i = len(envs)
|
|
|
|
envs = append(envs, kv)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
env[key] = i
|
|
|
|
setenv_c(key, value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Clearenv() {
|
|
|
|
envOnce.Do(copyenv) // prevent copyenv in Getenv/Setenv
|
|
|
|
|
|
|
|
envLock.Lock()
|
|
|
|
defer envLock.Unlock()
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
for k := range env {
|
|
|
|
unsetenv_c(k)
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
env = make(map[string]int)
|
2015-08-28 15:33:40 +00:00
|
|
|
envs = []string{}
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Environ() []string {
|
|
|
|
envOnce.Do(copyenv)
|
|
|
|
envLock.RLock()
|
|
|
|
defer envLock.RUnlock()
|
2015-08-28 15:33:40 +00:00
|
|
|
a := make([]string, 0, len(envs))
|
|
|
|
for _, env := range envs {
|
|
|
|
if env != "" {
|
|
|
|
a = append(a, env)
|
|
|
|
}
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
return a
|
|
|
|
}
|