2013-12-05 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / go / syscall / env_windows.go
blob420b3872464cbf5bfb05d59a21af9cfe2abffda6
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 b := make([]uint16, 100)
20 n, e := GetEnvironmentVariable(keyp, &b[0], uint32(len(b)))
21 if n == 0 && e == ERROR_ENVVAR_NOT_FOUND {
22 return "", false
24 if n > uint32(len(b)) {
25 b = make([]uint16, n)
26 n, e = GetEnvironmentVariable(keyp, &b[0], uint32(len(b)))
27 if n > uint32(len(b)) {
28 n = 0
31 return string(utf16.Decode(b[0:n])), true
34 func Setenv(key, value string) error {
35 v, err := UTF16PtrFromString(value)
36 if err != nil {
37 return err
39 keyp, err := UTF16PtrFromString(key)
40 if err != nil {
41 return err
43 e := SetEnvironmentVariable(keyp, v)
44 if e != nil {
45 return e
47 return nil
50 func Clearenv() {
51 for _, s := range Environ() {
52 // Environment variables can begin with =
53 // so start looking for the separator = at j=1.
54 // http://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx
55 for j := 1; j < len(s); j++ {
56 if s[j] == '=' {
57 Setenv(s[0:j], "")
58 break
64 func Environ() []string {
65 s, e := GetEnvironmentStrings()
66 if e != nil {
67 return nil
69 defer FreeEnvironmentStrings(s)
70 r := make([]string, 0, 50) // Empty with room to grow.
71 for from, i, p := 0, 0, (*[1 << 24]uint16)(unsafe.Pointer(s)); true; i++ {
72 if p[i] == 0 {
73 // empty string marks the end
74 if i <= from {
75 break
77 r = append(r, string(utf16.Decode(p[from:i])))
78 from = i + 1
81 return r