diff options
| author | Ondřej Surý <ondrej@sury.org> | 2011-05-23 09:45:29 +0200 |
|---|---|---|
| committer | Ondřej Surý <ondrej@sury.org> | 2011-05-23 11:04:16 +0200 |
| commit | 4e5e42a1c1a9eb757743648c48df70cbd42635db (patch) | |
| tree | 64220a5cf0a1ad27206bbcdbc810ea56ba5ed67f /src/pkg/os/env_unix.go | |
| parent | fd089e27d7260cee1c7ca449c41cdd0341ebc6cb (diff) | |
| download | golang-4e5e42a1c1a9eb757743648c48df70cbd42635db.tar.gz | |
Imported Upstream version 2011.05.22
Diffstat (limited to 'src/pkg/os/env_unix.go')
| -rw-r--r-- | src/pkg/os/env_unix.go | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/src/pkg/os/env_unix.go b/src/pkg/os/env_unix.go index e7e1c3b90..8aa71e83a 100644 --- a/src/pkg/os/env_unix.go +++ b/src/pkg/os/env_unix.go @@ -29,6 +29,8 @@ func copyenv() { } } +var envLock sync.RWMutex + // Getenverror retrieves the value of the environment variable named by the key. // It returns the value and an error, if any. func Getenverror(key string) (value string, err Error) { @@ -37,6 +39,10 @@ func Getenverror(key string) (value string, err Error) { if len(key) == 0 { return "", EINVAL } + + envLock.RLock() + defer envLock.RUnlock() + v, ok := env[key] if !ok { return "", ENOENV @@ -55,35 +61,43 @@ func Getenv(key string) string { // It returns an Error, if any. func Setenv(key, value string) Error { once.Do(copyenv) - if len(key) == 0 { return EINVAL } + + envLock.Lock() + defer envLock.Unlock() + env[key] = value + setenv_c(key, value) // is a no-op if cgo isn't loaded return nil } // Clearenv deletes all environment variables. func Clearenv() { once.Do(copyenv) // prevent copyenv in Getenv/Setenv + + envLock.Lock() + defer envLock.Unlock() + env = make(map[string]string) + + // TODO(bradfitz): pass through to C } // Environ returns an array of strings representing the environment, // in the form "key=value". func Environ() []string { once.Do(copyenv) + envLock.RLock() + defer envLock.RUnlock() a := make([]string, len(env)) i := 0 for k, v := range env { - // check i < len(a) for safety, - // in case env is changing underfoot. - if i < len(a) { - a[i] = k + "=" + v - i++ - } + a[i] = k + "=" + v + i++ } - return a[0:i] + return a } // TempDir returns the default directory to use for temporary files. |
