#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
	char *endptr;
   	int i;
	double contentlength;
   	char name[1000];
	char email[1000];
	char webpage[1000];
	char comment[10000];
	char webbuff[10000];
        char buff2[10000];
	char hexstr[100];
	char *final;
	int escape;
	int x;
 	int y;
	const char *len1 = getenv("CONTENT_LENGTH");
 	contentlength=strtol(len1, &endptr, 10);
 	fread(webbuff, contentlength, 1, stdin); //get url parms
  	printf("Content-type: text/html\n\n"); //need this or it angers the server
   	for (x = 0, y = 0; x < strlen(webbuff); x++, y++) //loop to get rid of hex. thanx http://monarch.papillion.ne.us/~sthomas/cgi_in_c.html
      	{
      		switch (webbuff[x])
         	{
         		/* Convert all + chars to space chars */
         		case '+':
         		buff2[y] = ' ';
         		break;

         		/* Convert all %xy hex codes into ASCII chars */
         		case '%':

         		/* Copy the two bytes following the % */
         		strncpy(hexstr, &webbuff[x + 1], 2);

         		/* Skip over the hex */
         		x = x + 2;

         		/* Convert the hex to ASCII */
         		/* Prevent user from altering URL delimiter sequence */
         		if( ((strcmp(hexstr,"26")==0)) || ((strcmp(hexstr,"3D")==0)) )
            		{
            			buff2[y]='%';
            			y++;
            			strcpy(buff2,hexstr);
            			y=y+2;
            			break;
            		}

         		buff2[y] = (char)strtol(hexstr, NULL, 16);
         		break;

         		/* Make an exact copy of anything else */
         		default:
         		buff2[y] = webbuff[x];
         		break;
         	}     	
	}
	printf("%s %s\n<br>",webbuff,buff2);
/*	
	x = strcspn(buff2,"=")+1;
	y = strcspn(buff2,"&");
	printf("%d %d<br>\n",x,y);
        strncpy(split,buff2+x,y-x);
	printf("%s<br>\n",split);
	printf("obuff: %s\n<br>",buff1);
        */
	
	//grab &var and seperate value's
	i=0; //if you want this start with this line and stop at stop
	escape = 0;
	while( i < strlen(buff2) && escape != 1) {
		y = 0;
		for(x = 0 ; x < strlen(name) ; x++) {  //clear string
			name[x] = ' ';
		}
		strncpy(&name[y],&buff2[i],1);
	//	printf("1name: %s\n<br>",name);
		while(buff2[i++] != '=') {   //this copies the &var so it can be checked
			y++;
			strncpy(&name[y],&buff2[i],1);
		//	printf("w1buff: %s\n<br>",name);
		
		}
		name[y] = ' ';
		//printf("2buff: %s\n<br>",name);
       		
		if(strstr(name,"name")) { //this looks at &var and if it matches it gets the data from it
			for(x = 0 ; x < strlen(name) ; x++) { //empty the string
				name[x] = ' ';
			}
			strncpy(&name[y],&buff2[i],1);
			while(buff2[i++] != '&' && buff2[i] != 0 ) {
				y++;
				strncpy(&name[y],&buff2[i],1);
			//	printf("w2buff: %s\n<br>",name);
		
			
			}
			if(name[y] == '&')
				name[y] = ' '; //cut the & off the string
			printf("Name: %s\n<br>",name);
			escape = 1; //escape!
		
		} else {
			while(buff2[i++] != '&') {
				
		}	
		}
	} //stop
  	
	i=0;
	escape = 0;
	while( i < strlen(buff2) && escape != 1) {
		y = 0;
		for(x = 0 ; x < strlen(email) ; x++) {
			email[x] = ' ';
		}
		strncpy(&email[y],&buff2[i],1);
//		printf("1email: %s\n<br>",email);
		while(buff2[i++] != '=') {
			y++;
			strncpy(&email[y],&buff2[i],1);
//			printf("w1buff: %s\n<br>",email);
		
		}
		email[y] = ' ';
//		printf("2buff: %s\n<br>",email);
       		
		if(strstr(email,"email")) {
			for(x = 0 ; x < strlen(email) ; x++) {
				email[x] = ' ';
			}
			strncpy(&email[y],&buff2[i],1);
			while(buff2[i++] != '&' && buff2[i] != 0 ) {
				y++;
				strncpy(&email[y],&buff2[i],1);
			//	printf("w2buff: %s\n<br>",email);
		
			}
			if(email[y] == '&')
				email[y] = ' ';
			printf("e-mail: %s\n<br>",email);
			//strcpy(final,buff1);
			escape = 1;
		
		} else {
			while(buff2[i++] != '&') {
				
		}	
		}
	}
	i=0;
	escape = 0;
	while( i < strlen(buff2) && escape != 1) {
		y = 0;
		for(x = 0 ; x < strlen(webpage) ; x++) {
			webpage[x] = ' ';
		}
		strncpy(&webpage[y],&buff2[i],1);
	//	printf("1webpage: %s\n<br>",webpage);
		while(buff2[i++] != '=') {
			y++;
			strncpy(&webpage[y],&buff2[i],1);
		//	printf("w1buff: %s\n<br>",webpage);
		
		}
		webpage[y] = ' ';
		//printf("2buff: %s\n<br>",webpage);
       		
		if(strstr(webpage,"webpage")) {
			for(x = 0 ; x < strlen(webpage) ; x++) {
				webpage[x] = ' ';
			}
			strncpy(&webpage[y],&buff2[i],1);
			while(buff2[i++] != '&' && buff2[i] != 0 ) {
				y++;
				strncpy(&webpage[y],&buff2[i],1);
			//	printf("w2buff: %s\n<br>",webpage);
		
			
			}
			if(webpage[y] == '&')
				webpage[y] = ' ';
			printf("webpage: %s\n<br>",webpage);
			//strcpy(final,buff1);
			escape = 1;
		
		} else {
			while(buff2[i++] != '&') {
				
		}	
		}
	}
	i=0;
	escape = 0;
	while( i < strlen(buff2) && escape != 1) {
		y = 0;
		for(x = 0 ; x < strlen(comment) ; x++) {
			comment[x] = ' ';
		}
		strncpy(&comment[y],&buff2[i],1);
	//	printf("1comment: %s\n<br>",comment);
		while(buff2[i++] != '=') {
			y++;
			strncpy(&comment[y],&buff2[i],1);
		//	printf("w1buff: %s\n<br>",comment);
		
		}
		comment[y] = ' ';
		//printf("2buff: %s\n<br>",comment);
       		
		if(strstr(comment,"comment")) {
			for(x = 0 ; x < strlen(comment) ; x++) {
				comment[x] = ' ';
			}
			strncpy(&comment[y],&buff2[i],1);
			while(buff2[i++] != '&' && buff2[i] != 0 ) {
				y++;
				strncpy(&comment[y],&buff2[i],1);
			//	printf("w2buff: %s\n<br>",comment);
		
			
			}
			if(comment[y] == '&')
				comment[y] = ' ';
			printf("comment: %s\n<br>",comment);
			//strcpy(final,buff1);
			escape = 1;
		
		} else {
			while(buff2[i++] != '&') {
				
		}	
		}
	}
}


