|
3 | 3 | // For the full copyright and license information, please view the LICENSE |
4 | 4 | // file that was distributed with this source code. |
5 | 5 |
|
6 | | -// spell-checker:ignore (ToDO) tempdir dyld dylib optgrps libstdbuf |
| 6 | +// spell-checker:ignore (ToDO) tempdir dyld dylib optgrps libstdbuf execfn |
7 | 7 |
|
8 | 8 | use clap::{Arg, ArgAction, ArgMatches, Command}; |
9 | 9 | use std::ffi::OsString; |
@@ -183,14 +183,26 @@ fn get_preload_env(_tmp_dir: &TempDir) -> UResult<(String, PathBuf)> { |
183 | 183 | // Search paths in order: |
184 | 184 | // 1. Directory where stdbuf is located (program_path) |
185 | 185 | // 2. Compile-time directory from LIBSTDBUF_DIR |
186 | | - let mut search_paths: Vec<PathBuf> = Vec::new(); |
187 | | - |
| 186 | + let mut search_paths: Vec<PathBuf> = Vec::with_capacity(2); |
188 | 187 | // First, try to get the directory where stdbuf is running from |
189 | | - if let Ok(exe_path) = std::env::current_exe() { |
190 | | - if let Some(exe_dir) = exe_path.parent() { |
| 188 | + // current_exe() depends on /proc at Linux... |
| 189 | + #[cfg(any(target_os = "linux", target_os = "android"))] |
| 190 | + use std::os::unix::ffi::OsStrExt; |
| 191 | + { |
| 192 | + let exe_path = rustix::param::linux_execfn().to_bytes(); |
| 193 | + if let Some(exe_dir) = std::path::Path::new(std::ffi::OsStr::from_bytes(exe_path)).parent() |
| 194 | + { |
191 | 195 | search_paths.push(exe_dir.to_path_buf()); |
192 | 196 | } |
193 | 197 | } |
| 198 | + #[cfg(not(any(target_os = "linux", target_os = "android")))] |
| 199 | + { |
| 200 | + if let Ok(exe_path) = std::env::current_exe() { |
| 201 | + if let Some(exe_dir) = exe_path.parent() { |
| 202 | + search_paths.push(exe_dir.to_path_buf()); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
194 | 206 |
|
195 | 207 | // Add the compile-time directory as fallback |
196 | 208 | search_paths.push(PathBuf::from(LIBSTDBUF_DIR)); |
|
0 commit comments