github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/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 gogetenv(key string) string {
    12  	env := environ()
    13  	if env == nil {
    14  		throw("getenv before env init")
    15  	}
    16  	for _, s := range environ() {
    17  		if len(s) > len(key) && s[len(key)] == '=' && s[:len(key)] == key {
    18  			return s[len(key)+1:]
    19  		}
    20  	}
    21  	return ""
    22  }
    23  
    24  var _cgo_setenv unsafe.Pointer   // pointer to C function
    25  var _cgo_unsetenv unsafe.Pointer // pointer to C function
    26  
    27  // Update the C environment if cgo is loaded.
    28  // Called from syscall.Setenv.
    29  //go:linkname syscall_setenv_c syscall.setenv_c
    30  func syscall_setenv_c(k string, v string) {
    31  	if _cgo_setenv == nil {
    32  		return
    33  	}
    34  	arg := [2]unsafe.Pointer{cstring(k), cstring(v)}
    35  	asmcgocall(_cgo_setenv, unsafe.Pointer(&arg))
    36  }
    37  
    38  // Update the C environment if cgo is loaded.
    39  // Called from syscall.unsetenv.
    40  //go:linkname syscall_unsetenv_c syscall.unsetenv_c
    41  func syscall_unsetenv_c(k string) {
    42  	if _cgo_unsetenv == nil {
    43  		return
    44  	}
    45  	arg := [1]unsafe.Pointer{cstring(k)}
    46  	asmcgocall(_cgo_unsetenv, unsafe.Pointer(&arg))
    47  }
    48  
    49  func cstring(s string) unsafe.Pointer {
    50  	p := make([]byte, len(s)+1)
    51  	copy(p, s)
    52  	return unsafe.Pointer(&p[0])
    53  }