PR go/83787
[official-gcc.git] / libgo / go / syscall / env_windows.go
blob1606b424ca0efedad65a85aed3ddae540685f747
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.
5 // Windows environment variables.
7 package syscall
9 import (
10 "unicode/utf16"
11 "unsafe"
14 func Getenv(key string) (value string, found bool) {
15 keyp, err := UTF16PtrFromString(key)
16 if err != nil {
17 return "", false
19 n := uint32(100)
20 for {
21 b := make([]uint16, n)
22 n, err = GetEnvironmentVariable(keyp, &b[0], uint32(len(b)))
23 if n == 0 && err == ERROR_ENVVAR_NOT_FOUND {
24 return "", false
26 if n <= uint32(len(b)) {
27 return string(utf16.Decode(b[:n])), true
32 func Setenv(key, value string) error {
33 v, err := UTF16PtrFromString(value)
34 if err != nil {
35 return err
37 keyp, err := UTF16PtrFromString(key)
38 if err != nil {
39 return err
41 e := SetEnvironmentVariable(keyp, v)
42 if e != nil {
43 return e
45 return nil
48 func Unsetenv(key string) error {
49 keyp, err := UTF16PtrFromString(key)
50 if err != nil {
51 return err
53 return SetEnvironmentVariable(keyp, nil)
56 func Clearenv() {
57 for _, s := range Environ() {
58 // Environment variables can begin with =
59 // so start looking for the separator = at j=1.
60 // http://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx
61 for j := 1; j < len(s); j++ {
62 if s[j] == '=' {
63 Unsetenv(s[0:j])
64 break
70 func Environ() []string {
71 s, e := GetEnvironmentStrings()
72 if e != nil {
73 return nil
75 defer FreeEnvironmentStrings(s)
76 r := make([]string, 0, 50) // Empty with room to grow.
77 for from, i, p := 0, 0, (*[1 << 24]uint16)(unsafe.Pointer(s)); true; i++ {
78 if p[i] == 0 {
79 // empty string marks the end
80 if i <= from {
81 break
83 r = append(r, string(utf16.Decode(p[from:i])))
84 from = i + 1
87 return r