LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // +build darwin freebsd linux openbsd | 5 // +build darwin freebsd linux openbsd |
6 | 6 |
7 package exec | 7 package exec |
8 | 8 |
9 import ( | 9 import ( |
10 "errors" | 10 "errors" |
11 "os" | 11 "os" |
12 "strings" | 12 "strings" |
13 ) | 13 ) |
14 | 14 |
15 // ErrNotFound is the error resulting if a path search failed to find an executa
ble file. | 15 // ErrNotFound is the error resulting if a path search failed to find an executa
ble file. |
16 var ErrNotFound = errors.New("executable file not found in $PATH") | 16 var ErrNotFound = errors.New("executable file not found in $PATH") |
17 | 17 |
18 func findExecutable(file string) error { | 18 func findExecutable(file string) error { |
19 d, err := os.Stat(file) | 19 d, err := os.Stat(file) |
20 if err != nil { | 20 if err != nil { |
21 return err | 21 return err |
22 } | 22 } |
23 » if d.IsRegular() && d.Permission()&0111 != 0 { | 23 » if m := d.Mode(); !m.IsDir() && m&0111 != 0 { |
24 return nil | 24 return nil |
25 } | 25 } |
26 return os.EPERM | 26 return os.EPERM |
27 } | 27 } |
28 | 28 |
29 // LookPath searches for an executable binary named file | 29 // LookPath searches for an executable binary named file |
30 // in the directories named by the PATH environment variable. | 30 // in the directories named by the PATH environment variable. |
31 // If file contains a slash, it is tried directly and the PATH is not consulted. | 31 // If file contains a slash, it is tried directly and the PATH is not consulted. |
32 func LookPath(file string) (string, error) { | 32 func LookPath(file string) (string, error) { |
33 // NOTE(rsc): I wish we could use the Plan 9 behavior here | 33 // NOTE(rsc): I wish we could use the Plan 9 behavior here |
(...skipping 12 matching lines...) Expand all Loading... |
46 if dir == "" { | 46 if dir == "" { |
47 // Unix shell semantics: path element "" means "." | 47 // Unix shell semantics: path element "" means "." |
48 dir = "." | 48 dir = "." |
49 } | 49 } |
50 if err := findExecutable(dir + "/" + file); err == nil { | 50 if err := findExecutable(dir + "/" + file); err == nil { |
51 return dir + "/" + file, nil | 51 return dir + "/" + file, nil |
52 } | 52 } |
53 } | 53 } |
54 return "", &Error{file, ErrNotFound} | 54 return "", &Error{file, ErrNotFound} |
55 } | 55 } |
LEFT | RIGHT |