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.
15 // ErrNotFound is the error resulting if a path search failed to find an executable file.
16 var ErrNotFound
= errors
.New("executable file not found in $path")
18 func findExecutable(file
string) error
{
19 d
, err
:= os
.Stat(file
)
23 if m
:= d
.Mode(); !m
.IsDir() && m
&0111 != 0 {
26 return fs
.ErrPermission
29 // LookPath searches for an executable named file in the
30 // directories named by the path environment variable.
31 // If file begins with "/", "#", "./", or "../", it is tried
32 // directly and the path is not consulted.
33 // The result may be an absolute path or a path relative to the current directory.
34 func LookPath(file
string) (string, error
) {
35 // skip the path lookup for these prefixes
36 skip
:= []string{"/", "#", "./", "../"}
38 for _
, p
:= range skip
{
39 if strings
.HasPrefix(file
, p
) {
40 err
:= findExecutable(file
)
44 return "", &Error
{file
, err
}
48 path
:= os
.Getenv("path")
49 for _
, dir
:= range filepath
.SplitList(path
) {
50 path
:= filepath
.Join(dir
, file
)
51 if err
:= findExecutable(path
); err
== nil {
55 return "", &Error
{file
, ErrNotFound
}