mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-09 05:23:40 +00:00
src/os,src/syscall: move env copy code to syscall.Environ()
This commit is contained in:
@@ -235,11 +235,24 @@ func Mprotect(b []byte, prot int) (err error) {
|
||||
}
|
||||
|
||||
func Environ() []string {
|
||||
environ := libc_environ
|
||||
var envs []string
|
||||
for *environ != nil {
|
||||
// Convert the C string to a Go string.
|
||||
length := libc_strlen(*environ)
|
||||
// calculate total memory required
|
||||
var length uintptr
|
||||
var vars int
|
||||
for environ := libc_environ; *environ != nil; {
|
||||
length += libc_strlen(*environ)
|
||||
vars++
|
||||
environ = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(environ)) + unsafe.Sizeof(environ)))
|
||||
}
|
||||
|
||||
// allocate our backing slice for the strings
|
||||
b := make([]byte, length)
|
||||
// and the slice we're going to return
|
||||
envs := make([]string, 0, vars)
|
||||
|
||||
// loop over the environment again, this time copying over the data to the backing slice
|
||||
for environ := libc_environ; *environ != nil; {
|
||||
length = libc_strlen(*environ)
|
||||
// construct a Go string pointing at the libc-allocated environment variable data
|
||||
var envVar string
|
||||
rawEnvVar := (*struct {
|
||||
ptr unsafe.Pointer
|
||||
@@ -247,8 +260,16 @@ func Environ() []string {
|
||||
})(unsafe.Pointer(&envVar))
|
||||
rawEnvVar.ptr = *environ
|
||||
rawEnvVar.length = length
|
||||
envs = append(envs, envVar)
|
||||
// This is the Go equivalent of "environ++" in C.
|
||||
// pull off the number of bytes we need for this environment variable
|
||||
var bs []byte
|
||||
bs, b = b[:length], b[length:]
|
||||
// copy over the bytes to the Go heap
|
||||
copy(bs, envVar)
|
||||
// convert trimmed slice to string
|
||||
s := *(*string)(unsafe.Pointer(&bs))
|
||||
// add s to our list of environment variables
|
||||
envs = append(envs, s)
|
||||
// environ++
|
||||
environ = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(environ)) + unsafe.Sizeof(environ)))
|
||||
}
|
||||
return envs
|
||||
|
||||
Reference in New Issue
Block a user