Source file
src/os/exec/lp_unix.go
1
2
3
4
5
6
7 package exec
8
9 import (
10 "errors"
11 "internal/syscall/unix"
12 "io/fs"
13 "os"
14 "path/filepath"
15 "strings"
16 "syscall"
17 )
18
19
20 var ErrNotFound = errors.New("executable file not found in $PATH")
21
22 func findExecutable(file string) error {
23 d, err := os.Stat(file)
24 if err != nil {
25 return err
26 }
27 m := d.Mode()
28 if m.IsDir() {
29 return syscall.EISDIR
30 }
31 err = unix.Eaccess(file, unix.X_OK)
32
33
34
35 if err == nil || (err != syscall.ENOSYS && err != syscall.EPERM) {
36 return err
37 }
38 if m&0111 != 0 {
39 return nil
40 }
41 return fs.ErrPermission
42 }
43
44
45
46
47
48
49
50
51
52 func LookPath(file string) (string, error) {
53
54
55
56
57 if strings.Contains(file, "/") {
58 err := findExecutable(file)
59 if err == nil {
60 return file, nil
61 }
62 return "", &Error{file, err}
63 }
64 path := os.Getenv("PATH")
65 for _, dir := range filepath.SplitList(path) {
66 if dir == "" {
67
68 dir = "."
69 }
70 path := filepath.Join(dir, file)
71 if err := findExecutable(path); err == nil {
72 if !filepath.IsAbs(path) {
73 if execerrdot.Value() != "0" {
74 return path, &Error{file, ErrDot}
75 }
76 execerrdot.IncNonDefault()
77 }
78 return path, nil
79 }
80 }
81 return "", &Error{file, ErrNotFound}
82 }
83
84
85
86 func lookExtensions(path, dir string) (string, error) {
87 return path, nil
88 }
89
View as plain text