By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Escaping backslashes issue with printf/grep/popen!

P: n/a
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 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <11**********************@z14g2000cwz.googlegroups .com>
<ag***********@gmail.com> 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.
Dec 11 '05 #2

P: n/a
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 '05 #3

P: n/a
ag***********@gmail.com 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 <11**********************@z14g2000cwz.googlegroups .com>
<ag***********@gmail.com> 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.
Dec 11 '05 #4

P: n/a
Flash Gordon wrote:
ag***********@gmail.com 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 ag***********@gmail.com 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.
Dec 11 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.