Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

ruby-runner.c
Go to the documentation of this file.
1 #define _POSIX_C_SOURCE 200809L
2 #include "ruby/config.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9
10 #include "ruby-runner.h"
11
12 #ifdef MAKE_MJIT_BUILD_DIR
13 const char MJIT_HEADER[] = BUILDDIR "/" MJIT_MIN_HEADER;
14 #else
15
16 #define STRINGIZE(expr) STRINGIZE0(expr)
17 #define STRINGIZE0(expr) #expr
18
19 static void
20 insert_env_path(const char *envname, const char *paths, size_t size, int prepend)
21 {
22 const char *env = getenv(envname);
23 char c = 0;
24 size_t n = 0;
25
26 if (env) {
27 while ((c = *env) == PATH_SEP) ++env;
28 n = strlen(env);
29 while (n > 0 && env[n-1] == PATH_SEP) --n;
30 }
31 if (c) {
32 char *e = malloc(size+n+1);
33 size_t pos = 0;
34 if (prepend) {
35 memcpy(e, paths, pos = size-1);
36 e[pos++] = PATH_SEP;
37 }
38 memcpy(e+pos, env, n);
39 pos += n;
40 if (!prepend) {
41 e[pos++] = PATH_SEP;
42 memcpy(e+pos, paths, size-1);
43 pos += size-1;
44 }
45 e[pos] = '\0';
46 env = e;
47 }
48 else {
49 env = paths;
50 }
51 setenv(envname, env, 1);
52 }
53
54 #define EXTOUT_DIR BUILDDIR"/"EXTOUT
55 int
56 main(int argc, char **argv)
57 {
58 static const char builddir[] = BUILDDIR;
59 static const char rubypath[] = BUILDDIR"/"STRINGIZE(RUBY_INSTALL_NAME);
60 static const char rubylib[] =
61 ABS_SRCDIR"/lib"
62 PATH_SEPARATOR
63 EXTOUT_DIR"/common"
64 PATH_SEPARATOR
65 EXTOUT_DIR"/"ARCH
66 ;
67 #ifndef LOAD_RELATIVE
68 static const char mjit_build_dir[] = BUILDDIR"/mjit_build_dir."SOEXT;
69 struct stat stbuf;
70 #endif
71 const size_t dirsize = sizeof(builddir);
72 const size_t namesize = sizeof(rubypath) - dirsize;
73 const char *rubyname = rubypath + dirsize;
74 char *arg0 = argv[0], *p;
75
76 insert_env_path(LIBPATHENV, builddir, dirsize, 1);
77 insert_env_path("RUBYLIB", rubylib, sizeof(rubylib), 0);
78 #ifndef LOAD_RELATIVE
79 if (PRELOADENV[0] && stat(mjit_build_dir, &stbuf) == 0) {
80 insert_env_path(PRELOADENV, mjit_build_dir, sizeof(mjit_build_dir), 1);
81 setenv("MJIT_SEARCH_BUILD_DIR", "true", 0);
82 }
83 #endif
84
85 if (!(p = strrchr(arg0, '/'))) p = arg0; else p++;
86 if (strlen(p) < namesize - 1) {
87 argv[0] = malloc(p - arg0 + namesize);
88 memcpy(argv[0], arg0, p - arg0);
89 p = argv[0] + (p - arg0);
90 }
91 memcpy(p, rubyname, namesize);
92
93 execv(rubypath, argv);
94 perror(rubypath);
95 return -1;
96 }
97
98 #endif /* MAKE_MJIT_BUILD_DIR */
STRINGIZE
#define STRINGIZE(expr)
Definition: ruby-runner.c:16
stat
Definition: rb_mjit_min_header-2.7.0.h:2384
execv
int execv(const char *__path, char *const __argv[])
env
#define env
stat
int stat(const char *__restrict __path, struct stat *__restrict __sbuf)
malloc
void * malloc(size_t) __attribute__((__malloc__)) __attribute__((__warn_unused_result__)) __attribute__((__alloc_size__(1)))
perror
void perror(const char *)
getenv
#define getenv(name)
Definition: win32.c:73
MJIT_HEADER
#define MJIT_HEADER
Definition: rb_mjit_min_header-2.7.0.h:1
strlen
size_t strlen(const char *)
memcpy
void * memcpy(void *__restrict, const void *__restrict, size_t)
main
int main(int argc, char **argv)
Definition: ruby-runner.c:56
size
int size
Definition: encoding.c:58
n
const char size_t n
Definition: rb_mjit_min_header-2.7.0.h:5456
argv
char ** argv
Definition: ruby.c:223
strrchr
char * strrchr(const char *, const char)
argc
int argc
Definition: ruby.c:222
setenv
int setenv(const char *__string, const char *__value, int __overwrite)
PRELOADENV
#define PRELOADENV
Definition: mjit_config.h:17
EXTOUT_DIR
#define EXTOUT_DIR
Definition: ruby-runner.c:54
PATH_SEP
#define PATH_SEP
Definition: defines.h:437