env_windows.go 1.3 KB

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