From 41c62dc413d80d00ff564a00fd7cf92c150fa3a3 Mon Sep 17 00:00:00 2001 From: Andrey Prygunkov Date: Mon, 30 Nov 2015 19:38:59 +0100 Subject: [PATCH] #117: better handling of command line when calling external programs (Windows) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Trailing slashes must be doubled. This in particular improves compatibility with user-compiled “unrar”. --- daemon/util/Script.cpp | 24 +++++++++++++++++------- daemon/util/Script.h | 3 +++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/daemon/util/Script.cpp b/daemon/util/Script.cpp index c111b4ef..a4068524 100644 --- a/daemon/util/Script.cpp +++ b/daemon/util/Script.cpp @@ -449,6 +449,22 @@ int ScriptController::Execute() return exitCode; } +#ifdef WIN32 +void ScriptController::BuildCommandLine(char* cmdLineBuf, int bufSize) +{ + int usedLen = 0; + for (const char** argPtr = m_args; *argPtr; argPtr++) + { + const char* arg = *argPtr; + int len = strlen(arg); + bool endsWithBackslash = arg[len - 1] == '\\'; + snprintf(cmdLineBuf + usedLen, bufSize - usedLen, endsWithBackslash ? "\"%s\\\" " : "\"%s\" ", arg); + usedLen += len + 3 + (endsWithBackslash ? 1 : 0); + } + cmdLineBuf[usedLen < bufSize ? usedLen - 1 : bufSize - 1] = '\0'; +} +#endif + /* * Returns file descriptor of the read-pipe of -1 on error. */ @@ -459,13 +475,7 @@ int ScriptController::StartProcess() char cmdLineBuf[2048]; if (m_args) { - int usedLen = 0; - for (const char** argPtr = m_args; *argPtr; argPtr++) - { - snprintf(cmdLineBuf + usedLen, 2048 - usedLen, "\"%s\" ", *argPtr); - usedLen += strlen(*argPtr) + 3; - } - cmdLineBuf[usedLen < 2048 ? usedLen - 1 : 2048 - 1] = '\0'; + BuildCommandLine(cmdLineBuf, sizeof(cmdLineBuf)); cmdLine = cmdLineBuf; } diff --git a/daemon/util/Script.h b/daemon/util/Script.h index 36d7d842..5e537c7e 100644 --- a/daemon/util/Script.h +++ b/daemon/util/Script.h @@ -85,6 +85,9 @@ protected: void PrepareArgs(); int StartProcess(); int WaitProcess(); +#ifdef WIN32 + void BuildCommandLine(char* cmdLineBuf, int bufSize); +#endif void UnregisterRunningScript(); public: