github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/os/env_windows.go (about)

     1  // Copyright 2019 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  package os
     6  
     7  import (
     8  	"internal/syscall/windows"
     9  	"syscall"
    10  	"unicode/utf16"
    11  	"unsafe"
    12  )
    13  
    14  func environForSysProcAttr(sys *syscall.SysProcAttr) (env []string, err error) {
    15  	if sys == nil || sys.Token == 0 {
    16  		return Environ(), nil
    17  	}
    18  	var block *uint16
    19  	err = windows.CreateEnvironmentBlock(&block, sys.Token, false)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	defer windows.DestroyEnvironmentBlock(block)
    24  	blockp := uintptr(unsafe.Pointer(block))
    25  	for {
    26  		entry := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(blockp))[:]
    27  		for i, v := range entry {
    28  			if v == 0 {
    29  				entry = entry[:i]
    30  				break
    31  			}
    32  		}
    33  		if len(entry) == 0 {
    34  			break
    35  		}
    36  		env = append(env, string(utf16.Decode(entry)))
    37  		blockp += 2 * (uintptr(len(entry)) + 1)
    38  	}
    39  	return
    40  }