Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CGI in C

Posted on 2003-12-01
22
Medium Priority
?
422 Views
Last Modified: 2013-11-18
hi i am trying to write a cgi in c program.
i have a char  szString  containing   the following  
MIP=230.76.2.190&PIP=user

i need to extract the number (ip address) after MIP= into a char called MIP and extract the username after &PIP= into a char called user. Note that the username (user) will not always be the same length nor will the ipaddress
0
Comment
Question by:shepson990
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
  • 5
  • +1
22 Comments
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9855698
Hi

Here is a small program which does what you want.

It copies everything after "MIP=" until "&PIP=" into a char array IP.
It also copies everything after "&PIP=" until end of string into char array Username.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main(void)
{
      char *szString = "MIP=230.76.2.190 &PIP=dhyanesh";
      char *IP, *Username;
      char *IPptr,*Userptr;

      IPptr = strstr(szString,"MIP=");
      IPptr += 4;
      Userptr = strstr(szString,"&PIP=");
      IP = (char *)malloc(Userptr-IPptr+1);
      strncpy(IP,IPptr,Userptr-IPptr);
      IP[Userptr-IPptr] = '\0';
      Userptr += 5;
      Username = (char *)malloc(strlen(Userptr)+1);
      strncpy(Username,Userptr,strlen(Userptr)+1);
      printf("IP : %s \nUsername : %s",IP,Username);

      free(IP);
      free(Username);
      return 0;
}


Dhyanesh
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9855708
>i need to extract the number (ip address) after MIP= into a char called MIP and extract the username after &PIP= into a char
>called user. Note that the username (user) will not always be the same length nor will the ipaddress
I presume you mean array of chars / char pointer for which you have allocated sufficient memory

>MIP=230.76.2.190&PIP=user

char * MIP;     /*make sure you malloced sufficient memory */
char * user;

char * temp1, *temp2;

temp1 = strstr ( string, "MIP=" );
temp1 = temp1+4;

temp2 = strstr ( string, "&PIP=" );

strncpy ( MIP, temp1, temp2-temp1 );
* ( MIP + temp2 - temp1 ) = 0;

temp = temp2 + 5;
strcpy ( user, temp2 );

There are two more ways of doing it -- strtok() and strchr()
0
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9855716
Hi

The explanation is:

First using strstr find location of "MIP=" and increment by four to point just after the "=" sign.

Next find location of "&PIP=". Allocate space for IP and copy the characters from IPptr.

Now increment Userptr by 5 to point just after the "=" sign of "&PIP=". Allocate space and copy characters until end of string.

Dhyanesh
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:pankajtiwary
ID: 9855719
Hi shepson990,

Why don't you do one thing. If you start writing everything by scratch that will be a problem. You can do one thing, there is a library API available name chic. Using that you can easily take input from the string or even from POST request. You can search for cgic in google and that will do it for you. You can also have the source code for that and thats quite easy to understand also.

Hope it helps
pankajtiwary
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 600 total points
ID: 9855739
here is how to do it using strtok ::: assuming again that your data is in string

char * MIP;    
char * user;
char * delim = "&=";

strtok ( string, delim );
MIP = strtok ( NULL, delim );
strtok ( NULL, delim);
user = strtok (NULL, delim);
0
 

Author Comment

by:shepson990
ID: 9855776
how would i do it if there was no  &PIP=dhyanesh. if it was just MIP=230.76.2.190
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9855784
IP = strrchr ( string, '=' );
IP++;
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9855787
or
IP = strchr ( string, '=' );
IP++;
will work equally well
0
 

Author Comment

by:shepson990
ID: 9863232
sorry one other question do i have to asign memory to MIP using mallox??.
also say char MIP contains  203.7.5.136  how would i go about getting the 4th number using strtok. note that the numbers between the "."'s won always be the same length.?
0
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9864097
Check my first post I have allocated memory for MIP using malloc.
0
 

Author Comment

by:shepson990
ID: 9864119
yes but how can i do it using sunnycoder's way, it's looks alot simpler this way

char * MIP;    
char * user;
char * delim = "&=";

strtok ( string, delim );
MIP = strtok ( NULL, delim );
strtok ( NULL, delim);
user = strtok (NULL, delim);
0
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9864498
Hi

In this case MIP is only a pointer to a place in string where the IP is stored. If you want to store it somewhere else you have to then create a new pointer like:

char *newMIP;
newMIP = (char *)malloc(strlen(MIP));           //allocate memory
strcpy(newMIP,MIP);

Also same you will have to do for user.

One more thing, your original string i.e. 'string' will be reduced to "MIP" because strtok puts '\0' character just before the delimiter. You must copy it to another location if you wish to use it further.

Dhyanesh
0
 

Author Comment

by:shepson990
ID: 9864560
thanks for that info
sorry Dhyanesh i cant give you any points as i had already gave them to sunnycoder but thanks anyway


what about
say char MIP contains  203.7.5.136  how would i go about getting the 4th number using strtok. note that the numbers between the "."'s won always be the same length.?

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9864571
>say char MIP contains  203.7.5.136  how would i go about getting the 4th number using strtok.
fourth = strrchr ( MIP, '.' );
fourth++;
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9864604
>sorry one other question do i have to asign memory to MIP using mallox??.
No

Just a few clarifications:
>One more thing, your original string i.e. 'string' will be reduced to "MIP" because strtok puts '\0' character just before the
>delimiter.
is misleading ... String will not be reduced to MIP but broken down into tokens as strtok puts \0 in place of each delimiter (not before) ... thus all delimiters will be totally lost and original string will be reduced to a sequence of tokens
0
 

Author Comment

by:shepson990
ID: 9864617
>>sorry one other question do i have to asign memory to MIP using mallox??.
>No
Should I???
Thanks for the clarification
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9864656
>Should I???
No ... strtok returns char * .... all you need is to store that address and for that purpose char * is sufficient

there is no point in doing
temp = malloc (...);       /*assign memory */
temp = strtok (..);

the memory assigned gets lost and results in memory leaks

However, if you are going to make *copy* of the token, then you must allocate memory (as Dhyanesh showed)

If you are going to use the token as such, you do not have to and you should not allocate memory for it
0
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9865211
>One more thing, your original string i.e. 'string' will be reduced to "MIP" because strtok puts '\0' character just before the
>delimiter.
is misleading ... String will not be reduced to MIP but broken down into tokens as strtok puts \0 in place of each delimiter (not before) ... thus all delimiters will be totally lost and original string will be reduced to a sequence of tokens


Thanks for clarifying sunnycoder. Actually that is exactly what I had meant when wrote the line. However I may not have put it in the correct words.

Dhyanesh
0
 

Author Comment

by:shepson990
ID: 9870824
okay char fourth contains the fourth number in the ip address but i think it has a  white space after the number!. i need to remove that space as i am going to have an if statement like below but it doesnt work, which i think is because of a space in fourth.


      if (fourth >= "140" && (fourth <=150 ) {
            // If ip above or = to 140 and below or = to 150 user must be on Max6000
            sprintf(szBuf,"xxxxxxx",SNMP,MAX4000MIB,getpid());
      }
0
 

Author Comment

by:shepson990
ID: 9872153
sorry about my last post, i worked the problem out
thanks
0
 

Author Comment

by:shepson990
ID: 9878761
one last question
i want to create a file name in char szTemp. i have been trying to do this using sprintf like below

sprintf(szTemp,"/tmp/snmpdata.%x",getpid());

but this does not not work for some reason. It compiles okay but when i am running it on my webserver, the webserver says unexpected end of script. Is there an alternate to sprintf that i could use??
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9881044
> It compiles okay but when i am running it on my webserver, the webserver says unexpected end of script. Is there an
>alternate to sprintf that i could use??
the sprintf looks ok ... there could be an error in some other part of your script
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question