Commit faa00164
faa00164f529Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 1 file · ~1 modified
Description
Use /proc/self/exe for reliable lib/ path resolution
Falls back to argv[0] on non-Linux platforms.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- faa00164f529
- Parent
- 14a39043ba59
src/main.cmodifiedView file
@@ -8,6 +8,7 @@
88
#include <stdlib.h>99
#include <string.h>1010
#include <limits.h>11+
#include <unistd.h>1112
#include <prism.h>1213
#include "codegen.h"1314
@@ -76,7 +77,13 @@
7677
if (lib_path_count == 0) {7778
static char default_lib[4096];7879
char exe_dir[4096];79-
snprintf(exe_dir, sizeof(exe_dir), "%s", argv[0]);80+
/* Try /proc/self/exe for reliable exe path (Linux) */81+
ssize_t len = readlink("/proc/self/exe", exe_dir, sizeof(exe_dir) - 1);82+
if (len > 0) {83+
exe_dir[len] = '\0';84+
} else {85+
snprintf(exe_dir, sizeof(exe_dir), "%s", argv[0]);86+
}8087
char *slash = strrchr(exe_dir, '/');8188
if (slash) *slash = '\0'; else snprintf(exe_dir, sizeof(exe_dir), ".");8289
snprintf(default_lib, sizeof(default_lib), "%s/lib", exe_dir);