Getenv: almost no one wants the error, so make it return a string that may be empty.

Getenverror is the new name for the old routine that returns an error too.

R=rsc
DELTA=35  (7 added, 7 deleted, 21 changed)
OCL=30818
CL=30821
This commit is contained in:
Rob Pike 2009-06-26 20:28:41 -07:00
parent ac7f2152eb
commit d330c712c1
8 changed files with 23 additions and 23 deletions

View File

@ -111,13 +111,13 @@ func (a FileArray) Swap(i, j int) {
// If current directory is under $GOROOT/src/pkg, return the // If current directory is under $GOROOT/src/pkg, return the
// path relative to there. Otherwise return "". // path relative to there. Otherwise return "".
func PkgDir() string { func PkgDir() string {
goroot, err := os.Getenv("GOROOT"); goroot := os.Getenv("GOROOT");
if err != nil || goroot == "" { if goroot == "" {
return "" return ""
} }
srcroot := path.Clean(goroot + "/src/pkg/"); srcroot := path.Clean(goroot + "/src/pkg/");
pwd, err1 := os.Getenv("PWD"); // TODO(rsc): real pwd pwd := os.Getenv("PWD"); // TODO(rsc): real pwd
if err1 != nil || pwd == "" { if pwd == "" {
return "" return ""
} }
if pwd == srcroot { if pwd == srcroot {

View File

@ -45,9 +45,8 @@ func fatal(format string, args ...) {
} }
func init() { func init() {
var err os.Error; goarch = os.Getenv("GOARCH");
goarch, err = os.Getenv("GOARCH"); goos = os.Getenv("GOOS");
goos, err = os.Getenv("GOOS");
var ok bool; var ok bool;
theChar, ok = theChars[goarch]; theChar, ok = theChars[goarch];
@ -64,7 +63,7 @@ func init() {
for i, v := range binaries { for i, v := range binaries {
var s string; var s string;
if s, err = exec.LookPath(v); err != nil { if s, err := exec.LookPath(v); err != nil {
fatal("cannot find binary %s", v); fatal("cannot find binary %s", v);
} }
bin[v] = s; bin[v] = s;

View File

@ -97,9 +97,8 @@ var (
func init() { func init() {
var err os.Error; goroot = os.Getenv("GOROOT");
goroot, err = os.Getenv("GOROOT"); if goroot != "" {
if err != nil {
goroot = "/home/r/go-release/go"; goroot = "/home/r/go-release/go";
} }
flag.StringVar(&goroot, "goroot", goroot, "Go root directory"); flag.StringVar(&goroot, "goroot", goroot, "Go root directory");

View File

@ -208,12 +208,7 @@ func LookPath(file string) (string, os.Error) {
} }
return "", os.ENOENT; return "", os.ENOENT;
} }
pathenv, err := os.Getenv("PATH"); pathenv := os.Getenv("PATH");
if err != nil {
// Unix shell semantics: no $PATH means assume PATH=""
// (equivalent to PATH=".").
pathenv = "";
}
for i, dir := range strings.Split(pathenv, ":", 0) { for i, dir := range strings.Split(pathenv, ":", 0) {
if dir == "" { if dir == "" {
// Unix shell semantics: path element "" means "." // Unix shell semantics: path element "" means "."

View File

@ -29,9 +29,9 @@ func copyenv() {
} }
} }
// Getenv retrieves the value of the environment variable named by the key. // Getenverror retrieves the value of the environment variable named by the key.
// It returns the value and an error, if any. // It returns the value and an error, if any.
func Getenv(key string) (value string, err Error) { func Getenverror(key string) (value string, err Error) {
once.Do(copyenv); once.Do(copyenv);
if len(key) == 0 { if len(key) == 0 {
@ -44,6 +44,13 @@ func Getenv(key string) (value string, err Error) {
return v, nil; return v, nil;
} }
// Getenv retrieves the value of the environment variable named by the key.
// It returns the value, which will be empty if the variable is not present.
func Getenv(key string) string {
v, _ := Getenverror(key);
return v;
}
// Setenv sets the value of the environment variable named by the key. // Setenv sets the value of the environment variable named by the key.
// It returns an Error, if any. // It returns an Error, if any.
func Setenv(key, value string) Error { func Setenv(key, value string) Error {

View File

@ -28,7 +28,7 @@ func Getwd() (string, Error) {
// Clumsy but widespread kludge: // Clumsy but widespread kludge:
// if $PWD is set and matches ".", use it. // if $PWD is set and matches ".", use it.
pwd, _ := Getenv("PWD"); pwd:= Getenv("PWD");
if len(pwd) > 0 && pwd[0] == '/' { if len(pwd) > 0 && pwd[0] == '/' {
d, err := Stat(pwd); d, err := Stat(pwd);
if err == nil && d.Dev == dot.Dev && d.Ino == dot.Ino { if err == nil && d.Dev == dot.Dev && d.Ino == dot.Ino {

View File

@ -212,7 +212,7 @@ func setupZone() {
// $TZ="" means use UTC. // $TZ="" means use UTC.
// $TZ="foo" means use /usr/share/zoneinfo/foo. // $TZ="foo" means use /usr/share/zoneinfo/foo.
tz, err := os.Getenv("TZ"); tz, err := os.Getenverror("TZ");
var ok bool; var ok bool;
switch { switch {
case err == os.ENOENV: case err == os.ENOENV:

View File

@ -9,7 +9,7 @@ package main
import os "os" import os "os"
func main() { func main() {
ga, e0 := os.Getenv("GOARCH"); ga, e0 := os.Getenverror("GOARCH");
if e0 != nil { if e0 != nil {
print("$GOARCH: ", e0.String(), "\n"); print("$GOARCH: ", e0.String(), "\n");
os.Exit(1); os.Exit(1);
@ -18,7 +18,7 @@ func main() {
print("$GOARCH=", ga, "\n"); print("$GOARCH=", ga, "\n");
os.Exit(1); os.Exit(1);
} }
xxx, e1 := os.Getenv("DOES_NOT_EXIST"); xxx, e1 := os.Getenverror("DOES_NOT_EXIST");
if e1 != os.ENOENV { if e1 != os.ENOENV {
print("$DOES_NOT_EXIST=", xxx, "; err = ", e1.String(), "\n"); print("$DOES_NOT_EXIST=", xxx, "; err = ", e1.String(), "\n");
os.Exit(1); os.Exit(1);