pathsearch 0.2.0

Search for files in PATH
Documentation
  • Coverage
  • 80%
    8 out of 10 items documented1 out of 8 items with examples
  • Size
  • Source code size: 11.39 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.82 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • wez/wzsh
    88 6 2
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • wez

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