os: implement and smoketest os.Setenv

This commit is contained in:
Dan Kegel
2021-12-05 13:24:00 -08:00
committed by Ron Evans
parent 93ac7cec0d
commit cff4493ca0
5 changed files with 96 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
// +build linux
package runtime
// Update the C environment if cgo is loaded.
// Called from syscall.Setenv.
//go:linkname syscall_setenv_c syscall.setenv_c
func syscall_setenv_c(key string, val string) {
keydata := cstring(key)
valdata := cstring(val)
// ignore any errors
libc_setenv(&keydata[0], &valdata[0], 1)
return
}
// cstring converts a Go string to a C string.
// borrowed from syscall
func cstring(s string) []byte {
data := make([]byte, len(s)+1)
copy(data, s)
// final byte should be zero from the initial allocation
return data
}
// int setenv(const char *name, const char *val, int replace);
//export setenv
func libc_setenv(name *byte, val *byte, replace int32) int32