diff options
Diffstat (limited to 'src/pkg/os/env.go')
-rw-r--r-- | src/pkg/os/env.go | 53 |
1 files changed, 50 insertions, 3 deletions
diff --git a/src/pkg/os/env.go b/src/pkg/os/env.go index 3772c090b..7e3f52502 100644 --- a/src/pkg/os/env.go +++ b/src/pkg/os/env.go @@ -6,7 +6,10 @@ package os -func setenv_c(k, v string) +import ( + "errors" + "syscall" +) // Expand replaces ${var} or $var in the string based on the mapping function. // Invocations of undefined variables are replaced with the empty string. @@ -16,9 +19,9 @@ func Expand(s string, mapping func(string) string) string { i := 0 for j := 0; j < len(s); j++ { if s[j] == '$' && j+1 < len(s) { - buf = append(buf, []byte(s[i:j])...) + buf = append(buf, s[i:j]...) name, w := getShellName(s[j+1:]) - buf = append(buf, []byte(mapping(name))...) + buf = append(buf, mapping(name)...) j += w i = j + 1 } @@ -73,3 +76,47 @@ func getShellName(s string) (string, int) { } return s[:i], i } + +// ENOENV is the error indicating that an environment variable does not exist. +var ENOENV = errors.New("no such environment variable") + +// 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) { + if len(key) == 0 { + return "", EINVAL + } + val, found := syscall.Getenv(key) + if !found { + return "", ENOENV + } + return val, nil +} + +// Getenv retrieves the value of the environment variable named by the key. +// It returns the value, which will be empty if the variable is not present. +func Getenv(key string) string { + v, _ := Getenverror(key) + return v +} + +// Setenv sets the value of the environment variable named by the key. +// It returns an error, if any. +func Setenv(key, value string) error { + err := syscall.Setenv(key, value) + if err != nil { + return NewSyscallError("setenv", err) + } + return nil +} + +// Clearenv deletes all environment variables. +func Clearenv() { + syscall.Clearenv() +} + +// Environ returns an array of strings representing the environment, +// in the form "key=value". +func Environ() []string { + return syscall.Environ() +} |