github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/syscall/execenv/execenv_windows.go (about)

     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.
     4  
     5  //go:build windows
     6  
     7  package execenv
     8  
     9  import (
    10  	"syscall"
    11  	"unsafe"
    12  
    13  	"github.com/go-asm/go/syscall/windows"
    14  )
    15  
    16  // Default will return the default environment
    17  // variables based on the process attributes
    18  // provided.
    19  //
    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
    27  	}
    28  	var blockp *uint16
    29  	err = windows.CreateEnvironmentBlock(&blockp, sys.Token, false)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	defer windows.DestroyEnvironmentBlock(blockp)
    34  
    35  	const size = unsafe.Sizeof(*blockp)
    36  	for *blockp != 0 { // environment block ends with empty string
    37  		// find NUL terminator
    38  		end := unsafe.Add(unsafe.Pointer(blockp), size)
    39  		for *(*uint16)(end) != 0 {
    40  			end = unsafe.Add(end, size)
    41  		}
    42  
    43  		entry := unsafe.Slice(blockp, (uintptr(end)-uintptr(unsafe.Pointer(blockp)))/2)
    44  		env = append(env, syscall.UTF16ToString(entry))
    45  		blockp = (*uint16)(unsafe.Add(end, size))
    46  	}
    47  	return
    48  }