github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/src/runtime/env_posix.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 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows 6 7 package runtime 8 9 import "unsafe" 10 11 func getenv(s *byte) *byte { 12 val := gogetenv(gostringnocopy(s)) 13 if val == "" { 14 return nil 15 } 16 // Strings found in environment are NUL-terminated. 17 return &bytes(val)[0] 18 } 19 20 func gogetenv(key string) string { 21 env := environ() 22 if env == nil { 23 gothrow("getenv before env init") 24 } 25 for _, s := range environ() { 26 if len(s) > len(key) && s[len(key)] == '=' && s[:len(key)] == key { 27 return s[len(key)+1:] 28 } 29 } 30 return "" 31 } 32 33 var _cgo_setenv unsafe.Pointer // pointer to C function 34 var _cgo_unsetenv unsafe.Pointer // pointer to C function 35 36 // Update the C environment if cgo is loaded. 37 // Called from syscall.Setenv. 38 func syscall_setenv_c(k string, v string) { 39 if _cgo_setenv == nil { 40 return 41 } 42 arg := [2]unsafe.Pointer{cstring(k), cstring(v)} 43 asmcgocall(unsafe.Pointer(_cgo_setenv), unsafe.Pointer(&arg)) 44 } 45 46 // Update the C environment if cgo is loaded. 47 // Called from syscall.unsetenv. 48 func syscall_unsetenv_c(k string) { 49 if _cgo_unsetenv == nil { 50 return 51 } 52 arg := [1]unsafe.Pointer{cstring(k)} 53 asmcgocall(unsafe.Pointer(_cgo_unsetenv), unsafe.Pointer(&arg)) 54 } 55 56 func cstring(s string) unsafe.Pointer { 57 p := make([]byte, len(s)+1) 58 sp := (*_string)(unsafe.Pointer(&s)) 59 memmove(unsafe.Pointer(&p[0]), unsafe.Pointer(sp.str), uintptr(len(s))) 60 return unsafe.Pointer(&p[0]) 61 }