Get rid of type-punning pointer casts
[ruby.git] / rubystub.c
blob75aeca18699ed31e89b52acd89364b2acc613ce0
1 #include "internal.h"
2 #include "internal/missing.h"
3 #if defined HAVE_DLADDR
4 #include <dlfcn.h>
5 #endif
6 #if defined HAVE_SYS_PARAM_H
7 #include <sys/param.h>
8 #endif
9 static void* stub_options(int argc, char **argv);
10 #define ruby_options stub_options
11 #include <main.c>
12 #undef ruby_options
14 void *
15 stub_options(int argc, char **argv)
17 char xflag[] = "-x";
18 char *xargv[4] = {NULL, xflag};
19 char *cmd = argv[0];
20 void *ret;
22 #if defined __CYGWIN__ || defined _WIN32
23 /* GetCommandLineW should contain the accessible path,
24 * use argv[0] as is */
25 #elif defined __linux__
27 char selfexe[MAXPATHLEN];
28 ssize_t len = readlink("/proc/self/exe", selfexe, sizeof(selfexe));
29 if (len < 0) {
30 perror("readlink(\"/proc/self/exe\")");
31 return NULL;
33 selfexe[len] = '\0';
34 cmd = selfexe;
36 #elif defined HAVE_DLADDR
38 Dl_info dli;
39 if (!dladdr(stub_options, &dli)) {
40 perror("dladdr");
41 return NULL;
43 cmd = (char *)dli.dli_fname;
45 #endif
47 #ifndef HAVE_SETPROCTITLE
48 /* argc and argv must be the original */
49 ruby_init_setproctitle(argc, argv);
50 #endif
52 /* set script with -x option */
53 /* xargv[0] is NULL not to re-initialize setproctitle again */
54 xargv[2] = cmd;
55 ret = ruby_options(3, xargv);
57 /* set all arguments to ARGV */
58 ruby_set_argv(argc - 1, argv + 1);
60 return ret;