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. Other args can use single-quotes.
12 * Generated using Win32 "CMD /?":
14 * 1. If all of the following conditions are met, then quote characters
15 * on the command line are preserved:
18 * - exactly two quote characters
19 * - no special characters between the two quote characters, where special
21 * - there are one or more whitespace characters between the two quote
23 * - the string between the two quote characters is the name of an
26 * 2. Otherwise, old behavior is to see if the first character is a quote
27 * character and if so, strip the leading character and remove the last
28 * quote character on the command line, preserving any text after the last
31 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
35 *-------------------------------------------------------------------------
38 #if defined(WIN32) && !defined(__CYGWIN__)
43 #include "postgres_fe.h"
53 pgwin32_system(const char *command)
55 size_t cmdlen = strlen(command);
61 * Create a malloc'd copy of the command string, enclosed with an extra
64 buf = malloc(cmdlen + 2 + 1);
71 memcpy(&buf[1], command, cmdlen);
72 buf[cmdlen + 1] = '"';
73 buf[cmdlen + 2] = '\0';
86 pgwin32_popen(const char *command, const char *type)
88 size_t cmdlen = strlen(command);
94 * Create a malloc'd copy of the command string, enclosed with an extra
97 buf = malloc(cmdlen + 2 + 1);
104 memcpy(&buf[1], command, cmdlen);
105 buf[cmdlen + 1] = '"';
106 buf[cmdlen + 2] = '\0';
108 res = _popen(buf, type);