2011-11-14 12:06:50 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Plan 9 environment variables.
|
|
|
|
|
|
|
|
package syscall
|
|
|
|
|
2012-01-31 19:14:02 -07:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
2011-11-14 12:06:50 -07:00
|
|
|
|
2012-01-31 19:14:02 -07:00
|
|
|
var (
|
2012-12-17 09:33:51 -07:00
|
|
|
errZeroLengthKey = errors.New("zero length key")
|
|
|
|
errShortWrite = errors.New("i/o count too small")
|
2012-01-31 19:14:02 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func readenv(key string) (string, error) {
|
|
|
|
fd, err := Open("/env/"+key, O_RDONLY)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2011-11-14 12:06:50 -07:00
|
|
|
}
|
2012-01-31 19:14:02 -07:00
|
|
|
defer Close(fd)
|
|
|
|
l, _ := Seek(fd, 0, 2)
|
|
|
|
Seek(fd, 0, 0)
|
|
|
|
buf := make([]byte, l)
|
|
|
|
n, err := Read(fd, buf)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2011-11-14 12:06:50 -07:00
|
|
|
}
|
2012-01-31 19:14:02 -07:00
|
|
|
if n > 0 && buf[n-1] == 0 {
|
|
|
|
buf = buf[:n-1]
|
|
|
|
}
|
|
|
|
return string(buf), nil
|
|
|
|
}
|
2011-11-14 12:06:50 -07:00
|
|
|
|
2012-01-31 19:14:02 -07:00
|
|
|
func writeenv(key, value string) error {
|
|
|
|
fd, err := Create("/env/"+key, O_RDWR, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer Close(fd)
|
2012-12-17 09:33:51 -07:00
|
|
|
b := []byte(value)
|
|
|
|
n, err := Write(fd, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != len(b) {
|
|
|
|
return errShortWrite
|
|
|
|
}
|
|
|
|
return nil
|
2012-01-31 19:14:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func Getenv(key string) (value string, found bool) {
|
|
|
|
if len(key) == 0 {
|
2011-11-18 11:36:06 -07:00
|
|
|
return "", false
|
2011-11-14 12:06:50 -07:00
|
|
|
}
|
2012-12-17 09:33:51 -07:00
|
|
|
v, err := readenv(key)
|
|
|
|
if err != nil {
|
2012-01-31 19:14:02 -07:00
|
|
|
return "", false
|
2011-11-14 12:06:50 -07:00
|
|
|
}
|
2012-01-31 19:14:02 -07:00
|
|
|
return v, true
|
2011-11-14 12:06:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func Setenv(key, value string) error {
|
|
|
|
if len(key) == 0 {
|
2012-12-17 09:33:51 -07:00
|
|
|
return errZeroLengthKey
|
2011-11-14 12:06:50 -07:00
|
|
|
}
|
2012-01-31 19:14:02 -07:00
|
|
|
err := writeenv(key, value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2011-11-14 12:06:50 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Clearenv() {
|
|
|
|
RawSyscall(SYS_RFORK, RFCENVG, 0, 0)
|
|
|
|
}
|
|
|
|
|
2014-10-01 12:17:15 -06:00
|
|
|
func Unsetenv(key string) error {
|
|
|
|
if len(key) == 0 {
|
|
|
|
return errZeroLengthKey
|
|
|
|
}
|
|
|
|
Remove("/env/" + key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-11-14 12:06:50 -07:00
|
|
|
func Environ() []string {
|
2014-10-16 14:30:14 -06:00
|
|
|
fd, err := Open("/env", O_RDONLY)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer Close(fd)
|
|
|
|
files, err := readdirnames(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ret := make([]string, 0, len(files))
|
2012-12-17 09:33:51 -07:00
|
|
|
|
2014-10-16 14:30:14 -06:00
|
|
|
for _, key := range files {
|
|
|
|
v, err := readenv(key)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
2014-10-01 12:17:15 -06:00
|
|
|
}
|
2014-10-16 14:30:14 -06:00
|
|
|
ret = append(ret, key+"="+v)
|
2014-10-01 12:17:15 -06:00
|
|
|
}
|
|
|
|
return ret
|
2011-11-14 12:06:50 -07:00
|
|
|
}
|