On Jan 8, 2008, at 10:50 AM, JJ wrote:
> This is a dangerous way to program. The correct solution (given by
> Nobuyoshi) is this:
>
>   cmd = "C:/Program Files/Internet Explorer/IEXPLORE.exe"
>   system([cmd, cmd], "http://www.google.com")
>
> This avoids the sub-shell mess all together, regardless of whether  
> "C:/
> Program Files/Internet" is executable.

I believe:

system(cmd, "http://www.google.com")

would also skip the shell entirely, since you are providing more than  
one argument to system.

The problem is that when you don't want to pass any arguments to the  
program you are left with a single argument to system (or exec),  
which triggers the shell expansion algorithm. As you illustrated  
with  'Program Files', this is a problem if your cmd path includes  
characters that the shell is interested in (such as spaces). As  
Nobuyoshi pointed out, the way out of that is to pass an array  
instead of  a single argument.

I just wanted to point out that if you are passing arguments to the  
program you can use:

   system(cmd, arg1, arg2)

instead of

   system([cmd, cmd], arg1, arg2)

and still avoid the shell.  Probably safer to use the second syntax  
though since if
you are doing something like:

   system(cmd, *args)

You may in fact end up passing just a single argument and invoke the  
shell again.

Seems like a dodgy sort of design to me.  I gather it comes from  
trying to
merge system() and exec() from the C library, probably for systems that
don't have a concept of fork().  From JJ's post, I gather that Perl  
might
be the source of this chimera.

Gary Wright