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.
12 // ErrNotFound is the error resulting if a path search failed to find an executable file.
13 var ErrNotFound = os.NewError("executable file not found in %PATH%")
15 func chkStat(file string) os.Error {
16 d, err := os.Stat(file)
26 func findExecutable(file string, exts []string) (string, os.Error) {
28 return file, chkStat(file)
30 f := strings.ToLower(file)
31 for _, e := range exts {
32 if strings.HasSuffix(f, e) {
33 return file, chkStat(file)
36 for _, e := range exts {
37 if f := file + e; chkStat(f) == nil {
44 func LookPath(file string) (f string, err os.Error) {
45 x := os.Getenv(`PATHEXT`)
47 x = `.COM;.EXE;.BAT;.CMD`
50 for _, e := range strings.Split(strings.ToLower(x), `;`) {
57 exts = append(exts, e)
59 if strings.IndexAny(file, `:\/`) != -1 {
60 if f, err = findExecutable(file, exts); err == nil {
63 return ``, &Error{file, err}
65 if pathenv := os.Getenv(`PATH`); pathenv == `` {
66 if f, err = findExecutable(`.\`+file, exts); err == nil {
70 for _, dir := range strings.Split(pathenv, `;`) {
71 if f, err = findExecutable(dir+`\`+file, exts); err == nil {
76 return ``, &Error{file, ErrNotFound}