...
Source file
src/runtime/env_posix.go
Documentation: runtime
1
2
3
4
5 package runtime
6
7 import "unsafe"
8
9 func gogetenv(key string) string {
10 env := environ()
11 if env == nil {
12 throw("getenv before env init")
13 }
14 for _, s := range env {
15 if len(s) > len(key) && s[len(key)] == '=' && envKeyEqual(s[:len(key)], key) {
16 return s[len(key)+1:]
17 }
18 }
19 return ""
20 }
21
22
23
24 func envKeyEqual(a, b string) bool {
25 if GOOS == "windows" {
26 for i := 0; i < len(a); i++ {
27 ca, cb := a[i], b[i]
28 if ca == cb || lowerASCII(ca) == lowerASCII(cb) {
29 continue
30 }
31 return false
32 }
33 return true
34 }
35 return a == b
36 }
37
38 func lowerASCII(c byte) byte {
39 if 'A' <= c && c <= 'Z' {
40 return c + ('a' - 'A')
41 }
42 return c
43 }
44
45 var _cgo_setenv unsafe.Pointer
46 var _cgo_unsetenv unsafe.Pointer
47
48
49 func setenv_c(k string, v string) {
50 if _cgo_setenv == nil {
51 return
52 }
53 arg := [2]unsafe.Pointer{cstring(k), cstring(v)}
54 asmcgocall(_cgo_setenv, unsafe.Pointer(&arg))
55 }
56
57
58 func unsetenv_c(k string) {
59 if _cgo_unsetenv == nil {
60 return
61 }
62 arg := [1]unsafe.Pointer{cstring(k)}
63 asmcgocall(_cgo_unsetenv, unsafe.Pointer(&arg))
64 }
65
66 func cstring(s string) unsafe.Pointer {
67 p := make([]byte, len(s)+1)
68 copy(p, s)
69 return unsafe.Pointer(&p[0])
70 }
71
View as plain text