1 // Copyright 2011 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.
13 // ErrNotFound is the error resulting if a path search failed to find an executable file.
14 var ErrNotFound = errors.New("executable file not found in $path")
16 func findExecutable(file string) error {
17 d, err := os.Stat(file)
21 if d.IsRegular() && d.Permission()&0111 != 0 {
27 // LookPath searches for an executable binary named file
28 // in the directories named by the path environment variable.
29 // If file begins with "/", "#", "./", or "../", it is tried
30 // directly and the path is not consulted.
31 func LookPath(file string) (string, error) {
32 // skip the path lookup for these prefixes
33 skip := []string{"/", "#", "./", "../"}
35 for _, p := range skip {
36 if strings.HasPrefix(file, p) {
37 err := findExecutable(file)
41 return "", &Error{file, err}
45 path := os.Getenv("path")
46 for _, dir := range strings.Split(path, "\000") {
47 if err := findExecutable(dir + "/" + file); err == nil {
48 return dir + "/" + file, nil
51 return "", &Error{file, ErrNotFound}