|
| 1 | +/* |
| 2 | +Copyright (c) 2009 The Go Authors. All rights reserved. |
| 3 | +
|
| 4 | +Redistribution and use in source and binary forms, with or without |
| 5 | +modification, are permitted provided that the following conditions are |
| 6 | +met: |
| 7 | +
|
| 8 | +* Redistributions of source code must retain the above copyright |
| 9 | +notice, this list of conditions and the following disclaimer. |
| 10 | +* Redistributions in binary form must reproduce the above |
| 11 | +copyright notice, this list of conditions and the following disclaimer |
| 12 | +in the documentation and/or other materials provided with the |
| 13 | +distribution. |
| 14 | +* Neither the name of Google Inc. nor the names of its |
| 15 | +contributors may be used to endorse or promote products derived from |
| 16 | +this software without specific prior written permission. |
| 17 | +
|
| 18 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +*/ |
| 30 | + |
| 31 | +package resolvepath |
| 32 | + |
| 33 | +import ( |
| 34 | + "errors" |
| 35 | + "os" |
| 36 | + "os/exec" |
| 37 | + "path/filepath" |
| 38 | + "strings" |
| 39 | +) |
| 40 | + |
| 41 | +// ErrNotFound is the error resulting if a path search failed to find an executable file. |
| 42 | +var ErrNotFound = errors.New("executable file not found in %PATH%") |
| 43 | + |
| 44 | +func chkStat(file string) error { |
| 45 | + d, err := os.Stat(file) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + if d.IsDir() { |
| 50 | + return os.ErrPermission |
| 51 | + } |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +func hasExt(file string) bool { |
| 56 | + i := strings.LastIndex(file, ".") |
| 57 | + if i < 0 { |
| 58 | + return false |
| 59 | + } |
| 60 | + return strings.LastIndexAny(file, `:\/`) < i |
| 61 | +} |
| 62 | + |
| 63 | +func findExecutable(file string, exts []string) (string, error) { |
| 64 | + if len(exts) == 0 { |
| 65 | + return file, chkStat(file) |
| 66 | + } |
| 67 | + if hasExt(file) { |
| 68 | + if chkStat(file) == nil { |
| 69 | + return file, nil |
| 70 | + } |
| 71 | + } |
| 72 | + for _, e := range exts { |
| 73 | + if f := file + e; chkStat(f) == nil { |
| 74 | + return f, nil |
| 75 | + } |
| 76 | + } |
| 77 | + return "", os.ErrNotExist |
| 78 | +} |
| 79 | + |
| 80 | +// LookPath searches for an executable named file in the |
| 81 | +// directories named by the PATH environment variable. |
| 82 | +// If file contains a slash, it is tried directly and the PATH is not consulted. |
| 83 | +// LookPath also uses PATHEXT environment variable to match |
| 84 | +// a suitable candidate. |
| 85 | +// The result may be an absolute path or a path relative to the current directory. |
| 86 | +func LookPath(file string) (string, error) { |
| 87 | + var exts []string |
| 88 | + x := os.Getenv(`PATHEXT`) |
| 89 | + if x != "" { |
| 90 | + for _, e := range strings.Split(strings.ToLower(x), `;`) { |
| 91 | + if e == "" { |
| 92 | + continue |
| 93 | + } |
| 94 | + if e[0] != '.' { |
| 95 | + e = "." + e |
| 96 | + } |
| 97 | + exts = append(exts, e) |
| 98 | + } |
| 99 | + } else { |
| 100 | + exts = []string{".com", ".exe", ".bat", ".cmd"} |
| 101 | + } |
| 102 | + |
| 103 | + if strings.ContainsAny(file, `:\/`) { |
| 104 | + if f, err := findExecutable(file, exts); err == nil { |
| 105 | + return f, nil |
| 106 | + } else { |
| 107 | + return "", &exec.Error{file, err} |
| 108 | + } |
| 109 | + } |
| 110 | + // See https://github.com/golang/go/issues/38736 |
| 111 | + // DO NOT lookup current folder |
| 112 | + //if f, err := findExecutable(filepath.Join(".", file), exts); err == nil { |
| 113 | + // return f, nil |
| 114 | + //} |
| 115 | + path := os.Getenv("path") |
| 116 | + for _, dir := range filepath.SplitList(path) { |
| 117 | + // empty dir means dupicate semicolon in PATH, should not resolve files in current working dir... |
| 118 | + if strings.TrimSpace(dir) == "" { |
| 119 | + continue |
| 120 | + } |
| 121 | + if f, err := findExecutable(filepath.Join(dir, file), exts); err == nil { |
| 122 | + return f, nil |
| 123 | + } |
| 124 | + } |
| 125 | + return "", &exec.Error{file, ErrNotFound} |
| 126 | +} |
0 commit comments