diff options
| author | Ondřej Surý <ondrej@sury.org> | 2011-06-30 15:34:22 +0200 | 
|---|---|---|
| committer | Ondřej Surý <ondrej@sury.org> | 2011-06-30 15:34:22 +0200 | 
| commit | d39f5aa373a4422f7a5f3ee764fb0f6b0b719d61 (patch) | |
| tree | 1833f8b72a4b3a8f00d0d143b079a8fcad01c6ae /src/pkg/os/env_unix.go | |
| parent | 8652e6c371b8905498d3d314491d36c58d5f68d5 (diff) | |
| download | golang-d39f5aa373a4422f7a5f3ee764fb0f6b0b719d61.tar.gz | |
Imported Upstream version 58upstream/58
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. | 
