mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 19:17:47 +00:00
runtime: implement environment variables for Linux
This commit is contained in:
committed by
Ron Evans
parent
768a15c1dd
commit
c47cdfa66f
@@ -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
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user