env_windows.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // Windows environment variables.
  5. package windows
  6. import (
  7. "syscall"
  8. "unsafe"
  9. )
  10. func Getenv(key string) (value string, found bool) {
  11. return syscall.Getenv(key)
  12. }
  13. func Setenv(key, value string) error {
  14. return syscall.Setenv(key, value)
  15. }
  16. func Clearenv() {
  17. syscall.Clearenv()
  18. }
  19. func Environ() []string {
  20. return syscall.Environ()
  21. }
  22. // Returns a default environment associated with the token, rather than the current
  23. // process. If inheritExisting is true, then this environment also inherits the
  24. // environment of the current process.
  25. func (token Token) Environ(inheritExisting bool) (env []string, err error) {
  26. var block *uint16
  27. err = CreateEnvironmentBlock(&block, token, inheritExisting)
  28. if err != nil {
  29. return nil, err
  30. }
  31. defer DestroyEnvironmentBlock(block)
  32. size := unsafe.Sizeof(*block)
  33. for *block != 0 {
  34. // find NUL terminator
  35. end := unsafe.Pointer(block)
  36. for *(*uint16)(end) != 0 {
  37. end = unsafe.Add(end, size)
  38. }
  39. entry := unsafe.Slice(block, (uintptr(end)-uintptr(unsafe.Pointer(block)))/size)
  40. env = append(env, UTF16ToString(entry))
  41. block = (*uint16)(unsafe.Add(end, size))
  42. }
  43. return env, nil
  44. }
  45. func Unsetenv(key string) error {
  46. return syscall.Unsetenv(key)
  47. }