blob: ec50cac484a61d12a488a51310f0006a18a99eb9 [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 Colombier9d06cfc2014-10-20 23:03:03 +020033 n := seek(fd, 0, 2)
David du Colombierb3f224b2014-09-03 00:56:31 +020034 if n <= 0 {
35 close(fd)
36 return ""
37 }
Russ Cox50199d72014-08-30 14:53:47 -040038
David du Colombier20d9cc42014-09-01 23:03:26 -040039 p := make([]byte, n)
Russ Cox50199d72014-08-30 14:53:47 -040040
David du Colombier20d9cc42014-09-01 23:03:26 -040041 r := pread(fd, unsafe.Pointer(&p[0]), int32(n), 0)
Russ Cox50199d72014-08-30 14:53:47 -040042 close(fd)
43 if r < 0 {
44 return ""
45 }
46
David du Colombier9d06cfc2014-10-20 23:03:03 +020047 if p[r-1] == 0 {
48 r--
49 }
50
Russ Cox50199d72014-08-30 14:53:47 -040051 var s string
52 sp := (*_string)(unsafe.Pointer(&s))
David du Colombier20d9cc42014-09-01 23:03:26 -040053 sp.str = &p[0]
Russ Cox50199d72014-08-30 14:53:47 -040054 sp.len = int(r)
55 return s
56}
David du Colombiere9c57d82014-11-21 19:39:01 +010057
58var _cgo_setenv unsafe.Pointer // pointer to C function
59var _cgo_unsetenv unsafe.Pointer // pointer to C function