2015-10-03 12:28:23 -04:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2015-10-03 11:09:47 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2016-04-13 21:30:12 -04:00
|
|
|
#include "ruby-runner.h"
|
2015-10-31 19:34:17 -04:00
|
|
|
|
|
|
|
#define STRINGIZE(expr) STRINGIZE0(expr)
|
|
|
|
#define STRINGIZE0(expr) #expr
|
2015-10-03 11:09:47 -04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
static const char builddir[] = BUILDDIR;
|
2017-05-29 02:39:41 -04:00
|
|
|
static const char rubypath[] = BUILDDIR"/"STRINGIZE(RUBY_INSTALL_NAME);
|
|
|
|
const size_t dirsize = sizeof(builddir);
|
|
|
|
const size_t namesize = sizeof(rubypath) - dirsize;
|
|
|
|
const char *rubyname = rubypath + dirsize;
|
|
|
|
char *arg0 = argv[0], *p;
|
2015-10-03 11:09:47 -04:00
|
|
|
const char *libpath = getenv(LIBPATHENV);
|
2015-10-31 22:50:59 -04:00
|
|
|
char c = 0;
|
|
|
|
|
2015-10-03 11:09:47 -04:00
|
|
|
if (libpath) {
|
2015-10-31 22:50:59 -04:00
|
|
|
while ((c = *libpath) == PATH_SEP) ++libpath;
|
2015-10-05 02:29:58 -04:00
|
|
|
}
|
2015-10-31 22:50:59 -04:00
|
|
|
if (c) {
|
2015-10-03 11:09:47 -04:00
|
|
|
size_t n = strlen(libpath);
|
2017-05-29 02:39:41 -04:00
|
|
|
char *e = malloc(dirsize+n+1);
|
|
|
|
memcpy(e, builddir, dirsize-1);
|
|
|
|
e[dirsize-1] = PATH_SEP;
|
|
|
|
memcpy(e+dirsize, libpath, n+1);
|
2015-10-03 11:09:47 -04:00
|
|
|
libpath = e;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
libpath = builddir;
|
|
|
|
}
|
|
|
|
setenv(LIBPATHENV, libpath, 1);
|
2017-05-29 02:39:41 -04:00
|
|
|
|
|
|
|
if (!(p = strrchr(arg0, '/'))) p = arg0; else p++;
|
|
|
|
if (strlen(p) < namesize - 1) {
|
|
|
|
argv[0] = malloc(p - arg0 + namesize);
|
|
|
|
memcpy(argv[0], arg0, p - arg0);
|
|
|
|
memcpy(argv[0] + (p - arg0), rubypath + dirsize, namesize);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
memcpy(p, rubyname, namesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
execv(rubypath, argv);
|
2015-10-03 11:09:47 -04:00
|
|
|
return -1;
|
|
|
|
}
|