diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-08-03 16:54:30 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-08-03 16:54:30 +0200 |
commit | 28592ee1ea1f5cdffcf85472f9de0285d928cf12 (patch) | |
tree | 32944e18b23f7fe4a0818a694aa2a6dfb1835463 /src/pkg/exec | |
parent | e836bee4716dc0d4d913537ad3ad1925a7ac32d0 (diff) | |
download | golang-upstream/59.tar.gz |
Imported Upstream version 59upstream/59
Diffstat (limited to 'src/pkg/exec')
-rw-r--r-- | src/pkg/exec/Makefile | 3 | ||||
-rw-r--r-- | src/pkg/exec/exec.go | 6 | ||||
-rw-r--r-- | src/pkg/exec/exec_test.go | 2 | ||||
-rw-r--r-- | src/pkg/exec/lp_plan9.go | 51 | ||||
-rw-r--r-- | src/pkg/exec/lp_unix.go | 4 | ||||
-rw-r--r-- | src/pkg/exec/lp_windows.go | 25 |
6 files changed, 78 insertions, 13 deletions
diff --git a/src/pkg/exec/Makefile b/src/pkg/exec/Makefile index 262ecac85..90bb74b41 100644 --- a/src/pkg/exec/Makefile +++ b/src/pkg/exec/Makefile @@ -20,6 +20,9 @@ GOFILES_linux=\ GOFILES_windows=\ lp_windows.go\ +GOFILES_plan9=\ + lp_plan9.go\ + GOFILES+=$(GOFILES_$(GOOS)) include ../../Make.pkg diff --git a/src/pkg/exec/exec.go b/src/pkg/exec/exec.go index 935f24c21..5b988d5eb 100644 --- a/src/pkg/exec/exec.go +++ b/src/pkg/exec/exec.go @@ -12,6 +12,7 @@ import ( "io" "os" "strconv" + "syscall" ) // Error records the name of a binary that failed to be be executed @@ -62,6 +63,10 @@ type Cmd struct { Stdout io.Writer Stderr io.Writer + // SysProcAttr holds optional, operating system-specific attributes. + // Run passes it to os.StartProcess as the os.ProcAttr's Sys field. + SysProcAttr *syscall.SysProcAttr + // Process is the underlying process, once started. Process *os.Process @@ -225,6 +230,7 @@ func (c *Cmd) Start() os.Error { Dir: c.Dir, Files: c.childFiles, Env: c.envv(), + Sys: c.SysProcAttr, }) if err != nil { return err diff --git a/src/pkg/exec/exec_test.go b/src/pkg/exec/exec_test.go index c45a7d70a..f6cebb905 100644 --- a/src/pkg/exec/exec_test.go +++ b/src/pkg/exec/exec_test.go @@ -55,7 +55,7 @@ func TestCatGoodAndBadFile(t *testing.T) { t.Errorf("expected Waitmsg from cat combined; got %T: %v", err, err) } s := string(bs) - sp := strings.Split(s, "\n", 2) + sp := strings.SplitN(s, "\n", 2) if len(sp) != 2 { t.Fatalf("expected two lines from cat; got %q", s) } diff --git a/src/pkg/exec/lp_plan9.go b/src/pkg/exec/lp_plan9.go new file mode 100644 index 000000000..e4751a4df --- /dev/null +++ b/src/pkg/exec/lp_plan9.go @@ -0,0 +1,51 @@ +// Copyright 2011 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. + +package exec + +import ( + "os" + "strings" +) + +// ErrNotFound is the error resulting if a path search failed to find an executable file. +var ErrNotFound = os.NewError("executable file not found in $path") + +func findExecutable(file string) os.Error { + d, err := os.Stat(file) + if err != nil { + return err + } + if d.IsRegular() && d.Permission()&0111 != 0 { + return nil + } + return os.EPERM +} + +// LookPath searches for an executable binary named file +// in the directories named by the path environment variable. +// If file begins with "/", "#", "./", or "../", it is tried +// directly and the path is not consulted. +func LookPath(file string) (string, os.Error) { + // skip the path lookup for these prefixes + skip := []string{"/", "#", "./", "../"} + + for _, p := range skip { + if strings.HasPrefix(file, p) { + err := findExecutable(file) + if err == nil { + return file, nil + } + return "", &Error{file, err} + } + } + + path := os.Getenv("path") + for _, dir := range strings.Split(path, "\000") { + if err := findExecutable(dir + "/" + file); err == nil { + return dir + "/" + file, nil + } + } + return "", &Error{file, ErrNotFound} +} diff --git a/src/pkg/exec/lp_unix.go b/src/pkg/exec/lp_unix.go index 3fc3be832..008fb11a8 100644 --- a/src/pkg/exec/lp_unix.go +++ b/src/pkg/exec/lp_unix.go @@ -10,7 +10,7 @@ import ( ) // ErrNotFound is the error resulting if a path search failed to find an executable file. -var ErrNotFound = os.ErrorString("executable file not found in $PATH") +var ErrNotFound = os.NewError("executable file not found in $PATH") func findExecutable(file string) os.Error { d, err := os.Stat(file) @@ -39,7 +39,7 @@ func LookPath(file string) (string, os.Error) { return "", &Error{file, err} } pathenv := os.Getenv("PATH") - for _, dir := range strings.Split(pathenv, ":", -1) { + for _, dir := range strings.Split(pathenv, ":") { if dir == "" { // Unix shell semantics: path element "" means "." dir = "." diff --git a/src/pkg/exec/lp_windows.go b/src/pkg/exec/lp_windows.go index 758861021..7581088eb 100644 --- a/src/pkg/exec/lp_windows.go +++ b/src/pkg/exec/lp_windows.go @@ -10,7 +10,7 @@ import ( ) // ErrNotFound is the error resulting if a path search failed to find an executable file. -var ErrNotFound = os.ErrorString("executable file not found in %PATH%") +var ErrNotFound = os.NewError("executable file not found in %PATH%") func chkStat(file string) os.Error { d, err := os.Stat(file) @@ -38,20 +38,25 @@ func findExecutable(file string, exts []string) (string, os.Error) { return f, nil } } - return ``, ErrNotFound + return ``, os.ENOENT } func LookPath(file string) (f string, err os.Error) { + x := os.Getenv(`PATHEXT`) + if x == `` { + x = `.COM;.EXE;.BAT;.CMD` + } exts := []string{} - if x := os.Getenv(`PATHEXT`); x != `` { - exts = strings.Split(strings.ToLower(x), `;`, -1) - for i, e := range exts { - if e == `` || e[0] != '.' { - exts[i] = `.` + e - } + for _, e := range strings.Split(strings.ToLower(x), `;`) { + if e == "" { + continue + } + if e[0] != '.' { + e = "." + e } + exts = append(exts, e) } - if strings.Contains(file, `\`) || strings.Contains(file, `/`) { + if strings.IndexAny(file, `:\/`) != -1 { if f, err = findExecutable(file, exts); err == nil { return } @@ -62,7 +67,7 @@ func LookPath(file string) (f string, err os.Error) { return } } else { - for _, dir := range strings.Split(pathenv, `;`, -1) { + for _, dir := range strings.Split(pathenv, `;`) { if f, err = findExecutable(dir+`\`+file, exts); err == nil { return } |