blob: b6dcb4c1eaa09ee4a1e85cbd5fc0472eb070f31e [file] [log] [blame]
Russ Cox50199d72014-08-30 14:53:47 -04001// 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
5package runtime
6
7import "unsafe"
8
9func 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
18var tracebackbuf [128]byte
19
20func 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 }
David du Colombier20d9cc42014-09-01 23:03:26 -040033 n := seek(fd, 0, 2) - 1
Russ Cox50199d72014-08-30 14:53:47 -040034
David du Colombier20d9cc42014-09-01 23:03:26 -040035 p := make([]byte, n)
Russ Cox50199d72014-08-30 14:53:47 -040036
David du Colombier20d9cc42014-09-01 23:03:26 -040037 r := pread(fd, unsafe.Pointer(&p[0]), int32(n), 0)
Russ Cox50199d72014-08-30 14:53:47 -040038 close(fd)
39 if r < 0 {
40 return ""
41 }
42
43 var s string
44 sp := (*_string)(unsafe.Pointer(&s))
David du Colombier20d9cc42014-09-01 23:03:26 -040045 sp.str = &p[0]
Russ Cox50199d72014-08-30 14:53:47 -040046 sp.len = int(r)
47 return s
48}