Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Escaping backslashes issue with printf/grep/popen!

Reply
Thread Tools

Escaping backslashes issue with printf/grep/popen!

 
 
agarwalpiyush@gmail.com
Guest
Posts: n/a
 
      12-11-2005
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!

 
Reply With Quote
 
 
 
 
Chris Torek
Guest
Posts: n/a
 
      12-11-2005
In article <(E-Mail Removed) .com>
<(E-Mail Removed)> 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 (4039.22'N, 11150.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.
 
Reply With Quote
 
 
 
 
agarwalpiyush@gmail.com
Guest
Posts: n/a
 
      12-11-2005
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!

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      12-11-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 <(E-Mail Removed) .com>
>> <(E-Mail Removed)> 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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      12-11-2005
Flash Gordon wrote:
> (E-Mail Removed) 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 (E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Escaping backslashes in 'HERE documents'? Jim Ford Perl Misc 3 05-18-2007 11:30 PM
GridView and Backslashes =?Utf-8?B?TWF0dCBIYW1pbHRvbg==?= ASP .Net 0 05-02-2006 12:21 PM
Save String that contains a windows file path with backslashes peter.o.mueller@gmx.de Java 1 09-05-2005 07:51 PM
Double BackSlashes Mikheil C++ 6 08-15-2003 05:00 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments