blob: 92ac05ff4ea606ee76025c4004a2556599a6f02c [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// 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"
khenaidooab1f7bd2019-11-14 14:00:27 -050011 "unsafe"
12)
13
14func Getenv(key string) (value string, found bool) {
15 return syscall.Getenv(key)
16}
17
18func Setenv(key, value string) error {
19 return syscall.Setenv(key, value)
20}
21
22func Clearenv() {
23 syscall.Clearenv()
24}
25
26func Environ() []string {
27 return syscall.Environ()
28}
29
30// Returns a default environment associated with the token, rather than the current
31// process. If inheritExisting is true, then this environment also inherits the
32// environment of the current process.
33func (token Token) Environ(inheritExisting bool) (env []string, err error) {
34 var block *uint16
35 err = CreateEnvironmentBlock(&block, token, inheritExisting)
36 if err != nil {
37 return nil, err
38 }
39 defer DestroyEnvironmentBlock(block)
40 blockp := uintptr(unsafe.Pointer(block))
41 for {
Andrea Campanella3614a922021-02-25 12:40:42 +010042 entry := UTF16PtrToString((*uint16)(unsafe.Pointer(blockp)))
khenaidooab1f7bd2019-11-14 14:00:27 -050043 if len(entry) == 0 {
44 break
45 }
Andrea Campanella3614a922021-02-25 12:40:42 +010046 env = append(env, entry)
khenaidooab1f7bd2019-11-14 14:00:27 -050047 blockp += 2 * (uintptr(len(entry)) + 1)
48 }
49 return env, nil
50}
51
52func Unsetenv(key string) error {
53 return syscall.Unsetenv(key)
54}