Solved

CGI in C

Posted on 2003-12-01
22
410 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
  • 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
 
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 150 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now