summaryrefslogtreecommitdiff
path: root/src/pkg/os/env.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/os/env.go')
-rw-r--r--src/pkg/os/env.go40
1 files changed, 34 insertions, 6 deletions
diff --git a/src/pkg/os/env.go b/src/pkg/os/env.go
index 3772c090b..eb265f241 100644
--- a/src/pkg/os/env.go
+++ b/src/pkg/os/env.go
@@ -6,7 +6,7 @@
package os
-func setenv_c(k, v string)
+import "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 +16,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
}
@@ -26,10 +26,10 @@ func Expand(s string, mapping func(string) string) string {
return string(buf) + s[i:]
}
-// ShellExpand replaces ${var} or $var in the string according to the values
-// of the operating system's environment variables. References to undefined
+// ExpandEnv replaces ${var} or $var in the string according to the values
+// of the current environment variables. References to undefined
// variables are replaced by the empty string.
-func ShellExpand(s string) string {
+func ExpandEnv(s string) string {
return Expand(s, Getenv)
}
@@ -73,3 +73,31 @@ func getShellName(s string) (string, int) {
}
return s[:i], i
}
+
+// 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, _ := syscall.Getenv(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 a copy of strings representing the environment,
+// in the form "key=value".
+func Environ() []string {
+ return syscall.Environ()
+}