exec(...)
public
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 argv[0] value, which may show up in
process listings. In MSDOS environments, the command is executed in a
subshell; otherwise, one of the exec(2) system calls is used, so
the running command may inherit some of the environment of the original
program (including open file descriptors).
exec "echo *"
exec "echo", "*"
Show source
/*
* 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;
struct rb_exec_arg earg;
if (argc == 0) {
rb_last_status = Qnil;
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);
}
proc_prepare_args(&earg, argc, argv, prog);
proc_exec_args((VALUE)&earg);
rb_sys_fail(RSTRING(argv[0])->ptr);
return Qnil; /* dummy */
}