Process::Status#to_s

e$B8=:_e(B Process::Status#to_s e$B$O@0?t$JJ8;zNs$rJV$7$^$9!#e(B

% ./ruby -e ‘system(“exit 1;”); p $?.to_s’
“256”

e$B$3$l$O$o$+$j$K$/$$$N$G0J2<$N$h$&$JJ8;zNs$rJV$9$N$O$I$&$G$7$ge(B
e$B$&$+!#e(B

% ./ruby -e ‘system(“exit 1;”); p $?.to_s’
“pid 21978 exit 1”

signal e$B$G;`$s$@>l9ge(B:
% ./ruby -e ‘system(“kill -ILL $$”); puts $?’
pid 7666 SIGILL (signal 4) (core dumped)

signal e$B$G;_$^$C$?>l9ge(B:
% ./ruby -e ‘pid = spawn(“kill -TSTP $$”); Process.waitpid pid,
Process::WUNTRACED; puts $?’
pid 22014 stopped SIGTSTP (signal 20)

e$B$J$!"%a%C%;!<%8$N@8@.%3!<%I$Oe(B inspect e$B$H6&M-$7$F$$j!"e(B
inspect e$B$N7k2L$b>e5-$N7A<0$re(B #<Process::Status: …> e$B$G3g$Ce(B
e$B$?$b$N$K$J$j$^$9!#e(B

% ./ruby -e ‘system(“true”); p $?’
#<Process::Status: pid 7695 exit 0>

Index: process.c

— process.c (e$B%j%S%8%g%se(B 13695)
+++ process.c (e$B:n6H%3%T!<e(B)
@@ -248,20 +248,6 @@

/*

  • call-seq:
    • stat.to_s   => string
      
    • Equivalent to stat.to_i.to_s.
  • */

-static VALUE
-pst_to_s(VALUE st)
-{

  • return rb_fix2str(pst_to_i(st), 10);
    -}

-/*

    • call-seq:
    • stat.pid   => fixnum
      
    • Returns the process ID that this status object represents.
      @@ -277,34 +263,20 @@
      return rb_iv_get(st, “pid”);
      }

-/*

    • call-seq:
    • stat.inspect   => string
      
    • Override the inspection method.
  • */

-static VALUE
-pst_inspect(VALUE st)
+static void
+pst_message(VALUE str, rb_pid_t pid, int status)
{

  • VALUE pid;
  • int status;
  • VALUE str;
    char buf[256];
  • pid = pst_pid(st);
  • status = NUM2INT(st);
  • str = rb_sprintf("#<%s: pid=%ld", rb_class2name(CLASS_OF(st)),
    NUM2LONG(pid));
  • snprintf(buf, sizeof(buf), “pid %ld”, (long)pid);
  • rb_str_cat2(str, buf);
    if (WIFSTOPPED(status)) {
    int stopsig = WSTOPSIG(status);
    const char *signame = ruby_signal_name(stopsig);
    if (signame) {
  •  snprintf(buf, sizeof(buf), ",stopped(SIG%s=%d)", signame, 
    

stopsig);

  •  snprintf(buf, sizeof(buf), " stopped SIG%s (signal %d)", signame, 
    

stopsig);
}
else {

  •  snprintf(buf, sizeof(buf), ",stopped(%d)", stopsig);
    
  •  snprintf(buf, sizeof(buf), " stopped signal %d", stopsig);
    
    }
    rb_str_cat2(str, buf);
    }
    @@ -312,22 +284,67 @@
    int termsig = WTERMSIG(status);
    const char *signame = ruby_signal_name(termsig);
    if (signame) {
  •  snprintf(buf, sizeof(buf), ",signaled(SIG%s=%d)", signame, 
    

termsig);

  •  snprintf(buf, sizeof(buf), " SIG%s (signal %d)", signame, 
    

termsig);
}
else {

  •  snprintf(buf, sizeof(buf), ",signaled(%d)", termsig);
    
  •  snprintf(buf, sizeof(buf), " signal %d", termsig);
    
    }
    rb_str_cat2(str, buf);
    }
    if (WIFEXITED(status)) {
  • snprintf(buf, sizeof(buf), “,exited(%d)”, WEXITSTATUS(status));
  • snprintf(buf, sizeof(buf), " exit %d", WEXITSTATUS(status));
    rb_str_cat2(str, buf);
    }
    #ifdef WCOREDUMP
    if (WCOREDUMP(status)) {
  • rb_str_cat2(str, “,coredumped”);
  • rb_str_cat2(str, " (core dumped)");
    }
    #endif
    +}

+/*

    • call-seq:
    • stat.to_s   => string
      
    • Show pid and exit status as a string.
  • */

+static VALUE
+pst_to_s(VALUE st)
+{

  • rb_pid_t pid;
  • int status;
  • VALUE str;
  • pid = NUM2LONG(pst_pid(st));
  • status = NUM2INT(st);
  • str = rb_str_buf_new(0);
  • pst_message(str, pid, status);
  • return str;
    +}

+/*

    • call-seq:
    • stat.inspect   => string
      
    • Override the inspection method.
  • */

+static VALUE
+pst_inspect(VALUE st)
+{

  • rb_pid_t pid;
  • int status;
  • VALUE str;
  • pid = NUM2LONG(pst_pid(st));
  • status = NUM2INT(st);
  • str = rb_sprintf("#<%s: ", rb_class2name(CLASS_OF(st)));
  • pst_message(str, pid, status);
    rb_str_cat2(str, “>”);
    return str;
    }

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:32053] Process::Status#to_s”
on Sun, 14 Oct 2007 11:42:08 +0900, Tanaka A. [email protected]
writes:

|e$B8=:_e(B Process::Status#to_s e$B$O@0?t$JJ8;zNs$rJV$7$^$9!#e(B
|
|% ./ruby -e ‘system(“exit 1;”); p $?.to_s’
|“256”
|
|e$B$3$l$O$o$+$j$K$/$$$N$G0J2<$N$h$&$JJ8;zNs$rJV$9$N$O$I$&$G$7$ge(B
|e$B$&$+!#e(B

e$B%3%_%C%H$7$F$/$@$5$$!#e(B