...
Source file src/pkg/os/exec/lp_plan9.go
1
2
3
4
5 package exec
6
7 import (
8 "errors"
9 "os"
10 "path/filepath"
11 "strings"
12 )
13
14
15 var ErrNotFound = errors.New("executable file not found in $path")
16
17 func findExecutable(file string) error {
18 d, err := os.Stat(file)
19 if err != nil {
20 return err
21 }
22 if m := d.Mode(); !m.IsDir() && m&0111 != 0 {
23 return nil
24 }
25 return os.ErrPermission
26 }
27
28
29
30
31
32
33 func LookPath(file string) (string, error) {
34
35 skip := []string{"/", "#", "./", "../"}
36
37 for _, p := range skip {
38 if strings.HasPrefix(file, p) {
39 err := findExecutable(file)
40 if err == nil {
41 return file, nil
42 }
43 return "", &Error{file, err}
44 }
45 }
46
47 path := os.Getenv("path")
48 for _, dir := range filepath.SplitList(path) {
49 path := filepath.Join(dir, file)
50 if err := findExecutable(path); err == nil {
51 return path, nil
52 }
53 }
54 return "", &Error{file, ErrNotFound}
55 }
56
View as plain text