Escaping backslashes issue with printf/grep/popen!

A

agarwalpiyush

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 "\*\.\* *[email protected]"

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 \"\\*\\.\\* *[email protected]\"
/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 \\\"\\\\*\\\\.\\\\*
*[email protected]\\\"
/etc/syslog.conf\"");
OR
sprintf (buff, "\"/bin/grep -soub \\\"\\\\*\\\\.\\\\*
*[email protected]\\\" /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!
 
C

Chris Torek

... 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 \\\"\\\\*\\\\.\\\\*
*[email protected]\\\"
/etc/syslog.conf\"");
OR
sprintf (buff, "\"/bin/grep -soub \\\"\\\\*\\\\.\\\\*
*[email protected]\\\" /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.
 
A

agarwalpiyush

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!
 
F

Flash Gordon

(e-mail address 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:
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.
 
F

Flash Gordon

Flash said:
(e-mail address removed) wrote:


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



The printf statement given by (e-mail address 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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top