Skip to content

Commit

Permalink
Fix for calling a script outside the bundle dir
Browse files Browse the repository at this point in the history
ruby_bang will change the working directory into the directory
that the ruby script is in
  • Loading branch information
calh committed Nov 10, 2016
1 parent 62e391d commit 03060ea
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions ruby_bang.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <stdarg.h>
#include <libgen.h>

#define MAX_PARAMS 100

Expand All @@ -24,6 +25,7 @@ int count_params(char **params);
int has_bundle_exec(char **rvm_params);
int rvm_exec(int quiet, char **envp, char **rvm_params, ...);
void bundle_install(char **rvm_params, char **envp);
char * working_dir(char *script_path);

int main(int argc, char *argv[], char **envp)
{
Expand All @@ -45,6 +47,28 @@ int main(int argc, char *argv[], char **envp)
return 0;
}

// change the working dir to
char * working_dir(char *script_path)
{
char resolved_path[PATH_MAX];

if ( ! realpath(script_path, resolved_path) )
{
perror("realpath");
exit(1);
}

char *dir = dirname(strdup(resolved_path));

if ( chdir(dir) < 0 )
{
perror("chdir");
exit(1);
}

return strdup(resolved_path);
}

/* check if:
* 1) "bundle exec" is in the rvm_params list
* 2) run "bundle check", do nothing if exit status == 0
Expand Down Expand Up @@ -174,9 +198,12 @@ char ** build_rvm_params(char *rvm, char *argv[])
params[0] = rvm;

num_params = add_parameters(argv[1], &params);
// argv[2] contains the shebang script's name.
// pass it to RVM
params[++num_params] = argv[2];
/* argv[2] contains the shebang script's name.
* pass it to RVM as a full canonical path.
* also change working directory to script dirname
*/
char *canon_script = working_dir(argv[2]);
params[++num_params] = canon_script;

// Everthing after argv[2] are parameters passed from
// the command line on the shell to the ruby script
Expand Down

0 comments on commit 03060ea

Please sign in to comment.