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