• C

C Programming: string compare

I'm having trouble with a method that compares two strings. Specifically I need my program to understand the difference between "http://www.csun.edu/" and "http://www.csun.edu/~steve/" (see config file below).

If I pass "http://www.csun.edu/", how can I make my program understand I'm referring to the redirect line in my config file and not the cached line.

I tried doing strcat( URL, " " ) as well as strcat( URL, "\t" ) to add an extra space or tab character at the end of my URL, but then later in my code when I do strstr( URL_ConfigFile, URL ), it returns a NULL pointer saying that a the URL string doesn't exist inside of my config file.

I think it has something to do with the additional space ( " " ) I added.

 

# Example Configuration File for deflect:  
# deflect:  Daemon for HTTP Egress, Filtering, Load balancing, and Caching Trivially
#
# URL-prefix			Disposition	Host list

http://www.csun.edu/		redirected	130.166.238.195, redwing.csun.edu
http://facebook.com/		filtered
http://www.csun.edu/~steve/	cached		www.csun.edu

# Note that any URL that is not covered by any rule is transferred through the system 
# without any additional processing.

Open in new window

LVL 8
pzozulkaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
What was in URL before the strcat, and how was URL_ConfigFile set?
0
pzozulkaAuthor Commented:
The URL before strcat was "http://www.csun.edu/".

The URL_ConfigFile was created using nano on a Linux machine. I tried editing that file to have tabs and spaces between the URL and the Disposition columns. I tried both ways. As of right now, I'm using tabs to separate the columns.
0
ozoCommented:
What was in URL_ConfigFile when strstr( URL_ConfigFile, URL ) was executed?

Can you do strstr( URL_ConfigFile, "http://www.csun.edu/") and then check the following character?
0
Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

pzozulkaAuthor Commented:
The config file mentioned above was read() into a buffer. I then scanned the buffer, and replaced all \r or \n with \0. This broke the file into lines where each line is a string.

 Then there is a for-loop that cycles through each line, treating each line as a string.

So to answer your question, during one of the iterations of the loop:
URL_ConfigFile = "http://www.csun.edu/            redirected      130.166.238.195, redwing.csun.edu"

URL = "http://www.csun.edu/            " //tab character at the end was added with strcat()
0
ozoCommented:
In that case, strstr( URL_ConfigFile, URL ) would not return a null pointer.
0
pzozulkaAuthor Commented:
For simplicity sake I said URL_ConfigFile, but in my code it's actually Lines[j]. See below.

So I tried strstr(Lines[j], "www.csun.edu/"), and it returns the line in the config file that says cached.

I tried comparing to "www.csun.edu/ " <-- (space char at the end), but no luck.
I also tried "www.csun.edu/     " <-- (tab char at the end), also no luck.

if(strstr(Lines[j], "www.csun.edu/") != NULL) {
					if(strstr(Lines[j], "redirected") != NULL) {
						m = 1;
					}
					else if(strstr(Lines[j], "filtered") != NULL) {
						m = 2;
					}
					else if(strstr(Lines[j], "cached") != NULL) {
						m = 3;
					}
				}
				else
					m = -1;

Open in new window

0
ozoCommented:
You can't do strcat( "www.csun.edu/","\t" )
0
ozoCommented:
can you try
    char *p;
   if( (p=strstr(Lines[j], "www.csun.edu/")) != NULL && p[13] != '~' ){  printf("%c %d\n",p[13],p[13]);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pzozulkaAuthor Commented:
Here's what I get, very strange:
http://www.csun.edu/            redirected      130.166.238.195, redwing.csun.edu
R:       9
http://facebook.com/            filtered
http://www.csun.edu/~steve/     cached          www.csun.edu

Open in new window


Code:
fprintf(configfp,"%s\n",Lines[i]);

				char *p;
				if( (p=strstr(Lines[i], "www.csun.edu/")) != NULL && p[13] != '~' )
				{
					fprintf(configfp, "R:%c %d\n",p[13],p[13]);
				}
				if(strstr(Lines[i], "www.csun.edu/") != NULL) {
					if(strstr(Lines[i], "redirected") != NULL) {
						m = 1;
					}
					else if(strstr(Lines[i], "filtered") != NULL) {
						m = 2;
					}
					else if(strstr(Lines[i], "cached") != NULL) {
						m = 3;
					}
				}
				else
					m = -1;

Open in new window

0
ozoCommented:
9 == '\t'
0
pzozulkaAuthor Commented:
Thanks much, I think I know what happened. I kept on looping even after finding the correct line in my config file. Since the loop continued it would override my m variable that keeps track of this, and at the end would return the wrong thing.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.