1// Copyright 2010 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// Windows environment variables.
6
7package windows
8
9import (
10	"syscall"
11	"unicode/utf16"
12	"unsafe"
13)
14
15func Getenv(key string) (value string, found bool) {
16	return syscall.Getenv(key)
17}
18
19func Setenv(key, value string) error {
20	return syscall.Setenv(key, value)
21}
22
23func Clearenv() {
24	syscall.Clearenv()
25}
26
27func Environ() []string {
28	return syscall.Environ()
29}
30
31// Returns a default environment associated with the token, rather than the current
32// process. If inheritExisting is true, then this environment also inherits the
33// environment of the current process.
34func (token Token) Environ(inheritExisting bool) (env []string, err error) {
35	var block *uint16
36	err = CreateEnvironmentBlock(&block, token, inheritExisting)
37	if err != nil {
38		return nil, err
39	}
40	defer DestroyEnvironmentBlock(block)
41	blockp := uintptr(unsafe.Pointer(block))
42	for {
43		entry := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(blockp))[:]
44		for i, v := range entry {
45			if v == 0 {
46				entry = entry[:i]
47				break
48			}
49		}
50		if len(entry) == 0 {
51			break
52		}
53		env = append(env, string(utf16.Decode(entry)))
54		blockp += 2 * (uintptr(len(entry)) + 1)
55	}
56	return env, nil
57}
58
59func Unsetenv(key string) error {
60	return syscall.Unsetenv(key)
61}
62