diff options
author | Araq <rumpf_a@web.de> | 2017-01-06 14:07:05 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2017-01-06 14:07:47 +0100 |
commit | 36964d7491070e0acfb28f127971ad6caddbab66 (patch) | |
tree | 33a6bcfea9571ffe5bcabc942fc54d40e56d31ca /lib | |
parent | 864946afb8980546fa4a7bcda2a8c5ee382e673d (diff) | |
download | Nim-36964d7491070e0acfb28f127971ad6caddbab66.tar.gz |
findExe finds cmd and bat files; refs #5049
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/ospaths.nim | 59 |
1 files changed, 34 insertions, 25 deletions
diff --git a/lib/pure/ospaths.nim b/lib/pure/ospaths.nim index 3d3a105f0..89dc944d0 100644 --- a/lib/pure/ospaths.nim +++ b/lib/pure/ospaths.nim @@ -562,43 +562,52 @@ when declared(getEnv) or defined(nimscript): if lstat(path, rawInfo) < 0'i32: result = false else: result = S_ISLNK(rawInfo.st_mode) - proc findExe*(exe: string, followSymlinks: bool = true): string {. + const + ExeExts* = when defined(windows): ["exe", "cmd", "bat"] else: [""] ## \ + ## platform specific file extension for executables. On Windows + ## ``["exe", "cmd", "bat"]``, on Posix ``[""]``. + + proc findExe*(exe: string, followSymlinks: bool = true; + extensions=ExeExts): string {. tags: [ReadDirEffect, ReadEnvEffect, ReadIOEffect].} = ## Searches for `exe` in the current working directory and then ## in directories listed in the ``PATH`` environment variable. - ## Returns "" if the `exe` cannot be found. On DOS-like platforms, `exe` - ## is added the `ExeExt <#ExeExt>`_ file extension if it has none. + ## Returns "" if the `exe` cannot be found. `exe` + ## is added the `ExeExts <#ExeExts>`_ file extensions if it has none. ## If the system supports symlinks it also resolves them until it ## meets the actual file. This behavior can be disabled if desired. - result = addFileExt(exe, ExeExt) - if existsFile(result): return + for ext in extensions: + result = addFileExt(exe, ext) + if existsFile(result): return var path = string(getEnv("PATH")) for candidate in split(path, PathSep): when defined(windows): var x = (if candidate[0] == '"' and candidate[^1] == '"': substr(candidate, 1, candidate.len-2) else: candidate) / - result + exe else: - var x = expandTilde(candidate) / result - if existsFile(x): - when not defined(windows) and declared(os): - while followSymlinks: # doubles as if here - if x.checkSymlink: - var r = newString(256) - var len = readlink(x, r, 256) - if len < 0: - raiseOSError(osLastError()) - if len > 256: - r = newString(len+1) - len = readlink(x, r, len) - setLen(r, len) - if isAbsolute(r): - x = r + var x = expandTilde(candidate) / exe + for ext in extensions: + let x = addFileExt(x, ext) + if existsFile(x): + when not defined(windows) and declared(os): + while followSymlinks: # doubles as if here + if x.checkSymlink: + var r = newString(256) + var len = readlink(x, r, 256) + if len < 0: + raiseOSError(osLastError()) + if len > 256: + r = newString(len+1) + len = readlink(x, r, len) + setLen(r, len) + if isAbsolute(r): + x = r + else: + x = parentDir(x) / r else: - x = parentDir(x) / r - else: - break - return x + break + return x result = "" when defined(nimscript) or (defined(nimdoc) and not declared(os)): |