&dave looks like you've been lurking all over.
here is the code. Its really annoying. More so because the first form seemed to work and then the second completly crashed and I don't just want to fix it. I want to understand why. Some have suggested I'm calling memory I've already freed but I can't see where.
here is some code.
the getenv function returns values of my environment and that is hard code html i'm running on a local server. but at least the functions can give you an idea.
here is getvalue and the subsequent functions it calls: decodeUrl,hex2dec(yours :d) and nameValue in that order.
Code:
#include <stdio.h>
#include <stdlib.h> // for getenv. don't implicitly call this function
#include <string.h>
#include "CGINCform.h"
char *getvalue(char *name){
if( (strcmp(getenv("REQUEST_METHOD"),"POST"))==0){
/*length of info from stdin,buffer for url, temp and return value of nameValue()*/
char *formLength;
char *url;
char *Temp;
char *ValueOfName;
unsigned long INPUT;/*size of input from stdin*/
/*get form length and convert to a number.*/
formLength = getenv("CONTENT_LENGTH");
INPUT = atol(formLength);
/*make sure room available on system to store info from stdin*/
if ((url = malloc( (INPUT+1) * sizeof(char) ) ) == NULL){
perror("No room Allocated for url");
return NULL;
}
if ((Temp = malloc((INPUT+1) * sizeof (char) ) ) == NULL){
perror("No room allocated for Temp");
return NULL;
}
/*get info from stdin and store accordingly */
if( ( fgets(url,INPUT,stdin) ) !=NULL){
decodeUrl(url);/*parses and changes url */
hex2Dec(Temp,url); /*copies a changed url to temp*/
/*look for the value of name in temp which is up to INPUT and returns its address*/
ValueOfName=nameValue(Temp,INPUT,name);
//free(url);/*isn't used anymore copied into temp by hex2Dec*/
//free(Temp);
}/*end of info processing info from STDIN */
return ValueOfName;
}/*end of 1st if block and succesful input from stdin */
else
return NULL;/*no valid HTTP method given end of if branch */
}/*end of function getvalue.c */
decodeUrl:
Code:
#include "CGINCform.h"
void decodeUrl(char *str){
int *FILEDS= 0;
/* only really usable ascii characters are from d32 to d127 but it starts from
0 to 255. So might as well just parse everything and have the computer do its job */
getenv("QUERY_STRING")? puts(" using GETS") : puts (" using POST");
/*this function looks for all the characters '+' and '&' and replaces them with
a space and a '\n' respectively. It also converts all hexadecimal values to their
decimal equivalent.
IMPORTANT: Converting hexx to decimal should not be done on textarea inpput and
other text type input in case end user meant for that to happen */
while (*str++ ){
if ( *str == '+')
*str = ' ';
if ( *str == '&')
*str = '\n';
FILEDS+=1;//counts the number of name vlaue pairs and is sent to getValue()
}
}
hex2Dec:
Code:
#include "CGINCform.h"
#include <stdio.h>
char *hex2Dec(char *dst, const char *src)
{
int i, j = 0;
for ( i = 0; src[i]; ++i)
{
if ( src[i] != '%' )
{
dst[j++] = src[i];
/*only put everythng not a '%' in dest buffer */
}
else //if its a '%'
{
unsigned ch;
if ( sscanf(&src[++i/* skip '%' */], "%2x"/*read 2 hex characteres */, &ch) != 1 )
/*scan data for hex values up to 2field width assign to ch
this is done because string is a long sequence with no spaces. if spaces
wouldn't have to use width specifier to tell how many numbers to read
would automatically read the hex number
for intelectual curiouslity: &src(i++... */
{
return NULL;// if not a hex character retrun null. interesing seems scanf won't
//act stupid if it's not you can let it do what you want it to.
}
dst[j++] = ch;// assign hex to to dest. is hex converted to deciaml then
++i; /* with the loop's increment, this will skip the 2nd hex chars
intellectual curiousity: i++ */
}
}
dst[j] = '\0';
return dst;
}
NameValue
Code:
#include <stdio.h>
#include <string.h>
#define MAXFIELDS 100
/*gonna have to rename nameValue to getvalue which will accept
getvalue(char *url,int length, char *name)
can't think of anything else right now.
*/
char* nameValue(char *URL,int len,char *name ){
/*make len already known and rename key to name.
this function returns the value of a name value pair */
char names[MAXFIELDS][MAXFIELDS],values[MAXFIELDS][MAXFIELDS],temp[MAXFIELDS];
char *word;
int ndx;
int posNum=0;
int ndx2=0;
/*we use a second counter becuase we'll have to reset it to zero without
stopping iteration of the url */
for (ndx=0;ndx<len;ndx++){
temp[ndx2++] = URL[ndx];
if ( (URL[ndx] == '=') ){
temp[ndx2-1] = '\0';
strcpy(names[posNum],temp);
ndx2=0; //reset buffer to indx 0 for next word
}
else if( (URL[ndx] == '\n') ){
temp[ndx2-1] = '\0';
strcpy(values[posNum],temp);
ndx2=0;
posNum+=1;//the word postion only changes after a '\n'
}
}
//this prints the name value pairs per line to test it works in stdout
int i;
for(i=0;i<posNum;i++)
printf("%s = %s<br>", names[i],values[i]);
for (ndx=0;ndx<posNum;ndx++){
if( (strcmp(names[ndx],name)) ==0)
word = values[ndx];
}//end of 2nd for
return word;
}
Main:
Code:
/* basic CGI program in C allows
one to receive form values and
manipulate them.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "CGINCform.h"
int main(void){
/*once CGINCform has done all the work - gather input,parse data,decode data
and return the value of the name looking for. Your now ready to write whatever
you want back to stdout - that is your webbrowser client. CGINCform returns
a char * to the value of the name given as its parameter. What you do with it is up to you
*/
char *Identifier;
Identifier = "Comments";/*store addy of HTML Identifier */
char *x;
x=getvalue(Identifier);/*store return addy of getvalue */
/*inform browser of the content type to display */
printf("Content-type: text/html\n\n");
/*write html code to display */
printf("<html>\n");
puts("<head> <title> CGINCform</title> </head>");
printf("<body bgcolor = \"purple\">\n");
/*on another module enumerate a list of colors and have user select */
printf("<center> <h1>CGINC</h1> </center>");
puts("<br>");
puts(x);
printf("<br>%s",x );
puts("</body>");
printf("</html>\n");
/*end of html output */
return 0;
}