ruby-runner.c: replace argv[0]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58950 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-05-29 06:39:41 +00:00
parent f8fbb8bc11
commit d4408a3d8b
1 changed files with 21 additions and 5 deletions

View File

@ -12,6 +12,11 @@ int
main(int argc, char **argv)
{
static const char builddir[] = BUILDDIR;
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;
const char *libpath = getenv(LIBPATHENV);
char c = 0;
@ -20,16 +25,27 @@ main(int argc, char **argv)
}
if (c) {
size_t n = strlen(libpath);
char *e = malloc(sizeof(builddir)+n+1);
memcpy(e, builddir, sizeof(builddir)-1);
e[sizeof(builddir)-1] = PATH_SEP;
memcpy(e+sizeof(builddir), libpath, n+1);
char *e = malloc(dirsize+n+1);
memcpy(e, builddir, dirsize-1);
e[dirsize-1] = PATH_SEP;
memcpy(e+dirsize, libpath, n+1);
libpath = e;
}
else {
libpath = builddir;
}
setenv(LIBPATHENV, libpath, 1);
execv(BUILDDIR"/"STRINGIZE(RUBY_INSTALL_NAME), argv);
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);
return -1;
}