1 unstable release

0.2.0 Apr 11, 2020

#1047 in Filesystem

Download history 6761/week @ 2024-06-12 6602/week @ 2024-06-19 6611/week @ 2024-06-26 6016/week @ 2024-07-03 6416/week @ 2024-07-10 7259/week @ 2024-07-17 9766/week @ 2024-07-24 8527/week @ 2024-07-31 9137/week @ 2024-08-07 8651/week @ 2024-08-14 10015/week @ 2024-08-21 8070/week @ 2024-08-28 10319/week @ 2024-09-04 10032/week @ 2024-09-11 9924/week @ 2024-09-18 8613/week @ 2024-09-25

40,228 downloads per month
Used in 29 crates (9 directly)

MIT license

12KB
146 lines

pathsearch

This crate provides functions that can be used to search for an executable based on the PATH environment on both POSIX and Windows systems.

find_executable_in_path is the most convenient function exported by this crate; given the name of an executable, it will yield the absolute path of the first matching file.

use pathsearch::find_executable_in_path;

if let Some(exe) = find_executable_in_path("ls") {
  println!("Found ls at {}", exe.display());
}

PathSearcher is platform-independent struct that encompasses the path searching algorithm used by find_executable_in_path. Construct it by passing in the PATH and PATHEXT (for Windows) environment variables and iterate it to incrementally produce all candidate results. This is useful when implementing utilities such as which that want to show all possible paths.

use pathsearch::PathSearcher;
use std::ffi::OsString;

let path = std::env::var_os("PATH");
let path_ext = std::env::var_os("PATHEXT");

for exe in PathSearcher::new(
    "zsh",
    path.as_ref().map(OsString::as_os_str),
    path_ext.as_ref().map(OsString::as_os_str),
) {
    println!("{}", exe.display());
}

SimplePathSearcher is a simple iterator that can be used to search an arbitrary path for an arbitrary file that doesn't have to be executable.

License: MIT

Dependencies

~175KB