Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ruby/ruby-runner.c
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
79 lines (72 sloc)
1.95 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _POSIX_C_SOURCE 200809L | |
#include "ruby/internal/config.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include "ruby-runner.h" | |
static void | |
insert_env_path(const char *envname, const char *paths, size_t size, int prepend) | |
{ | |
const char *env = getenv(envname); | |
char c = 0; | |
size_t n = 0; | |
if (env) { | |
while ((c = *env) == PATH_SEP) ++env; | |
n = strlen(env); | |
while (n > 0 && env[n-1] == PATH_SEP) --n; | |
} | |
if (c) { | |
char *e = malloc(size+n+1); | |
size_t pos = 0; | |
if (prepend) { | |
memcpy(e, paths, pos = size-1); | |
e[pos++] = PATH_SEP; | |
} | |
memcpy(e+pos, env, n); | |
pos += n; | |
if (!prepend) { | |
e[pos++] = PATH_SEP; | |
memcpy(e+pos, paths, size-1); | |
pos += size-1; | |
} | |
e[pos] = '\0'; | |
env = e; | |
} | |
else { | |
env = paths; | |
} | |
setenv(envname, env, 1); | |
} | |
#define EXTOUT_DIR BUILDDIR"/"EXTOUT | |
int | |
main(int argc, char **argv) | |
{ | |
static const char builddir[] = BUILDDIR; | |
static const char rubypath[] = BUILDDIR"/"STRINGIZE(RUBY_INSTALL_NAME); | |
static const char rubylib[] = | |
ABS_SRCDIR"/lib" | |
PATH_SEPARATOR | |
EXTOUT_DIR"/common" | |
PATH_SEPARATOR | |
EXTOUT_DIR"/"ARCH | |
; | |
const size_t dirsize = sizeof(builddir); | |
const size_t namesize = sizeof(rubypath) - dirsize; | |
const char *rubyname = rubypath + dirsize; | |
char *arg0 = argv[0], *p; | |
insert_env_path(LIBPATHENV, builddir, dirsize, 1); | |
insert_env_path("RUBYLIB", rubylib, sizeof(rubylib), 0); | |
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); | |
p = argv[0] + (p - arg0); | |
} | |
memcpy(p, rubyname, namesize); | |
execv(rubypath, argv); | |
perror(rubypath); | |
return -1; | |
} |