Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / exec / lp_unix.go
blobb2feecd10e1479963808dea4cbea91303666d53b
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.
5 package exec
7 import (
8 "os"
9 "strings"
12 func canExec(file string) bool {
13 d, err := os.Stat(file)
14 if err != nil {
15 return false
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, "/") {
29 if canExec(file) {
30 return file, nil
32 return "", os.ENOENT
34 pathenv := os.Getenv("PATH")
35 for _, dir := range strings.Split(pathenv, ":", -1) {
36 if dir == "" {
37 // Unix shell semantics: path element "" means "."
38 dir = "."
40 if canExec(dir + "/" + file) {
41 return dir + "/" + file, nil
44 return "", os.ENOENT