process_path/lib.rs
1//! # Executable & Dynamic Library Paths
2//! Utility functions to get the path of the currently executing
3//! process or the the current dynamic library.
4//!
5//! The latter is particularly useful for ‘plug-in’ type dynamic
6//! libraries that need to load resources stored relative to the
7//! location of the library in the file system.
8//! ## Example
9//! ```
10//! let path = process_path::get_executable_path();
11//! match path {
12//! None => println!("The process path could not be determined"),
13//! Some(path) => println!("{:?}", path)
14//! }
15//! ```
16//! ## Supported Platforms
17//! * Linux
18//! * FreeBSD
19//! * NetBSD
20//! * DragonflyBSD
21//! * macOS
22//! * Windows
23use std::path::PathBuf;
24
25#[cfg(target_os = "linux")]
26mod linux;
27#[cfg(target_os = "linux")]
28use linux as os;
29
30#[cfg(target_os = "macos")]
31mod macos;
32#[cfg(target_os = "macos")]
33use macos as os;
34
35#[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))]
36mod bsd;
37#[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))]
38use bsd as os;
39
40#[cfg(any(
41 target_os = "linux",
42 target_os = "freebsd",
43 target_os = "dragonfly",
44 target_os = "netbsd",
45 target_os = "macos"
46))]
47mod nix;
48
49#[cfg(target_os = "windows")]
50mod windows;
51#[cfg(target_os = "windows")]
52use windows as os;
53
54/// Gets the path of the currently running process. If the path cannot be determined,
55/// `None` is returned.
56#[inline]
57pub fn get_executable_path() -> Option<PathBuf> {
58 os::get_executable_path()
59}
60
61/// Gets the path of the current dynamic library. If the path cannot be determined,
62/// `None` is returned.
63#[inline]
64pub fn get_dylib_path() -> Option<PathBuf> {
65 #[cfg(target_os = "windows")]
66 {
67 os::get_dylib_path()
68 }
69 #[cfg(not(target_os = "windows"))]
70 {
71 nix::get_dylib_path()
72 }
73}