...
Source file
src/os/exec/lp_unix.go
Documentation: os/exec
1
2
3
4
5
6
7 package exec
8
9 import (
10 "errors"
11 "internal/godebug"
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
37
38
39
40 func LookPath(file string) (string, error) {
41
42
43
44
45 if strings.Contains(file, "/") {
46 err := findExecutable(file)
47 if err == nil {
48 return file, nil
49 }
50 return "", &Error{file, err}
51 }
52 path := os.Getenv("PATH")
53 for _, dir := range filepath.SplitList(path) {
54 if dir == "" {
55
56 dir = "."
57 }
58 path := filepath.Join(dir, file)
59 if err := findExecutable(path); err == nil {
60 if !filepath.IsAbs(path) && godebug.Get("execerrdot") != "0" {
61 return path, &Error{file, ErrDot}
62 }
63 return path, nil
64 }
65 }
66 return "", &Error{file, ErrNotFound}
67 }
68
View as plain text