1 // Copyright 2010 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 // +build darwin dragonfly freebsd linux netbsd openbsd
7 // Unix environment variables.
14 // envOnce guards initialization by copyenv, which populates env.
17 // envLock guards env and envs.
20 // env maps from an environment variable to its first occurrence in envs.
23 // envs is provided by the runtime. elements are expected to be
24 // of the form "key=value".
28 // setenv_c is provided by the runtime, but is a no-op if cgo isn't
30 func setenv_c(k
, v
string)
33 env
= make(map[string]int)
34 for i
, s
:= range Envs
{
35 for j
:= 0; j
< len(s
); j
++ {
38 if _
, ok
:= env
[key
]; !ok
{
47 func Getenv(key
string) (value
string, found
bool) {
54 defer envLock
.RUnlock()
61 for i
:= 0; i
< len(s
); i
++ {
69 func Setenv(key
, value
string) error
{
74 for i
:= 0; i
< len(key
); i
++ {
75 if key
[i
] == '=' || key
[i
] == 0 {
79 for i
:= 0; i
< len(value
); i
++ {
86 defer envLock
.Unlock()
89 kv
:= key
+ "=" + value
94 Envs
= append(Envs
, kv
)
102 envOnce
.Do(copyenv
) // prevent copyenv in Getenv/Setenv
105 defer envLock
.Unlock()
107 env
= make(map[string]int)
109 // TODO(bradfitz): pass through to C
112 func Environ() []string {
115 defer envLock
.RUnlock()
116 a
:= make([]string, len(Envs
))