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