...

Source file src/runtime/env_posix.go

Documentation: runtime

     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  //go:build unix || (js && wasm) || windows || plan9
     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 env {
    17  		if len(s) > len(key) && s[len(key)] == '=' && envKeyEqual(s[:len(key)], key) {
    18  			return s[len(key)+1:]
    19  		}
    20  	}
    21  	return ""
    22  }
    23  
    24  // envKeyEqual reports whether a == b, with ASCII-only case insensitivity
    25  // on Windows. The two strings must have the same length.
    26  func envKeyEqual(a, b string) bool {
    27  	if GOOS == "windows" { // case insensitive
    28  		for i := 0; i < len(a); i++ {
    29  			ca, cb := a[i], b[i]
    30  			if ca == cb || lowerASCII(ca) == lowerASCII(cb) {
    31  				continue
    32  			}
    33  			return false
    34  		}
    35  		return true
    36  	}
    37  	return a == b
    38  }
    39  
    40  func lowerASCII(c byte) byte {
    41  	if 'A' <= c && c <= 'Z' {
    42  		return c + ('a' - 'A')
    43  	}
    44  	return c
    45  }
    46  
    47  var _cgo_setenv unsafe.Pointer   // pointer to C function
    48  var _cgo_unsetenv unsafe.Pointer // pointer to C function
    49  
    50  // Update the C environment if cgo is loaded.
    51  // Called from syscall.Setenv.
    52  //
    53  //go:linkname syscall_setenv_c syscall.setenv_c
    54  func syscall_setenv_c(k string, v string) {
    55  	if _cgo_setenv == nil {
    56  		return
    57  	}
    58  	arg := [2]unsafe.Pointer{cstring(k), cstring(v)}
    59  	asmcgocall(_cgo_setenv, unsafe.Pointer(&arg))
    60  }
    61  
    62  // Update the C environment if cgo is loaded.
    63  // Called from syscall.unsetenv.
    64  //
    65  //go:linkname syscall_unsetenv_c syscall.unsetenv_c
    66  func syscall_unsetenv_c(k string) {
    67  	if _cgo_unsetenv == nil {
    68  		return
    69  	}
    70  	arg := [1]unsafe.Pointer{cstring(k)}
    71  	asmcgocall(_cgo_unsetenv, unsafe.Pointer(&arg))
    72  }
    73  
    74  func cstring(s string) unsafe.Pointer {
    75  	p := make([]byte, len(s)+1)
    76  	copy(p, s)
    77  	return unsafe.Pointer(&p[0])
    78  }
    79  

View as plain text