From 6c746328f79d4767deb212ebc12fe06d90eb7ecb Mon Sep 17 00:00:00 2001 From: Yuval Pavel Zholkover Date: Wed, 8 Jun 2011 09:44:03 -0400 Subject: [PATCH] os: fix Getenv for Plan 9. Truncate the rightmost char if it is '\0'. R=mirtchovski, ality, taruti, rsc CC=golang-dev https://golang.org/cl/4386046 --- src/pkg/os/env_plan9.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/pkg/os/env_plan9.go b/src/pkg/os/env_plan9.go index 14df55ed0e..1fed89f927 100644 --- a/src/pkg/os/env_plan9.go +++ b/src/pkg/os/env_plan9.go @@ -23,13 +23,18 @@ func Getenverror(key string) (value string, err Error) { } defer f.Close() - var buf [4096]byte - n, e := f.Read(buf[:len(buf)-1]) + l, _ := f.Seek(0, 2) + f.Seek(0, 0) + buf := make([]byte, l) + n, e := f.Read(buf) if iserror(e) { return "", ENOENV } - buf[n] = 0 - return string(buf[0:n]), nil + + if n > 0 && buf[n-1] == 0 { + buf = buf[:n-1] + } + return string(buf), nil } // Getenv retrieves the value of the environment variable named by the key. @@ -52,7 +57,7 @@ func Setenv(key, value string) Error { } defer f.Close() - _, e = f.Write(syscall.StringByteSlice(value)) + _, e = f.Write([]byte(value)) return nil }