summaryrefslogtreecommitdiff
path: root/template/ruby-runner.c.in
blob: c4ee2f1ee44d1430d7b282713e210e9814376025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define BUILDDIR "@abs_top_builddir@"
#define LIBPATHENV "@LIBPATHENV@"
#define PATH_SEP '@PATH_SEPARATOR@'

#define STRINGIZE(expr) STRINGIZE0(expr)
#define STRINGIZE0(expr) #expr

int
main(int argc, char **argv)
{
    static const char builddir[] = BUILDDIR;
    const char *libpath = getenv(LIBPATHENV);
    if (libpath) {
	while (*libpath == PATH_SEP) ++libpath;
    }
    if (libpath && *libpath) {
	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);
	libpath = e;
    }
    else {
	libpath = builddir;
    }
    setenv(LIBPATHENV, libpath, 1);
    execv(BUILDDIR"/"STRINGIZE(RUBY_INSTALL_NAME), argv);
    return -1;
}