Escaping backslashes issue with printf/grep/popen!

Discussion in 'C Programming' started by agarwalpiyush@gmail.com, Dec 11, 2005.

  1. Guest

    Hello,
    I am going nuts with trying to get the following to work:

    This is what I intend to do: I have a line in /etc/syslog.conf which I
    need to delete based on ip-address provided to me in a variable.

    Forgetting that variable part for now .. this is what i want in the
    grep command:

    grep -suob "\*\.\* *...@172.23.62.12"

    to get the byte offset to the line

    *.* @172.23.62.12

    The entire goal is to delete this line (and not just comment it) from
    the file. I am a complete newbie to Sed but I'd ahve to create a new
    file and rename it to /etc/syslog.conf? .. kinda not neat?

    So, trying to use popen.

    Sub-Goal:
    FILE *fp = popen ("/bin/grep -soub \"\\*\\.\\* *...@172.23.62.12\"
    /etc/syslog.conf", "r");

    /* Just two characters */
    [\"] is a single character. So is [\\].

    Now the issue is that 172.23.62.12 is a variable, so I tried to use
    sprintf/strcpy like this:

    strcpy (buff, "\"/bin/grep -soub \\\"\\\\*\\\\.\\\\*
    *...@172.23.62.12\\\"
    /etc/syslog.conf\"");
    OR
    sprintf (buff, "\"/bin/grep -soub \\\"\\\\*\\\\.\\\\*
    *...@172.23.62.12\\\" /etc/syslog.conf\"");

    Followed by:
    FILE *fp = popen (buff, "r");

    But I get an error on grep indicating that the correct commands to grep
    are not getting passed!

    I seem to realize that both strcpy and sprintf above copy the string as

    /* FOUR characters :-( */
    [\]["] and [\]][\] -- which are 4 different characters as opposed to
    two "escaped" characters above.

    What am I doing wrong?

    Any help deeply appreciated!

    Thanks!
     
    , Dec 11, 2005
    #1
    1. Advertising

  2. Chris Torek Guest

    In article <>
    <> wrote:
    >... Now the issue is that 172.23.62.12 is a variable, so I tried to use
    >sprintf/strcpy like this:
    >
    >strcpy (buff, "\"/bin/grep -soub \\\"\\\\*\\\\.\\\\*
    >*...@172.23.62.12\\\"
    >/etc/syslog.conf\"");
    >OR
    >sprintf (buff, "\"/bin/grep -soub \\\"\\\\*\\\\.\\\\*
    >*...@172.23.62.12\\\" /etc/syslog.conf\"");
    >
    >Followed by:
    > FILE *fp = popen (buff, "r");


    popen() is not an ANSI/ISO standard C function, so I cannot comment
    on it here. (The system() function is, but -- except when you use
    system(NULL) to find out if system() is available -- its parameters
    are completely machine-dependent, which is no better.) I can,
    however, suggest that you do this to see what command(s) you are
    using:

    printf("going to invoke --%s--\n", buff);

    Use that, along with information available in a POSIX newsgroup like
    comp.unix.programmer, to figure out what you are passing to popen(),
    vs what you should be passing.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Dec 11, 2005
    #2
    1. Advertising

  3. Guest

    I tried that but the issue is printf does some escaping and replacement
    of characters by itself. So printing it there does not give us the
    correct picture. The explanation of 2 v/s 4 characters i gave above is
    based on examination of memory using gdb.

    I am still waiting for help!
    Thanks!
     
    , Dec 11, 2005
    #3
  4. Flash Gordon Guest

    wrote:


    Provide context, or do you think others provide context for the fun of
    it? See http://cfaj.freeshell.org/google/

    I happen to have the previous post available, the context was:
    >> In article <>
    >> <> wrote:
    >>> >... Now the issue is that 172.23.62.12 is a variable, so I tried to use
    >>> >sprintf/strcpy like this:
    >>> >
    >>> >strcpy (buff, "\"/bin/grep -soub \\\"\\\\*\\\\.\\\\*
    >>> >*...@172.23.62.12\\\"

    ..>> >/etc/syslog.conf\"");
    >>> >OR
    >>> >sprintf (buff, "\"/bin/grep -soub \\\"\\\\*\\\\.\\\\*
    >>> >*...@172.23.62.12\\\" /etc/syslog.conf\"");
    >>> >
    >>> >Followed by:
    >>> > FILE *fp = popen (buff, "r");

    >>
    >> popen() is not an ANSI/ISO standard C function, so I cannot comment
    >> on it here. (The system() function is, but -- except when you use
    >> system(NULL) to find out if system() is available -- its parameters
    >> are completely machine-dependent, which is no better.) I can,
    >> however, suggest that you do this to see what command(s) you are
    >> using:
    >>
    >> printf("going to invoke --%s--\n", buff);
    >>
    >> Use that, along with information available in a POSIX newsgroup like
    >> comp.unix.programmer, to figure out what you are passing to popen(),
    >> vs what you should be passing.


    > I tried that but the issue is printf does some escaping and replacement
    > of characters by itself. So printing it there does not give us the
    > correct picture.


    The printf statement given by Chris Torek shows you
    *exactly* what you are going to pass to popen with NO additional
    escaping. Try it and believe what it says.

    > The explanation of 2 v/s 4 characters i gave above is
    > based on examination of memory using gdb.


    gdb, on the other hand, may be adding in escaping to what it shows you.
    Read the manual or ask where it is topical.

    > I am still waiting for help!


    You've received about all the help you are likely to receive here.
    However I suggest you consider exactly what the command is you would
    type at the command line, specifically whether you would put the entire
    command in quotes, which is what you seem to be doing with your code.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Dec 11, 2005
    #4
  5. Flash Gordon Guest

    Flash Gordon wrote:
    > wrote:
    >
    >
    > Provide context, or do you think others provide context for the fun of
    > it? See http://cfaj.freeshell.org/google/


    <snip>

    >>> printf("going to invoke --%s--\n", buff);
    >>>
    >>> Use that, along with information available in a POSIX newsgroup like
    >>> comp.unix.programmer, to figure out what you are passing to popen(),
    >>> vs what you should be passing.

    >
    >> I tried that but the issue is printf does some escaping and replacement
    >> of characters by itself. So printing it there does not give us the
    >> correct picture.

    >
    > The printf statement given by shows you
    > *exactly* what you are going to pass to popen with NO additional
    > escaping. Try it and believe what it says.


    I meant provided by Chris Torek. An error I made due to you not
    providing any context.

    <snip>
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Dec 11, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Seberino
    Replies:
    6
    Views:
    5,958
    Nobody
    Jun 12, 2010
  2. Chris Withers

    subprocess escaping POpen?!

    Chris Withers, Aug 5, 2010, in forum: Python
    Replies:
    0
    Views:
    207
    Chris Withers
    Aug 5, 2010
  3. Chris Withers

    Re: subprocess escaping POpen?!

    Chris Withers, Aug 5, 2010, in forum: Python
    Replies:
    2
    Views:
    324
    Lawrence D'Oliveiro
    Aug 6, 2010
  4. Dan King

    Using popen & grep

    Dan King, Apr 12, 2010, in forum: Ruby
    Replies:
    2
    Views:
    215
    Robert Klemme
    Apr 13, 2010
  5. Jim Ford
    Replies:
    3
    Views:
    459
    Raymundo
    May 19, 2007
Loading...

Share This Page