subprocess: Do not escape double quotes for command line arguments on Windows

* refactor: Guard `util::quote_argument()` with `#ifdef __USING_WINDOWS__`

The `util::quote_argument()` function is specific to Windows and is used
in code already guarded by `#ifdef __USING_WINDOWS__`.

* Do not escape double quotes for command line arguments on Windows

This change fixes the handling of double quotes and aligns the behavior
with Python's `Popen` class. For example:
```
>py -3
>>> import subprocess
>>> p = subprocess.Popen("cmd.exe /c dir \"C:\\Program Files\"", stdout=subprocess.PIPE, text=True)
>>> print(f"Captured stdout:\n{stdout}")
```

Currently, the same command line processed by the `quote_argument()`
function looks like `cmd.exe /c dir "\"C:\Program" "Files\""`, which is
broken.

With this change, it looks correct: `cmd.exe /c dir "C:\Program Files"`.

Github-Pull: arun11299/cpp-subprocess#113
Rebased-From: ed313971c04ac10dc006104aba07d016ffc6542a
This commit is contained in:
Hennadii Stepanov 2025-04-27 17:23:27 +01:00
parent df7a52241f
commit bf8230d525
No known key found for this signature in database
GPG key ID: 410108112E7EA81F

View file

@ -171,6 +171,7 @@ public:
//--------------------------------------------------------------------
namespace util
{
#ifdef __USING_WINDOWS__
inline void quote_argument(const std::wstring &argument, std::wstring &command_line,
bool force)
{
@ -181,7 +182,7 @@ namespace util
//
if (force == false && argument.empty() == false &&
argument.find_first_of(L" \t\n\v\"") == argument.npos) {
argument.find_first_of(L" \t\n\v") == argument.npos) {
command_line.append(argument);
}
else {
@ -231,7 +232,6 @@ namespace util
}
}
#ifdef __USING_WINDOWS__
inline std::string get_last_error(DWORD errorMessageID)
{
if (errorMessageID == 0)