Ruby  2.5.0dev(2017-10-22revision60238)
ruby-runner.c
Go to the documentation of this file.
1 #define _POSIX_C_SOURCE 200809L
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 
6 #include "ruby-runner.h"
7 
8 #define STRINGIZE(expr) STRINGIZE0(expr)
9 #define STRINGIZE0(expr) #expr
10 
11 static void
12 insert_env_path(const char *envname, const char *paths, size_t size, int prepend)
13 {
14  const char *env = getenv(envname);
15  char c = 0;
16  size_t n = 0;
17 
18  if (env) {
19  while ((c = *env) == PATH_SEP) ++env;
20  n = strlen(env);
21  while (n > 0 && env[n-1] == PATH_SEP) --n;
22  }
23  if (c) {
24  char *e = malloc(size+n+1);
25  size_t pos = 0;
26  if (prepend) {
27  memcpy(e, paths, pos = size-1);
28  e[pos++] = PATH_SEP;
29  }
30  memcpy(e+pos, env, n);
31  pos += n;
32  if (!prepend) {
33  e[pos++] = PATH_SEP;
34  memcpy(e+pos, paths, size-1);
35  pos += size-1;
36  }
37  e[pos] = '\0';
38  env = e;
39  }
40  else {
41  env = paths;
42  }
43  setenv(envname, env, 1);
44 }
45 
46 #define EXTOUT_DIR BUILDDIR"/"EXTOUT
47 int
48 main(int argc, char **argv)
49 {
50  static const char builddir[] = BUILDDIR;
51  static const char rubypath[] = BUILDDIR"/"STRINGIZE(RUBY_INSTALL_NAME);
52  static const char rubylib[] =
53  ABS_SRCDIR"/lib"
55  EXTOUT_DIR"/common"
58  ;
59  const size_t dirsize = sizeof(builddir);
60  const size_t namesize = sizeof(rubypath) - dirsize;
61  const char *rubyname = rubypath + dirsize;
62  char *arg0 = argv[0], *p;
63 
64  insert_env_path(LIBPATHENV, builddir, dirsize, 1);
65  insert_env_path("RUBYLIB", rubylib, sizeof(rubylib), 0);
66 
67  if (!(p = strrchr(arg0, '/'))) p = arg0; else p++;
68  if (strlen(p) < namesize - 1) {
69  argv[0] = malloc(p - arg0 + namesize);
70  memcpy(argv[0], arg0, p - arg0);
71  p = argv[0] + (p - arg0);
72  }
73  memcpy(p, rubyname, namesize);
74 
75  execv(rubypath, argv);
76  return -1;
77 }
int main(int argc, char **argv)
Definition: ruby-runner.c:48
size_t strlen(const char *)
#define EXTOUT_DIR
Definition: ruby-runner.c:46
#define STRINGIZE(expr)
Definition: ruby-runner.c:8
#define BUILDDIR
Definition: ruby-runner.h:2
#define PATH_SEPARATOR
Definition: ruby-runner.h:4
int argc
Definition: ruby.c:187
#define malloc
Definition: ripper.c:358
#define ARCH
Definition: ruby-runner.h:7
#define getenv(name)
Definition: win32.c:71
int size
Definition: encoding.c:57
#define LIBPATHENV
Definition: ruby-runner.h:3
#define PATH_SEP
Definition: defines.h:302
#define ABS_SRCDIR
Definition: ruby-runner.h:1
#define env
char * strrchr(const char *, const char)
char ** argv
Definition: ruby.c:188