...

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 aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || windows || plan9
		 6  // +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris windows plan9
		 7  
		 8  package runtime
		 9  
		10  import "unsafe"
		11  
		12  func gogetenv(key string) string {
		13  	env := environ()
		14  	if env == nil {
		15  		throw("getenv before env init")
		16  	}
		17  	for _, s := range env {
		18  		if len(s) > len(key) && s[len(key)] == '=' && envKeyEqual(s[:len(key)], key) {
		19  			return s[len(key)+1:]
		20  		}
		21  	}
		22  	return ""
		23  }
		24  
		25  // envKeyEqual reports whether a == b, with ASCII-only case insensitivity
		26  // on Windows. The two strings must have the same length.
		27  func envKeyEqual(a, b string) bool {
		28  	if GOOS == "windows" { // case insensitive
		29  		for i := 0; i < len(a); i++ {
		30  			ca, cb := a[i], b[i]
		31  			if ca == cb || lowerASCII(ca) == lowerASCII(cb) {
		32  				continue
		33  			}
		34  			return false
		35  		}
		36  		return true
		37  	}
		38  	return a == b
		39  }
		40  
		41  func lowerASCII(c byte) byte {
		42  	if 'A' <= c && c <= 'Z' {
		43  		return c + ('a' - 'A')
		44  	}
		45  	return c
		46  }
		47  
		48  var _cgo_setenv unsafe.Pointer	 // pointer to C function
		49  var _cgo_unsetenv unsafe.Pointer // pointer to C function
		50  
		51  // Update the C environment if cgo is loaded.
		52  // Called from syscall.Setenv.
		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  //go:linkname syscall_unsetenv_c syscall.unsetenv_c
		65  func syscall_unsetenv_c(k string) {
		66  	if _cgo_unsetenv == nil {
		67  		return
		68  	}
		69  	arg := [1]unsafe.Pointer{cstring(k)}
		70  	asmcgocall(_cgo_unsetenv, unsafe.Pointer(&arg))
		71  }
		72  
		73  func cstring(s string) unsafe.Pointer {
		74  	p := make([]byte, len(s)+1)
		75  	copy(p, s)
		76  	return unsafe.Pointer(&p[0])
		77  }
		78  

View as plain text