1
0
mirror of https://github.com/golang/go synced 2024-11-22 10:14:40 -07:00

syscall: fix env routines for Plan 9

R=golang-dev
CC=ality, golang-dev, rsc
https://golang.org/cl/5364063
This commit is contained in:
Lucio De Re 2011-11-18 13:36:06 -05:00 committed by Russ Cox
parent ab957154b6
commit aa3dbf2947

View File

@ -10,40 +10,40 @@ import "errors"
func Getenv(key string) (value string, found bool) { func Getenv(key string) (value string, found bool) {
if len(key) == 0 { if len(key) == 0 {
return "", EINVAL return "", false
} }
f, e := Open("/env/" + key) f, e := Open("/env/"+key, O_RDONLY)
if e != nil { if e != nil {
return "", ENOENV return "", false
} }
defer f.Close() defer Close(f)
l, _ := f.Seek(0, 2) l, _ := Seek(f, 0, 2)
f.Seek(0, 0) Seek(f, 0, 0)
buf := make([]byte, l) buf := make([]byte, l)
n, e := f.Read(buf) n, e := Read(f, buf)
if e != nil { if e != nil {
return "", ENOENV return "", false
} }
if n > 0 && buf[n-1] == 0 { if n > 0 && buf[n-1] == 0 {
buf = buf[:n-1] buf = buf[:n-1]
} }
return string(buf), nil return string(buf), true
} }
func Setenv(key, value string) error { func Setenv(key, value string) error {
if len(key) == 0 { if len(key) == 0 {
return EINVAL return errors.New("bad arg in system call")
} }
f, e := Create("/env/" + key) f, e := Create("/env/"+key, O_RDWR, 0666)
if e != nil { if e != nil {
return e return e
} }
defer f.Close() defer Close(f)
_, e = f.Write([]byte(value)) _, e = Write(f, []byte(value))
return nil return nil
} }
@ -54,13 +54,13 @@ func Clearenv() {
func Environ() []string { func Environ() []string {
env := make([]string, 0, 100) env := make([]string, 0, 100)
f, e := Open("/env") f, e := Open("/env", O_RDONLY)
if e != nil { if e != nil {
panic(e) panic(e)
} }
defer f.Close() defer Close(f)
names, e := f.Readdirnames(-1) names, e := readdirnames(f)
if e != nil { if e != nil {
panic(e) panic(e)
} }