libgo: Merge to master revision 19184.
[official-gcc.git] / libgo / go / os / exec / lp_unix.go
blob7b9dec7e8b50894c4d1a1b0ff9848465c59c7087
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 // +build darwin dragonfly freebsd linux netbsd openbsd solaris
7 package exec
9 import (
10 "errors"
11 "os"
12 "strings"
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)
20 if err != nil {
21 return err
23 if m := d.Mode(); !m.IsDir() && m&0111 != 0 {
24 return nil
26 return os.ErrPermission
29 // LookPath searches for an executable binary named file
30 // in the directories named by the PATH environment variable.
31 // If file contains a slash, it is tried directly and the PATH is not consulted.
32 // The result may be an absolute path or a path relative to the current directory.
33 func LookPath(file string) (string, error) {
34 // NOTE(rsc): I wish we could use the Plan 9 behavior here
35 // (only bypass the path if file begins with / or ./ or ../)
36 // but that would not match all the Unix shells.
38 if strings.Contains(file, "/") {
39 err := findExecutable(file)
40 if err == nil {
41 return file, nil
43 return "", &Error{file, err}
45 pathenv := os.Getenv("PATH")
46 if pathenv == "" {
47 return "", &Error{file, ErrNotFound}
49 for _, dir := range strings.Split(pathenv, ":") {
50 if dir == "" {
51 // Unix shell semantics: path element "" means "."
52 dir = "."
54 path := dir + "/" + file
55 if err := findExecutable(path); err == nil {
56 return path, nil
59 return "", &Error{file, ErrNotFound}