Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: The Evil Embedded Space (system(@list))

by tye (Sage)
on May 30, 2005 at 18:17 UTC ( #461836=note: print w/replies, xml ) Need Help??

in reply to The Evil Embedded Space

Until system(@list) works in Win32 [it is currently just system("@list") which is system($scalar) which isn't what is needed for portably dealing with obnoxious file names], spaces in path names will be a big problem.

This issue has little to do with "the shell", since the design of Win32 means that "the shell" has almost no responsibility for the splitting of command-line arguments (on spaces), which is the source of the problem. Each program must split its own command line and they certainly don't all go about this the same way.

But things have finally settled down such that the MS Win32 C RTL's definition of how to quote arguments is fairly widely supported. This means that system(@list) can be made to mostly work, by adding quotes around arguments that would need them if parsed by a the C RTL's command-line parser (and by escaping a few things).

Now, if one insists on using the make command, then "the shell" does become a significant source of problems and you need to invent platform-customized quoting schemes to deal with space in file names and beat people up to always use them. But that is just one problem with using make and so the best solution for this is to move away from using make in a system that tries to be portable.

Of course, "platform-customized quoting" really should be "shell-customized" but the traditional solution here is to use the one shell that is guaranteed to be on the platform in question. So, on most platforms, /bin/sh is used even for installing things for users who prefer to use zsh or whatever. Likewise, on NT+ Win32, cmd.exe is used for the same reason. Unfortunately, this same logic means that is used on pre-NT Win32, but is not quite up to this task. But this, again, leads me to the clear solution of relying on Perl instead of some version of 'make' and some semblance of 'sh'.

- tye        

  • Comment on Re: The Evil Embedded Space (system(@list))

Replies are listed 'Best First'.
Re^2: The Evil Embedded Space (system(@list))
by tye (Sage) on Jun 03, 2005 at 21:25 UTC

    The basic Microsoft C argument processing is that you put double quotes around anything to make it a single argument. Backslash is only special if it is followed by a double quote. So \ is just \ but \" becomes a literal " character, but this is only reliable if done within double quotes. So \\" becomes a backslash followed by a close-quote. \\\" becomes backslash followed by literal quote.

    So you want something like:

    for( @args ) { if( /[\s"^*?%<>|&]/ ) { s#(\\*)"#$1$1\\"#g; $_= '"' . $_ . '"'; } }

    Perhaps with more special characters in the first match.

    Update: Oops, I was missing one backslash in my replacement. I had $1$1" when I needed $1$1\\", and we should probably avoid adding quotes if the argument is already surrounded by quotes, at least by default.

    - tye        

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://461836]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2018-10-19 19:51 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (110 votes). Check out past polls.