1 unstable release

0.2.0 Apr 11, 2020

#541 in Filesystem

Download history 3007/week @ 2023-12-11 2500/week @ 2023-12-18 1997/week @ 2023-12-25 2860/week @ 2024-01-01 3915/week @ 2024-01-08 3217/week @ 2024-01-15 4707/week @ 2024-01-22 3705/week @ 2024-01-29 3460/week @ 2024-02-05 3961/week @ 2024-02-12 4237/week @ 2024-02-19 4585/week @ 2024-02-26 5444/week @ 2024-03-04 5907/week @ 2024-03-11 5349/week @ 2024-03-18 5103/week @ 2024-03-25

21,967 downloads per month
Used in 25 crates (7 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

~170KB