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 func canExec(file
string) bool {
13 d
, err
:= os
.Stat(file
)
17 return d
.IsRegular() && d
.Permission()&0111 != 0
20 // LookPath searches for an executable binary named file
21 // in the directories named by the PATH environment variable.
22 // If file contains a slash, it is tried directly and the PATH is not consulted.
23 func LookPath(file
string) (string, os
.Error
) {
24 // NOTE(rsc): I wish we could use the Plan 9 behavior here
25 // (only bypass the path if file begins with / or ./ or ../)
26 // but that would not match all the Unix shells.
28 if strings
.Contains(file
, "/") {
34 pathenv
:= os
.Getenv("PATH")
35 for _
, dir
:= range strings
.Split(pathenv
, ":", -1) {
37 // Unix shell semantics: path element "" means "."
40 if canExec(dir
+ "/" + file
) {
41 return dir
+ "/" + file
, nil