csshgreppopenprintf

Keeping double quotes when passing string to popen in C


I'm having a dilemma where I'm trying to pass a string through popen in C, but have it maintain the double quotes in the string. The string reads as follows:

ssh %s@%s grep -c \"%s\" %s%s

I need to run this command so it greps from a log on a remote system and returns the count for it. I'm passing different arguments to through the string, so it generates the username and password as well as the search string and target. However, the search string contains multiple words separated by whitespace characters, so I need the double quotes intact so it parses out the search string correctly. However, so far popen has been stripping the double quotes from the string so the search doesn't complete. I'm not sure of another way to do the remote ssh command, or of keeping the double quotes in the statement. Any ideas?

Thanks!

*EDIT: Here is the full sprintf statement I'm using to generate the string as well as the popen command.

sprintf(command, "ssh %s@%s grep -c \"%s\" %s%s", user, server, search, path, file);
fd = popen(command, "r");

Solution

  • popen forks a child which execs a shell with 2 arguments: -c and the command string you passed to popen. So any characters you pass that are meaningful to the shell need to be escaped so the shell does the right thing with them. In your case, you need the shell to KEEP the " characters so that the remote shell will get them, which is easiest to do by wrapping ' quotes around them:

    sprintf(command, "ssh %s@%s grep -c '\"%s\"' %s%s", ...
    

    However, this will only work if your search string does not contain any ' or " characters -- if it does, you need to do some more complex escaping. You could instead use backslash escapes:

    sprintf(command, "ssh %s@%s grep -c \\\"%s\\\" %s%s", ...
    

    but THIS will fail if your search string has quotes or multiple consecutive spaces or other whitespace like tabs int it. To deal with all cases you need first insert backslashes before all relevant charaters in the search string, plus ' are a pain to deal with:

    // allocate 4*strlen(search)+1 space as escaped_search
    for (p1 = search, p2 = escaped_search; *p1;) {
        if (*p1 == '\'')
            *p2++ = '\'';
        if (strchr(" \t\r\n\"\\'{}()<>;&|`$", *p1))
            *p2++ = '\\';
        if (*p1 == '\'') 
            *p2++ = '\'';
        *p2++ = *p1++;
    }
    *p2 = '\0';
    sprintf(command, "ssh %s@%s grep -c '%s' %s%s", user, server, escaped_search, ...