473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Escaping backslashes issue with printf/grep/popen!

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
4 3211
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 (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.
Dec 11 '05 #2
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tom Chadwin | last post by:
Hello all Using PHP 4.1.2, I am seeing intermittent visible escaping backslashes in my HTML output. Try refreshing the following page a few times to see what I mean: ...
5
by: Aloysio Figueiredo | last post by:
I need to replace every ocurrence of '/' in s by '\/' in order to create a file named s. My first attempt was: s = '\/'.join(s.split('/')) but it doesn't work: >>> s = 'a/b' >>> s =...
3
by: Regan | last post by:
Hello, I have done tons of searching on this topic but have yet to find something relavent to the problem I am experiencing so I am hoping someone can help me. The problem I am having is that...
0
by: DeadAtBirth | last post by:
I have an XML file which have nodes that contain filepaths, e.g.<path>c:\SomeDirectory\SomeFile.txt</path> I'm using an XmlDocument and XPath query to try and find a node XmlDocument doc = new...
1
by: bhavya sg | last post by:
Hi, I saw in PEP4 of python 2.5 that grep module has gone obsolete in perl 2.5. But I am not able to find an alternative for that.My doubt is "are the other forms of grep like egrep and ggrep...
3
by: Taras_96 | last post by:
Hi everyone, I'm having a bit of trouble understanding the purpose of escaping nulls, and the use of addcslashes. Firstly, the manual states that: "Strictly speaking, MySQL requires only...
1
The1corrupted
by: The1corrupted | last post by:
Okay, this is somewhat annoying but I need to escape backslashes to make something look nice. <?php if ($mesgread=="/" AND $user!=$otheruser) { $action=explode(" ", $msg, 2); ...
3
by: placid | last post by:
Hi All, I have these files; which are Merge Request (ClearCase) files that are created by a Perl CGI script (being re-written in Python, as the HTML/ JavaScript have been mixed with Perl,...
7
by: wannymahoots | last post by:
optparse seems to be escaping control characters that I pass as arguments on the command line. Is this a bug? Am I missing something? Can this be prevented, or worked around? This behaviour...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.