runtime: implement environment variables for Linux

This commit is contained in:
Ayke van Laethem
2021-04-15 23:04:05 +02:00
committed by Ron Evans
parent 768a15c1dd
commit c47cdfa66f
5 changed files with 40 additions and 11 deletions
-5
View File
@@ -89,8 +89,3 @@ func AdjustTimeOffset(offset int64) {
func os_sigpipe() {
runtimePanic("too many writes on closed pipe")
}
//go:linkname syscall_runtime_envs syscall.runtime_envs
func syscall_runtime_envs() []string {
return nil
}
+34
View File
@@ -62,6 +62,40 @@ func runMain() {
run()
}
//go:extern environ
var environ *unsafe.Pointer
//export strlen
func strlen(ptr unsafe.Pointer) uintptr
//go:linkname syscall_runtime_envs syscall.runtime_envs
func syscall_runtime_envs() []string {
// Count how many environment variables there are.
env := environ
numEnvs := 0
for *env != nil {
numEnvs++
env = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(env)) + unsafe.Sizeof(environ)))
}
// Create a string slice of all environment variables.
// This requires just a single heap allocation.
env = environ
envs := make([]string, 0, numEnvs)
for *env != nil {
ptr := *env
length := strlen(ptr)
s := _string{
ptr: (*byte)(ptr),
length: length,
}
envs = append(envs, *(*string)(unsafe.Pointer(&s)))
env = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(env)) + unsafe.Sizeof(environ)))
}
return envs
}
func putchar(c byte) {
_putchar(int(c))
}