env_unix.go 2.77 KB
Newer Older
1 2 3 4
// 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.

5
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
6 7 8 9 10 11 12

// Unix environment variables.

package syscall

import "sync"

13 14 15
var (
	// envOnce guards initialization by copyenv, which populates env.
	envOnce sync.Once
16

17 18 19 20 21 22
	// envLock guards env and envs.
	envLock sync.RWMutex

	// env maps from an environment variable to its first occurrence in envs.
	env map[string]int

23 24 25 26
	// 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()
27 28
)

29 30 31 32
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.
33
func setenv_c(k, v string)
34
func unsetenv_c(k string)
35 36

func copyenv() {
37
	env = make(map[string]int)
38
	for i, s := range envs {
39 40
		for j := 0; j < len(s); j++ {
			if s[j] == '=' {
41 42
				key := s[:j]
				if _, ok := env[key]; !ok {
43 44 45 46 47 48 49
					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] = ""
50
				}
51 52 53 54 55 56
				break
			}
		}
	}
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70
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
}

71 72 73 74 75 76 77 78 79
func Getenv(key string) (value string, found bool) {
	envOnce.Do(copyenv)
	if len(key) == 0 {
		return "", false
	}

	envLock.RLock()
	defer envLock.RUnlock()

80
	i, ok := env[key]
81 82 83
	if !ok {
		return "", false
	}
84
	s := envs[i]
85 86 87 88 89 90
	for i := 0; i < len(s); i++ {
		if s[i] == '=' {
			return s[i+1:], true
		}
	}
	return "", false
91 92 93 94 95 96 97
}

func Setenv(key, value string) error {
	envOnce.Do(copyenv)
	if len(key) == 0 {
		return EINVAL
	}
98 99 100 101 102 103 104 105 106 107
	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
		}
	}
108 109 110 111

	envLock.Lock()
	defer envLock.Unlock()

112 113 114
	i, ok := env[key]
	kv := key + "=" + value
	if ok {
115
		envs[i] = kv
116
	} else {
117 118
		i = len(envs)
		envs = append(envs, kv)
119 120 121
	}
	env[key] = i
	setenv_c(key, value)
122 123 124 125 126 127 128 129 130
	return nil
}

func Clearenv() {
	envOnce.Do(copyenv) // prevent copyenv in Getenv/Setenv

	envLock.Lock()
	defer envLock.Unlock()

131 132 133
	for k := range env {
		unsetenv_c(k)
	}
134
	env = make(map[string]int)
135
	envs = []string{}
136 137 138 139 140 141
}

func Environ() []string {
	envOnce.Do(copyenv)
	envLock.RLock()
	defer envLock.RUnlock()
142 143 144 145 146 147
	a := make([]string, 0, len(envs))
	for _, env := range envs {
		if env != "" {
			a = append(a, env)
		}
	}
148 149
	return a
}