[Rd] system()/system2() using short paths of commands on Windows?

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Tue Oct 31 09:54:01 CET 2023


On Mon, 30 Oct 2023 15:36:50 -0500
Yihui Xie <xie using yihui.name> wrote:

> I have read about "system() not using a shell on Windows" on the help
> page many times before but never understood what it means technically.

This means resolving the path to the executable, then calling
CreateProcess() to launch that executable with the given parameters and
other settings:
https://github.com/wch/r-source/blob/635a672151a18a0e475986af592fab59e7479a9b/src/gnuwin32/run.c#L378-L386
https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa

As far as I know, CreateProcess() is the most direct officially
supported way to launch a process on Windows.

The alternative would be assembling the command line and giving it to
cmd.exe to interpret and launch, as done by shell(). This can be more
convenient in certain cases, as the shell can expand environment
variables from %STRINGS% or launch multiple commands separated by &,
but it also requires extreme care: quoting rules are subtle and have to
be understood on both the cmd.exe level *and* the C runtime level
(on Windows, the C runtime is responsible for parsing the string
returned from GetCommandLine() and creating the `argv` command line
argument array from it).

Working with shell command lines is like using string operations on
deparse() output instead of directly operating on language objects:
less complicated than rocket surgery, but better avoided if
alternatives exist.

-- 
Best regards,
Ivan



More information about the R-devel mailing list