The Go Programming Language

Source file src/pkg/os/env_unix.go

     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.
     4	
     5	// Unix environment variables.
     6	
     7	package os
     8	
     9	import (
    10		"sync"
    11	)
    12	
    13	// ENOENV is the Error indicating that an environment variable does not exist.
    14	var ENOENV = NewError("no such environment variable")
    15	
    16	var env map[string]string
    17	var once sync.Once
    18	
    19	func copyenv() {
    20		env = make(map[string]string)
    21		for _, s := range Envs {
    22			for j := 0; j < len(s); j++ {
    23				if s[j] == '=' {
    24					env[s[0:j]] = s[j+1:]
    25					break
    26				}
    27			}
    28		}
    29	}
    30	
    31	var envLock sync.RWMutex
    32	
    33	// Getenverror retrieves the value of the environment variable named by the key.
    34	// It returns the value and an error, if any.
    35	func Getenverror(key string) (value string, err Error) {
    36		once.Do(copyenv)
    37	
    38		if len(key) == 0 {
    39			return "", EINVAL
    40		}
    41	
    42		envLock.RLock()
    43		defer envLock.RUnlock()
    44	
    45		v, ok := env[key]
    46		if !ok {
    47			return "", ENOENV
    48		}
    49		return v, nil
    50	}
    51	
    52	// Getenv retrieves the value of the environment variable named by the key.
    53	// It returns the value, which will be empty if the variable is not present.
    54	func Getenv(key string) string {
    55		v, _ := Getenverror(key)
    56		return v
    57	}
    58	
    59	// Setenv sets the value of the environment variable named by the key.
    60	// It returns an Error, if any.
    61	func Setenv(key, value string) Error {
    62		once.Do(copyenv)
    63		if len(key) == 0 {
    64			return EINVAL
    65		}
    66	
    67		envLock.Lock()
    68		defer envLock.Unlock()
    69	
    70		env[key] = value
    71		setenv_c(key, value) // is a no-op if cgo isn't loaded
    72		return nil
    73	}
    74	
    75	// Clearenv deletes all environment variables.
    76	func Clearenv() {
    77		once.Do(copyenv) // prevent copyenv in Getenv/Setenv
    78	
    79		envLock.Lock()
    80		defer envLock.Unlock()
    81	
    82		env = make(map[string]string)
    83	
    84		// TODO(bradfitz): pass through to C
    85	}
    86	
    87	// Environ returns an array of strings representing the environment,
    88	// in the form "key=value".
    89	func Environ() []string {
    90		once.Do(copyenv)
    91		envLock.RLock()
    92		defer envLock.RUnlock()
    93		a := make([]string, len(env))
    94		i := 0
    95		for k, v := range env {
    96			a[i] = k + "=" + v
    97			i++
    98		}
    99		return a
   100	}
   101	
   102	// TempDir returns the default directory to use for temporary files.
   103	func TempDir() string {
   104		dir := Getenv("TMPDIR")
   105		if dir == "" {
   106			dir = "/tmp"
   107		}
   108		return dir
   109	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.