1 /*-------------------------------------------------------------------------
4 * Win32 system() and popen() replacements
7 * Win32 needs double quotes at the beginning and end of system()
8 * strings. If not, it gets confused with multiple quoted strings.
9 * It also requires double-quotes around the executable name and
10 * any files used for redirection. Filter other args through
11 * appendShellString() to quote them.
13 * Generated using Win32 "CMD /?":
15 * 1. If all of the following conditions are met, then quote characters
16 * on the command line are preserved:
19 * - exactly two quote characters
20 * - no special characters between the two quote characters, where special
22 * - there are one or more whitespace characters between the two quote
24 * - the string between the two quote characters is the name of an
27 * 2. Otherwise, old behavior is to see if the first character is a quote
28 * character and if so, strip the leading character and remove the last
29 * quote character on the command line, preserving any text after the last
32 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
36 *-------------------------------------------------------------------------
39 #if defined(WIN32) && !defined(__CYGWIN__)
44 #include "postgres_fe.h"
54 pgwin32_system(const char *command)
56 size_t cmdlen = strlen(command);
62 * Create a malloc'd copy of the command string, enclosed with an extra
65 buf = malloc(cmdlen + 2 + 1);
72 memcpy(&buf[1], command, cmdlen);
73 buf[cmdlen + 1] = '"';
74 buf[cmdlen + 2] = '\0';
87 pgwin32_popen(const char *command, const char *type)
89 size_t cmdlen = strlen(command);
95 * Create a malloc'd copy of the command string, enclosed with an extra
98 buf = malloc(cmdlen + 2 + 1);
105 memcpy(&buf[1], command, cmdlen);
106 buf[cmdlen + 1] = '"';
107 buf[cmdlen + 2] = '\0';
109 res = _popen(buf, type);