Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use execvp

  • Loading branch information...
commit 9258d3b50553779363d084b5551cf007f67ddf9f 1 parent 8c58592
@jordansissel authored
Showing with 52 additions and 0 deletions.
  1. +15 −0 README
  2. +37 −0 shebang.c
View
15 README
@@ -0,0 +1,15 @@
+
+Sometimes `#!/usr/bin/env` isn't sufficient. What if you wanted this:
+
+ #!/usr/bin/env jruby --1.9
+ puts RUBY_VERSION
+
+You'd get:
+
+ /usr/bin/env: jruby --1.9: No such file or directory
+
+That sucks. Use shebang instead.
+
+ #!/usr/bin/shebang jruby --1.9
+ puts RUBY_VERSION
+
View
37 shebang.c
@@ -0,0 +1,37 @@
+/* shebang helper.
+ *
+ * This tool lets you sanely execute things from the shebang line
+ * at the top of a script.
+ *
+ * Problem solved by this program:
+ * This line won't work like you might expect:
+ * #!/usr/bin/env scriptname arg1 arg2 ...
+ *
+ * When executed, this will run this:
+ * /usr/bin/env "scriptname arg1 arg2 ..."
+ *
+ * The above is likely not what you wanted, you wanted:
+ * /usr/bin/env "scriptname" "arg1" "arg2" ...
+ *
+ * If you use this program, it'll work as expected:
+ * #!/usr/bin/shebang scriptname arg1 arg2
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+
+int main(int argc, char **argv) {
+ char *cmd;
+ char *newargv[4];
+
+ /* argv[0] is the program name, don't care */
+ /* argv[1] is the string after the #!command */
+ /* argv[2] is the name of the file you are executing */
+ asprintf(&cmd, "%s %s", argv[1], argv[2]);
+ newargv[0] = "/bin/sh";
+ newargv[1] = "-c";
+ newargv[2] = cmd;
+ newargv[3] = NULL;
+ execvp("/bin/sh", newargv);
+ return -1;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.