forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/sys/windows/env_windows.go

91 lines
1.4 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2010 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
// Windows environment variables.
package windows
import (
"syscall"
"unsafe"
)
func Getenv(key string) (value string, found bool) {
2024-02-18 10:42:21 +00:00
return syscall.Getenv(key)
2024-02-18 10:42:21 +00:00
}
func Setenv(key, value string) error {
2024-02-18 10:42:21 +00:00
return syscall.Setenv(key, value)
2024-02-18 10:42:21 +00:00
}
func Clearenv() {
2024-02-18 10:42:21 +00:00
syscall.Clearenv()
2024-02-18 10:42:21 +00:00
}
func Environ() []string {
2024-02-18 10:42:21 +00:00
return syscall.Environ()
2024-02-18 10:42:21 +00:00
}
// Returns a default environment associated with the token, rather than the current
2024-02-18 10:42:21 +00:00
// process. If inheritExisting is true, then this environment also inherits the
2024-02-18 10:42:21 +00:00
// environment of the current process.
2024-02-18 10:42:21 +00:00
func (token Token) Environ(inheritExisting bool) (env []string, err error) {
2024-02-18 10:42:21 +00:00
var block *uint16
2024-02-18 10:42:21 +00:00
err = CreateEnvironmentBlock(&block, token, inheritExisting)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer DestroyEnvironmentBlock(block)
2024-05-14 13:07:09 +00:00
size := unsafe.Sizeof(*block)
2024-05-14 13:07:09 +00:00
for *block != 0 {
2024-05-14 13:07:09 +00:00
// find NUL terminator
2024-05-14 13:07:09 +00:00
end := unsafe.Pointer(block)
2024-05-14 13:07:09 +00:00
for *(*uint16)(end) != 0 {
2024-05-14 13:07:09 +00:00
end = unsafe.Add(end, size)
2024-02-18 10:42:21 +00:00
}
2024-05-14 13:07:09 +00:00
entry := unsafe.Slice(block, (uintptr(end)-uintptr(unsafe.Pointer(block)))/size)
2024-05-14 13:07:09 +00:00
env = append(env, UTF16ToString(entry))
2024-05-14 13:07:09 +00:00
block = (*uint16)(unsafe.Add(end, size))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return env, nil
2024-02-18 10:42:21 +00:00
}
func Unsetenv(key string) error {
2024-02-18 10:42:21 +00:00
return syscall.Unsetenv(key)
2024-02-18 10:42:21 +00:00
}