1
2
3
4
5
6
7 package execenv
8
9 import (
10 "internal/syscall/windows"
11 "syscall"
12 "unicode/utf16"
13 "unsafe"
14 )
15
16
17
18
19
20
21
22
23
24 func Default(sys *syscall.SysProcAttr) (env []string, err error) {
25 if sys == nil || sys.Token == 0 {
26 return syscall.Environ(), nil
27 }
28 var block *uint16
29 err = windows.CreateEnvironmentBlock(&block, sys.Token, false)
30 if err != nil {
31 return nil, err
32 }
33 defer windows.DestroyEnvironmentBlock(block)
34 blockp := uintptr(unsafe.Pointer(block))
35 for {
36
37
38 end := unsafe.Pointer(blockp)
39 for *(*uint16)(end) != 0 {
40 end = unsafe.Pointer(uintptr(end) + 2)
41 }
42
43 n := (uintptr(end) - uintptr(unsafe.Pointer(blockp))) / 2
44 if n == 0 {
45
46 break
47 }
48
49 entry := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(blockp))[:n:n]
50 env = append(env, string(utf16.Decode(entry)))
51 blockp += 2 * (uintptr(len(entry)) + 1)
52 }
53 return
54 }
55
View as plain text