syscall: refactor environment handling

* Move environment functions to their own files.
  * Rewrite the WASIp2 version of environment variables to be much
    simpler (don't go through C functions).
This commit is contained in:
Ayke van Laethem
2024-11-18 11:43:50 +01:00
committed by Ayke
parent e12da15f7d
commit 289fceb3ea
6 changed files with 142 additions and 146 deletions
-52
View File
@@ -238,58 +238,6 @@ func Wait4(pid int, wstatus *WaitStatus, options int, rusage uintptr) (wpid int,
return 0, ENOSYS // TODO
}
func Getenv(key string) (value string, found bool) {
data := cstring(key)
raw := libc_getenv(&data[0])
if raw == nil {
return "", false
}
ptr := uintptr(unsafe.Pointer(raw))
for size := uintptr(0); ; size++ {
v := *(*byte)(unsafe.Pointer(ptr))
if v == 0 {
src := *(*[]byte)(unsafe.Pointer(&sliceHeader{buf: raw, len: size, cap: size}))
return string(src), true
}
ptr += unsafe.Sizeof(byte(0))
}
}
func Setenv(key, val string) (err error) {
if len(key) == 0 {
return EINVAL
}
for i := 0; i < len(key); i++ {
if key[i] == '=' || key[i] == 0 {
return EINVAL
}
}
for i := 0; i < len(val); i++ {
if val[i] == 0 {
return EINVAL
}
}
runtimeSetenv(key, val)
return
}
func Unsetenv(key string) (err error) {
runtimeUnsetenv(key)
return
}
func Clearenv() {
for _, s := range Environ() {
for j := 0; j < len(s); j++ {
if s[j] == '=' {
Unsetenv(s[0:j])
break
}
}
}
}
func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
addr := libc_mmap(nil, uintptr(length), int32(prot), int32(flags), int32(fd), uintptr(offset))
if addr == unsafe.Pointer(^uintptr(0)) {