1#define _POSIX_C_SOURCE 200809L
2#include "ruby/internal/config.h"
11# error This feature is unnecessary on Windows in favor of SxS.
14#include "ruby-runner.h"
17insert_env_path(
const char *envname,
const char *paths,
size_t size,
int prepend)
19 const char *env = getenv(envname);
24 while ((c = *env) ==
PATH_SEP) ++env;
26 while (n > 0 && env[n-1] ==
PATH_SEP) --n;
29 char *e = malloc(size+n+1);
32 if (size == n || (size < n && env[size] ==
PATH_SEP)) {
33 if (strncmp(paths, env, size) == 0) {
38 memcpy(e, paths, pos = size-1);
41 memcpy(e+pos, env, n);
44 if (size == n || (size < n && env[n-size-1] ==
PATH_SEP)) {
45 if (strncmp(paths, &env[n-size], size) == 0) {
51 memcpy(e+pos, paths, size-1);
60 setenv(envname, env, 1);
63#define EXTOUT_DIR BUILDDIR"/"EXTOUT
65main(
int argc,
char **argv)
67 static const char builddir[] = BUILDDIR;
68 static const char rubypath[] = BUILDDIR
"/"STRINGIZE(RUBY_INSTALL_NAME);
69 static const char rubylib[] =
76 const size_t dirsize =
sizeof(builddir);
77 const size_t namesize =
sizeof(rubypath) - dirsize;
78 const char *rubyname = rubypath + dirsize;
79 char *arg0 = argv[0], *p;
81 insert_env_path(LIBPATHENV, builddir, dirsize, 1);
82 insert_env_path(
"RUBYLIB", rubylib,
sizeof(rubylib), 0);
84 if (!(p = strrchr(arg0,
'/'))) p = arg0;
else p++;
85 if (strlen(p) < namesize - 1) {
86 argv[0] = malloc(p - arg0 + namesize);
87 memcpy(argv[0], arg0, p - arg0);
88 p = argv[0] + (p - arg0);
90 memcpy(p, rubyname, namesize);
92 execv(rubypath, argv);
#define PATH_SEP
The delimiter of PATH environment variable.