OSDN Git Service

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