tags:

views:

226

answers:

2

I'm trying to call popen() using mingw like this:

#define RUN_COMMAND "\"C:\\Program Files\\CA\\BrightStor ARCserve Backup\\ca_qmgr.exe\" \"-list\""
int main() {
    outputPointer = popen(RUN_COMMAND, "r");
    ...
}

But I can't get it working. I think it's a quoting nightmare ...

A: 

According to the man page for popen:

The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to /bin/sh using the -c flag; interpretation, if any, is performed by the shell.

So what you want is to escape the string for sh to parse. You do not need to escape the -list argument, and you can try to escape the spaces instead of quoting the whole program name. I do not have a windows system here to test with but I would suggest

#define RUN_COMMAND "C:\\Program Files\\CA\\BrightStor\\ ARCserve\\ Backup\\ca_qmgr.exe -list"
int main() {
    outputPointer = popen(RUN_COMMAND, "r");
    ...
}
hlovdal
Bear in mind this is MinGW, though, so I don't know that `popen` passes the command to /bin/sh. Could it be passing it to some other more Windows-y and less POSIX-y shell instead? One which will interpret those backslashes as path separators rather than escapes, and which will have its own quoting rules.
Steve Jessop
+2  A: 

Take a look at What is the equivalent to posix popen in the win32 api. I ditched my attempt to use _popen() under Windows a long time ago. The pure WIN32 solution performs much more reliably.

If mingw is passing your parameters to sh -c instead of cmd.exe /c, then you probably want to change those backslashes to forward slashes. Something like the following should do the trick.

"\"C:/Program Files/CA/BrightStor ARCserve Backup/ca_qmgr.exe\" -list"
D.Shawley