Link to home
Start Free TrialLog in
Avatar of shepson990
shepson990

asked on

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
Avatar of dhyanesh
dhyanesh

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
Avatar of sunnycoder
>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()
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
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
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shepson990

ASKER

how would i do it if there was no  &PIP=dhyanesh. if it was just MIP=230.76.2.190
IP = strrchr ( string, '=' );
IP++;
or
IP = strchr ( string, '=' );
IP++;
will work equally well
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.?
Check my first post I have allocated memory for MIP using malloc.
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);
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
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.?

>say char MIP contains  203.7.5.136  how would i go about getting the 4th number using strtok.
fourth = strrchr ( MIP, '.' );
fourth++;
>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
>>sorry one other question do i have to asign memory to MIP using mallox??.
>No
Should I???
Thanks for the clarification
>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
>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
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());
      }
sorry about my last post, i worked the problem out
thanks
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??
> 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