github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/src/runtime/env_plan9.go (about) 1 // Copyright 2012 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. 4 5 package runtime 6 7 import "unsafe" 8 9 func getenv(s *byte) *byte { 10 val := gogetenv(gostringnocopy(s)) 11 if val == "" { 12 return nil 13 } 14 // Strings found in environment are NUL-terminated. 15 return &bytes(val)[0] 16 } 17 18 var tracebackbuf [128]byte 19 20 func gogetenv(key string) string { 21 var file [128]byte 22 if len(key) > len(file)-6 { 23 return "" 24 } 25 26 copy(file[:], "/env/") 27 copy(file[5:], key) 28 29 fd := open(&file[0], _OREAD, 0) 30 if fd < 0 { 31 return "" 32 } 33 n := seek(fd, 0, 2) 34 if n <= 0 { 35 close(fd) 36 return "" 37 } 38 39 p := make([]byte, n) 40 41 r := pread(fd, unsafe.Pointer(&p[0]), int32(n), 0) 42 close(fd) 43 if r < 0 { 44 return "" 45 } 46 47 if p[r-1] == 0 { 48 r-- 49 } 50 51 var s string 52 sp := (*_string)(unsafe.Pointer(&s)) 53 sp.str = &p[0] 54 sp.len = int(r) 55 return s 56 } 57 58 var _cgo_setenv unsafe.Pointer // pointer to C function 59 var _cgo_unsetenv unsafe.Pointer // pointer to C function