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