diff --git a/ChangeLog b/ChangeLog index b6c06feae9..848e922536 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed Aug 5 19:19:13 2009 Nobuyoshi Nakada + + * ruby.c (load_file_internal): assumes -x flag if no "ruby" is in + the shebang line. [ruby-dev:39015] + Wed Aug 5 19:11:01 2009 NARUSE, Yui * ruby.c (rb_stdio_set_default_encoding): declared. diff --git a/ruby.c b/ruby.c index 297f0e2812..2bfd88c893 100644 --- a/ruby.c +++ b/ruby.c @@ -1508,6 +1508,7 @@ load_file_internal(VALUE arg) rb_funcall(f, set_encoding, 1, rb_enc_from_encoding(enc)); if (opt->xflag) { + search_shebang: forbid_setid("-x"); opt->xflag = Qfalse; while (!NIL_P(line = rb_io_gets(f))) { @@ -1532,37 +1533,8 @@ load_file_internal(VALUE arg) return 0; if ((p = strstr(RSTRING_PTR(line), "ruby")) == 0) { - void rb_thread_stop_timer_thread(void); - /* not ruby script, kick the program */ - char **argv; - char *path; - char *pend = RSTRING_PTR(line) + RSTRING_LEN(line); - - p = RSTRING_PTR(line); /* skip `#!' */ - if (pend[-1] == '\n') - pend--; /* chomp line */ - if (pend[-1] == '\r') - pend--; - *pend = '\0'; - while (p < pend && ISSPACE(*p)) - p++; - path = p; /* interpreter path */ - while (p < pend && !ISSPACE(*p)) - p++; - *p++ = '\0'; - if (p < pend) { - argv = ALLOCA_N(char *, origarg.argc + 3); - argv[1] = p; - MEMCPY(argv + 2, origarg.argv + 1, char *, origarg.argc); - } - else { - argv = origarg.argv; - } - argv[0] = path; - rb_thread_stop_timer_thread(); - execv(path, argv); - - rb_fatal("Can't exec %s", path); + /* not ruby script, assume -x flag */ + goto search_shebang; } start_read: diff --git a/test/ruby/test_system.rb b/test/ruby/test_system.rb index 903918812d..0fbad0af33 100644 --- a/test/ruby/test_system.rb +++ b/test/ruby/test_system.rb @@ -45,6 +45,18 @@ class TestSystem < Test::Unit::TestCase assert_equal('', `#{ruby} -x #{tmpfilename}`) assert_equal('555', `#{ruby} -x #{tmpfilename} -zzz=555`) + tmp = open(tmpfilename, "w") + tmp.print "#! /non/exist\\interpreter?/./to|be:ignored\n"; + tmp.print "this is a leading junk\n"; + tmp.print "#! /usr/local/bin/ruby -s\n"; + tmp.print "print $zzz\n"; + tmp.print "__END__\n"; + tmp.print "this is a trailing junk\n"; + tmp.close + + assert_equal('', `#{ruby} #{tmpfilename}`) + assert_equal('555', `#{ruby} #{tmpfilename} -zzz=555`) + tmp = open(tmpfilename, "w") for i in 1..5 tmp.print i, "\n"