1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
10 "internal/syscall/windows"
16 // Default will return the default environment
17 // variables based on the process attributes
20 // If the process attributes contain a token, then
21 // the environment variables will be sourced from
22 // the defaults for that user token, otherwise they
23 // will be sourced from syscall.Environ().
24 func Default(sys *syscall.SysProcAttr) (env []string, err error) {
25 if sys == nil || sys.Token == 0 {
26 return syscall.Environ(), nil
29 err = windows.CreateEnvironmentBlock(&block, sys.Token, false)
33 defer windows.DestroyEnvironmentBlock(block)
34 blockp := uintptr(unsafe.Pointer(block))
37 // find NUL terminator
38 end := unsafe.Pointer(blockp)
39 for *(*uint16)(end) != 0 {
40 end = unsafe.Pointer(uintptr(end) + 2)
43 n := (uintptr(end) - uintptr(unsafe.Pointer(blockp))) / 2
45 // environment block ends with empty string
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)