lp_windows.go 3.7 KB

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