1 // Copyright 2011 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.
5 // Plan 9 environment variables.
11 // ENOENV is the Error indicating that an environment variable does not exist.
12 var ENOENV = NewError("no such environment variable")
14 // Getenverror retrieves the value of the environment variable named by the key.
15 // It returns the value and an error, if any.
16 func Getenverror(key string) (value string, err Error) {
20 f, e := Open("/env/" + key)
28 buf := make([]byte, l)
34 if n > 0 && buf[n-1] == 0 {
37 return string(buf), nil
40 // Getenv retrieves the value of the environment variable named by the key.
41 // It returns the value, which will be empty if the variable is not present.
42 func Getenv(key string) string {
43 v, _ := Getenverror(key)
47 // Setenv sets the value of the environment variable named by the key.
48 // It returns an Error, if any.
49 func Setenv(key, value string) Error {
54 f, e := Create("/env/" + key)
60 _, e = f.Write([]byte(value))
64 // Clearenv deletes all environment variables.
66 syscall.RawSyscall(syscall.SYS_RFORK, syscall.RFCENVG, 0, 0)
69 // Environ returns an array of strings representing the environment,
70 // in the form "key=value".
71 func Environ() []string {
72 env := make([]string, 0, 100)
80 names, e := f.Readdirnames(-1)
85 for _, k := range names {
86 if v, e := Getenverror(k); !iserror(e) {
87 env = append(env, k+"="+v)
90 return env[0:len(env)]
93 // TempDir returns the default directory to use for temporary files.
94 func TempDir() string {