WASI & darwin: support env variables based on libc

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
Takeshi Yoneda
2021-02-17 23:50:53 +09:00
committed by Ron Evans
parent c299386906
commit 9841ac4acd
9 changed files with 115 additions and 12 deletions
+27 -7
View File
@@ -1,4 +1,4 @@
// +build darwin nintendoswitch
// +build darwin nintendoswitch wasi
package syscall
@@ -6,6 +6,12 @@ import (
"unsafe"
)
type sliceHeader struct {
buf *byte
len uintptr
cap uintptr
}
func Close(fd int) (err error) {
return ENOSYS // TODO
}
@@ -48,18 +54,32 @@ func Getpid() (pid int) {
}
func Getenv(key string) (value string, found bool) {
return "", false // TODO
data := append([]byte(key), 0)
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 splitSlice(p []byte) (buf *byte, len uintptr) {
slice := (*struct {
buf *byte
len uintptr
cap uintptr
})(unsafe.Pointer(&p))
slice := (*sliceHeader)(unsafe.Pointer(&p))
return slice.buf, slice.len
}
// ssize_t write(int fd, const void *buf, size_t count)
//export write
func libc_write(fd int32, buf *byte, count uint) int
// char *getenv(const char *name);
//export getenv
func libc_getenv(name *byte) *byte