mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-09 21:43:40 +00:00
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:
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user