Yihui Xie
2023-Oct-30 20:36 UTC
[Rd] system()/system2() using short paths of commands on Windows?
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. Please forgive my ignorance. I still do not understand it, but thanks a lot for the explanation anyway! I'm just curious if the full path would work in system() today. If it still would not work because today's Windows is still like Windows 95 in this aspect, please ignore my question and I will ask Microsoft for a refund. Regards, Yihui On Mon, Oct 30, 2023 at 3:03?PM Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:> > On 30/10/2023 16:18, Yihui Xie wrote: > > Hi, > > > > It may have been so for 20+ years but I just discovered today that system() > > would always try to use the short path of a command on Windows: > > github.com/wch/r-source/blob/635a67/src/gnuwin32/run.c#L141 If > > that's true, I wonder if it could provide an option to disable this > > behavior, because we recently ran into a case in which short paths wouldn't > > work. I wonder what the original motivation of using short paths was. If it > > was to avoid spaces in paths, wouldn't shQuote() work? Thanks! > > No: system on Windows does not use a shell. > > The 'original motivation' was to work reliably! Back in the days of > Windows 95 when many parts of Windows only supported 8+3 names. > > > > > Regards, > > Yihui > > > > [[alternative HTML version deleted]] > > Please do re-read the posting guide. It has ' been so for 20+ years '.My apologies! Sometimes I forget to switch to the plain-text mode when writing to R mailing lists.> -- > Brian D. Ripley, ripley at stats.ox.ac.uk > Emeritus Professor of Applied Statistics, University of Oxford >
Ivan Krylov
2023-Oct-31 08:54 UTC
[Rd] system()/system2() using short paths of commands on Windows?
On Mon, 30 Oct 2023 15:36:50 -0500 Yihui Xie <xie at 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: github.com/wch/r-source/blob/635a672151a18a0e475986af592fab59e7479a9b/src/gnuwin32/run.c#L378-L386 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
Tomas Kalibera
2023-Oct-31 09:22 UTC
[Rd] system()/system2() using short paths of commands on Windows?
On 10/30/23 21:36, Yihui Xie 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. > Please forgive my ignorance. I still do not understand it, but thanks > a lot for the explanation anyway!As the documentation says, system() on Windows runs the command directly, without a shell (without cmd.exe). As it says, 'command must be an executable (extensions ?.exe?, ?.com?) or a batch file (extensions ?.cmd? and ?.bat?): these extensions are tried in turn if none is supplied. This means that redirection, pipes, DOS internal commands, ... cannot be used: see shell if you want to pass a shell command-line. ' Things like redirection, pipes, etc are implemented by a shell, you can use shell() to run a command via "cmd.exe /c ...", so these would work.> I'm just curious if the full path > would work in system() today. If it still would not work because > today's Windows is still like Windows 95 in this aspect, please ignore > my question and I will ask Microsoft for a refund.I am not sure if you are asking a general question or specifically still for this use. In principle, short names are still useful to get rid of spaces (sometimes they are not quoted correctly, sometimes they cannot be quoted correctly such as in make). Also short names reduce the risk of running over the path-length limits. So R uses short names when they are available, but supports also long names and tries itself to quote properly. You have run into a case when an external wrapper has a bug and isn't able to deal with short names. There could easily be other wrappers around, which have a different bug and cannot deal with long names, e.g. because of spaces, when passing them to other programs. Very much like in luatex. And they may have been written in times when they actually were correct. Best Tomas> Regards, > Yihui > > > > On Mon, Oct 30, 2023 at 3:03?PM Prof Brian Ripley<ripley at stats.ox.ac.uk> wrote: >> On 30/10/2023 16:18, Yihui Xie wrote: >>> Hi, >>> >>> It may have been so for 20+ years but I just discovered today that system() >>> would always try to use the short path of a command on Windows: >>> github.com/wch/r-source/blob/635a67/src/gnuwin32/run.c#L141 If >>> that's true, I wonder if it could provide an option to disable this >>> behavior, because we recently ran into a case in which short paths wouldn't >>> work. I wonder what the original motivation of using short paths was. If it >>> was to avoid spaces in paths, wouldn't shQuote() work? Thanks! >> No: system on Windows does not use a shell. >> >> The 'original motivation' was to work reliably! Back in the days of >> Windows 95 when many parts of Windows only supported 8+3 names. >> >>> Regards, >>> Yihui >>> >>> [[alternative HTML version deleted]] >> Please do re-read the posting guide. It has ' been so for 20+ years '. > My apologies! Sometimes I forget to switch to the plain-text mode when > writing to R mailing lists. > >> -- >> Brian D. Ripley,ripley at stats.ox.ac.uk >> Emeritus Professor of Applied Statistics, University of Oxford >> > ______________________________________________ > R-devel at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-devel[[alternative HTML version deleted]]
Apparently Analagous Threads
- system()/system2() using short paths of commands on Windows?
- system()/system2() using short paths of commands on Windows?
- system()/system2() using short paths of commands on Windows?
- system()/system2() using short paths of commands on Windows?
- system()/system2() using short paths of commands on Windows?