public Method

Kernel.exec(...)

exec(command [, arg, ...])

Replaces the current process by running the given external command.

If +exec+ is given a single argument, that argument is
taken as a line that is subject to shell expansion before being
executed. If multiple arguments are given, the second and subsequent
arguments are passed as parameters to _command_ with no shell
expansion. If the first argument is a two-element array, the first
element is the command to be executed, and the second argument is
used as the <code>argv[0]</code> value, which may show up in process
listings. In MSDOS environments, the command is executed in a
subshell; otherwise, one of the <code>exec(2)</code> system calls is
used, so the running command may inherit some of the environment of
the original program (including open file descriptors).

   exec "echo *"       # echoes list of files in current directory

never get here

exec "echo", "*"    # echoes an asterisk

never get here

Source Code

/*
*  call-seq:
*     exec(command [, arg, ...])
*
*  Replaces the current process by running the given external _command_.
*  If +exec+ is given a single argument, that argument is
*  taken as a line that is subject to shell expansion before being
*  executed. If multiple arguments are given, the second and subsequent
*  arguments are passed as parameters to _command_ with no shell
*  expansion. If the first argument is a two-element array, the first
*  element is the command to be executed, and the second argument is
*  used as the <code>argv[0]</code> value, which may show up in process
*  listings. In MSDOS environments, the command is executed in a
*  subshell; otherwise, one of the <code>exec(2)</code> system calls is
*  used, so the running command may inherit some of the environment of
*  the original program (including open file descriptors).
*
*     exec "echo *"       # echoes list of files in current directory
*     # never get here
*
*
*     exec "echo", "*"    # echoes an asterisk
*     # never get here
*/

VALUE
rb_f_exec(argc, argv)
   int argc;
   VALUE *argv;
{
   VALUE prog = 0;
   VALUE tmp;

   if (argc == 0) {
       rb_raise(rb_eArgError, "wrong number of arguments");
   }

   tmp = rb_check_array_type(argv[0]);
   if (!NIL_P(tmp)) {
       if (RARRAY(tmp)->len != 2) {
           rb_raise(rb_eArgError, "wrong first argument");
       }
       prog = RARRAY(tmp)->ptr[0];
       argv[0] = RARRAY(tmp)->ptr[1];
       SafeStringValue(prog);
   }
   if (argc == 1 && prog == 0) {
       VALUE cmd = argv[0];

       SafeStringValue(cmd);
       rb_proc_exec(RSTRING(cmd)->ptr);
   }
   else {
       proc_exec_n(argc, argv, prog);
   }
   rb_sys_fail(RSTRING(argv[0])->ptr);
   return Qnil;                /* dummy */
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.