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.
14 errZeroLengthKey
= errors
.New("zero length key")
15 errShortWrite
= errors
.New("i/o count too small")
18 func readenv(key
string) (string, error
) {
19 fd
, err
:= open("/env/"+key
, O_RDONLY
)
24 l
, _
:= Seek(fd
, 0, 2)
26 buf
:= make([]byte, l
)
27 n
, err
:= Read(fd
, buf
)
31 if n
> 0 && buf
[n
-1] == 0 {
34 return string(buf
), nil
37 func writeenv(key
, value
string) error
{
38 fd
, err
:= create("/env/"+key
, O_RDWR
, 0666)
44 n
, err
:= Write(fd
, b
)
54 func Getenv(key
string) (value
string, found
bool) {
58 v
, err
:= readenv(key
)
65 func Setenv(key
, value
string) error
{
67 return errZeroLengthKey
69 err
:= writeenv(key
, value
)
77 RawSyscall(SYS_RFORK
, RFCENVG
, 0, 0)
80 func Unsetenv(key
string) error
{
82 return errZeroLengthKey
88 func Environ() []string {
89 fd
, err
:= open("/env", O_RDONLY
)
94 files
, err
:= readdirnames(fd
)
98 ret
:= make([]string, 0, len(files
))
100 for _
, key
:= range files
{
101 v
, err
:= readenv(key
)
105 ret
= append(ret
, key
+"="+v
)