echoprint-codegen icon indicating copy to clipboard operation
echoprint-codegen copied to clipboard

Quote filenames with single quotes on POSIX systems

Open alastair opened this issue 12 years ago • 3 comments

This should fix #27 and maybe #25. On POSIX systems at least, quoting popen parameters with single quotes and replacing single quotes inside the filename with "'\\''" results in the filename being passed exactly. I've tested it with files named with backticks and single quotes. (http://stackoverflow.com/a/35857)

Since most of the GetCommandLine method used std::string anyway, I moved from cstrings for easier find/replace.

Tested on OSX and Linux. Not tested on windows, but it shouldn't make things any worse off.

alastair avatar May 10 '12 04:05 alastair

Thanks Alastair! Yes, I agree that doing the single quotes like this is the most robust solution for Unix-like systems. But as far as I can see, this may break operation on Windows - doesn't it handle single quotes differently?

Andrew

alnesbit avatar May 10 '12 17:05 alnesbit

You're right - single quotes might break on Windows. I suppose the alternative might be to #ifdef QUOTE to ' on posix and " on Windows and use that instead. I have no idea about quote behaviour on Windows, so can't comment on specifics

alastair avatar May 10 '12 18:05 alastair

Replacing isn't a robust solution either as this may break depending on which shell is used. In our case the only valid solution is to do fork and exec via execpl() like described here https://stackoverflow.com/questions/21845181/getting-return-from-execlp -- so there is no shell interpolation.

For bash a quick workaround would be to produce escaped string like this (if your files do not contain newlines):

find dir -type f -print0 |xargs -0 -I{} printf "%q\n" {}

fluential avatar Oct 12 '21 11:10 fluential