Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

CGI in C

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
shepson990
Asked:
shepson990
  • 8
  • 8
  • 5
  • +1
1 Solution
 
dhyaneshCommented:
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
 
sunnycoderCommented:
>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
 
dhyaneshCommented:
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
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!

 
pankajtiwaryCommented:
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
 
sunnycoderCommented:
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
 
shepson990Author Commented:
how would i do it if there was no  &PIP=dhyanesh. if it was just MIP=230.76.2.190
0
 
sunnycoderCommented:
IP = strrchr ( string, '=' );
IP++;
0
 
sunnycoderCommented:
or
IP = strchr ( string, '=' );
IP++;
will work equally well
0
 
shepson990Author Commented:
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
 
dhyaneshCommented:
Check my first post I have allocated memory for MIP using malloc.
0
 
shepson990Author Commented:
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
 
dhyaneshCommented:
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
 
shepson990Author Commented:
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
 
sunnycoderCommented:
>say char MIP contains  203.7.5.136  how would i go about getting the 4th number using strtok.
fourth = strrchr ( MIP, '.' );
fourth++;
0
 
sunnycoderCommented:
>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
 
shepson990Author Commented:
>>sorry one other question do i have to asign memory to MIP using mallox??.
>No
Should I???
Thanks for the clarification
0
 
sunnycoderCommented:
>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
 
dhyaneshCommented:
>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
 
shepson990Author Commented:
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
 
shepson990Author Commented:
sorry about my last post, i worked the problem out
thanks
0
 
shepson990Author Commented:
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
 
sunnycoderCommented:
> 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!

  • 8
  • 8
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now