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

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
use linux as os;

#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
use macos as os;

#[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))]
mod bsd;
#[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))]
use bsd as os;

#[cfg(any(
    target_os = "linux",
    target_os = "freebsd",
    target_os = "dragonfly",
    target_os = "netbsd",
    target_os = "macos"
))]
mod nix;

#[cfg(target_os = "windows")]
mod windows;
#[cfg(target_os = "windows")]
use windows as os;

/// Gets the path of the currently running process. If the path cannot be determined,
/// `None` is returned.
#[inline]
pub fn get_executable_path() -> Option<PathBuf> {
    os::get_executable_path()
}

/// Gets the path of the current dynamic library. If the path cannot be determined,
/// `None` is returned.
#[inline]
pub fn get_dylib_path() -> Option<PathBuf> {
    #[cfg(target_os = "windows")]
    {
        os::get_dylib_path()
    }
    #[cfg(not(target_os = "windows"))]
    {
        nix::get_dylib_path()
    }
}