blob: 4c53a9ad9ddb59cab5196c644e993e7b94954f75 [file] [log] [blame]
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Environment variables.
package os
import (
"once";
"os";
)
var (
ENOENV = NewError("no such environment variable");
env map[string] string;
)
func copyenv() {
env = make(map[string] string);
for i, s := range sys.Envs {
for j := 0; j < len(s); j++ {
if s[j] == '=' {
env[s[0:j]] = s[j+1:len(s)];
break;
}
}
}
}
func Getenv(key string) (value string, err *Error) {
once.Do(copyenv);
if len(key) == 0 {
return "", EINVAL;
}
v, ok := env[key];
if !ok {
return "", ENOENV;
}
return v, nil;
}
func Setenv(key, value string) *Error {
once.Do(copyenv);
if len(key) == 0 {
return EINVAL;
}
env[key] = value;
return nil;
}
func Clearenv() {
once.Do(copyenv); // prevent copyenv in Getenv/Setenv
env = make(map[string] string);
}
func Environ() []string {
once.Do(copyenv);
a := make([]string, len(env));
i := 0;
for k, v := range(env) {
// check i < len(a) for safety,
// in case env is changing underfoot.
if i < len(a) {
a[i] = k + "=" + v;
i++;
}
}
return a[0:i];
}