views:

27

answers:

1

What's the exact way to properly quote a single command line argument? For example, I have some random text in a variable $X. I need to quote it in a way so that if I call

system("program.exe " + $X_QUOTED);

then argv[1] of my program.exe has to match original unquoted $X

imagine I have this program.exe:

int main(const char **argv, int){ puts(argv[1]); }

and the output of command: "program xxxx" is:

"test |test

what xxxx has to literally be? I tried to add quotes and all that trickery, but then I can always add some other type of output that would break my approach to quote cmd line arguments.

A: 
H:>args """test |test"
argv[0] = args
argv[1] = "test |test

Apparently:

  • Replace each quote by ""
  • Surround the argument with quotes
Joey
That's the thing. For me it actually prints:1 : "test2 : |testMaybe because I'm using perl in this case and it uses different approach to split commandline?..
PPS
1 : "test<br/>2 : |test<br/>how the hell can i put newlines?!???
PPS
@PPS: You can't. You can edit your original question though, if you need to add something.
Joey
Also I don't know what exactly Perl's `system` command does. I have never used Perl and probably never will.
Joey
At the end it appeared that perl uses some buddy implementation to split command line parameters and that's why I would never be able to get it done properly. It appears that perl that comes with msys is the one that does it properly (and I used activestate or strawberry perl and both of them link to some other runtime that uses some other rules that aren't used on win32)
PPS