From c457dfbbc469333e7f116971b610a8e3fa1cdf66 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sun, 3 May 2020 17:52:37 -0500 Subject: [PATCH] fix/uutils ~ support function/util (aka, applet) symlink aliasing where possible ## [why] `std::env::current_exe()` has platform dependent behavior and will often return the target binary, not the symlink name, when the binary is executed via symlink. So, to support symlinking, the first (0th) arg from `std::env::args()` is used, when possible, with fallback to `std::env::current_ext()` if args are missing or empty. - ref: --- src/bin/uutils.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/bin/uutils.rs b/src/bin/uutils.rs index f57155672..3fc572c1c 100644 --- a/src/bin/uutils.rs +++ b/src/bin/uutils.rs @@ -10,7 +10,7 @@ // spell-checker:ignore (acronyms/names) Gehring // spell-checker:ignore (rustlang) clippy concat rustlang // spell-checker:ignore (uutils) coreutils uucore uumain uutils sigpipe -// spell-checker:ignore (shell) busybox +// spell-checker:ignore (shell) busybox symlinks include!(concat!(env!("OUT_DIR"), "/uutils_crates.rs")); @@ -24,7 +24,13 @@ use std::io::Write; static VERSION: &str = env!("CARGO_PKG_VERSION"); lazy_static! { - static ref BINARY_PATH: std::path::PathBuf = std::env::current_exe().unwrap(); + static ref BINARY_PATH: std::path::PathBuf = { + // support symlinks by using args[0], when possible, with fallback to current_exe() + match std::env::args().next() { + Some(ref s) if !s.is_empty() => std::path::PathBuf::from(s), + _ => std::env::current_exe().unwrap(), + } + }; static ref NAME: &'static str = &*BINARY_PATH.file_stem().unwrap().to_str().unwrap(); }