Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Quote filenames with single quotes on POSIX systems #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alastair
Copy link
Contributor

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.

@alnesbit
Copy link
Contributor

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

@alastair
Copy link
Contributor Author

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

@fluential
Copy link

fluential commented Oct 12, 2021

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" {}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ffmpeg popen does not protect from crazy filenames
3 participants