From: nobu.nokada@... Date: 2004-07-10T08:11:34+09:00 Subject: Re: [RCR/BUG?] fix system Hi, At Wed, 30 Jun 2004 11:22:54 +0900, Ara.T.Howard wrote in [ruby-talk:104891]: > > One thought: > > system could return nil keeping error info when the command > > couldn't execute, as perl does. Then, the current behavior > > would moved to Process.start, invoke or something else. > > i like this. i use this sort of thing alot I noticed that I've forgotten to send this patch. This separates Kernel#system and Process#system, the former behaves as older version and the latter does as current version. The name is provisional. Index: process.c =================================================================== RCS file: /cvs/ruby/src/ruby/process.c,v retrieving revision 1.107 diff -U2 -p -d -r1.107 process.c --- process.c 29 Jun 2004 01:17:39 -0000 1.107 +++ process.c 1 Jul 2004 02:04:36 -0000 @@ -1575,5 +1575,5 @@ rb_spawn(argc, argv) /* * call-seq: - * system(cmd [, arg, ...]) => true or false + * Process.system(cmd [, arg, ...]) => true or false * * Executes _cmd_ in a subshell, returning +true+ if the command ran @@ -1592,5 +1592,5 @@ rb_spawn(argc, argv) static VALUE -rb_f_system(argc, argv) +rb_proc_system(argc, argv) int argc; VALUE *argv; @@ -1610,4 +1610,37 @@ rb_f_system(argc, argv) /* * call-seq: + * system(cmd [, arg, ...]) => true, false or nil + * + * Same as Process.system except for no exception raise + * but nil is returned. + */ + +static VALUE +rb_f_system(argc, argv) + int argc; + VALUE *argv; +{ + int status; + + status = rb_spawn(argc, argv); + if (status == -1) { + VALUE args[2], e; + args[0] = argv[0]; + args[1] = INT2FIX(errno); + e = rb_class_new_instance(2, args, rb_eSystemCallError); + rb_warning("failed to execute: %s", RSTRING(rb_obj_as_string(e))->ptr); + ruby_errinfo = e; + return Qnil; + } +#if defined(HAVE_FORK) || defined(HAVE_SPAWNV) + rb_syswait(status); + status = NUM2INT(rb_last_status); +#endif + if (status == EXIT_SUCCESS) return Qtrue; + return Qfalse; +} + +/* + * call-seq: * spawn(cmd [, arg, ...]) => pid * @@ -3472,4 +3505,5 @@ Init_process() rb_define_singleton_method(rb_mProcess, "fork", rb_f_fork, 0); + rb_define_singleton_method(rb_mProcess, "system", rb_proc_system, -1); rb_define_singleton_method(rb_mProcess, "spawn", rb_f_spawn, -1); rb_define_singleton_method(rb_mProcess, "exit!", rb_f_exit_bang, -1); -- Nobu Nakada