LEFT | RIGHT |
(no file at all) | |
| 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) - 1 |
| 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 var s string |
| 48 sp := (*_string)(unsafe.Pointer(&s)) |
| 49 sp.str = &p[0] |
| 50 sp.len = int(r) |
| 51 return s |
| 52 } |
LEFT | RIGHT |