Feature #4046: Saving C's **argv and cwd allows Ruby programs to reliably=
 restart themselves
http://redmine.ruby-lang.org/issues/show/4046

Author: Rocky Bernstein
Status: Open, Priority: Normal
Target version: 1.9.x

In a debugger often one gets to a state where one just wants to restart e=
verything exactly the way the program was previously invoked. =


It would helpful if Ruby saved **argv and cwd. =


The attached patch saves these in RubyVM::OS_ARGV and RubyVM::OS_STARTUP_=
DIR. With this,
if a Ruby program wants to restart itself, it can run:

  chdir RubyVM::OS_STARTUP_DIR
  exec(*RubyVM::OS_ARGV)


----------------------------------------
http://redmine.ruby-lang.org
SW5kZXg6IGluY2x1ZGUvcnVieS9ydWJ5LmgKPT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09
PT09PQotLS0gaW5jbHVkZS9ydWJ5L3J1YnkuaAkocmV2aXNpb24gMjk3Njcp
CisrKyBpbmNsdWRlL3J1YnkvcnVieS5oCSh3b3JraW5nIGNvcHkpCkBAIC0x
NDU0LDYgKzE0NTQsMTIgQEAKICNpbmNsdWRlICJydWJ5L3N1YnN0LmgiCiAj
ZW5kaWYKIAorc3RydWN0IG9yaWdhcmcgeworICAgIGludCBhcmdjOworICAg
IGNoYXIgKiphcmd2OworICAgIGNoYXIgKm9zX3N0YXJ0dXBfZGlyOworfTsK
KwogI2lmIGRlZmluZWQoX19jcGx1c3BsdXMpCiAjaWYgMAogeyAvKiBzYXRp
c2Z5IGNjLW1vZGUgKi8KSW5kZXg6IHZtLmMKPT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09
PT09PQotLS0gdm0uYwkocmV2aXNpb24gMjk3NjcpCisrKyB2bS5jCSh3b3Jr
aW5nIGNvcHkpCkBAIC0xOTc1LDYgKzE5NzUsOCBAQAogICAgIHJldHVybiBh
cnk7CiB9CiAKK2V4dGVybiBzdHJ1Y3Qgb3JpZ2FyZyBvcmlnYXJnOworCiB2
b2lkCiB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