VM: improved vm_executable_path which works for arbitrary long paths
parent
53467d4e21
commit
9a531076f0
|
@ -2,20 +2,30 @@
|
||||||
|
|
||||||
namespace factor {
|
namespace factor {
|
||||||
|
|
||||||
/* Snarfed from SBCL linux-so.c. You must free() the result yourself. */
|
|
||||||
const char* vm_executable_path() {
|
const char* vm_executable_path() {
|
||||||
char* path = new char[PATH_MAX + 1];
|
ssize_t bufsiz = 4096;
|
||||||
|
|
||||||
int size = readlink("/proc/self/exe", path, PATH_MAX);
|
/* readlink is called in a loop with increasing buffer sizes in case
|
||||||
|
someone tries to run Factor from a incredibly deeply nested
|
||||||
|
path. */
|
||||||
|
while (true) {
|
||||||
|
char* buf = new char[bufsiz + 1];
|
||||||
|
ssize_t size= readlink("/proc/self/exe", buf, bufsiz);
|
||||||
if (size < 0) {
|
if (size < 0) {
|
||||||
fatal_error("Cannot read /proc/self/exe", 0);
|
fatal_error("Cannot read /proc/self/exe", errno);
|
||||||
return NULL;
|
|
||||||
} else {
|
} else {
|
||||||
path[size] = '\0';
|
if (size < bufsiz) {
|
||||||
|
/* Buffer was large enough, return string. */
|
||||||
const char* ret = safe_strdup(path);
|
buf[size] = '\0';
|
||||||
delete[] path;
|
const char* ret = safe_strdup(buf);
|
||||||
|
delete[] buf;
|
||||||
return ret;
|
return ret;
|
||||||
|
} else {
|
||||||
|
/* Buffer wasn't big enough, double it and try again. */
|
||||||
|
delete[] buf;
|
||||||
|
bufsiz *= 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue