I have the following code
Code:
#include <stdio.h>
#include <stdlib.h> // for getenv. don't implicit call this function
#include "functions.h"
void Form(void){
/*if REQUEST_METHOD == GET */
char *GETS = getenv("QUERY_STRING");
/*gets query string up to \0 and assignes to GETVALUE */
/*if REQUEST_METHOD == POST */
char *formLength = getenv("CONTENT_LENGTH");
long size = atol(formLength);
char hold[size];
if ( (GETS) !=NULL){
decodeUrl(GETS);
fprintf(stdout,"<p>%s</p>",GETS);
}
else{ //method was post. now parse accordingly.
printf("The submission size is %d characters.", size);
if( ( fgets(hold,size,stdin) ) !=NULL)
decodeUrl(hold);
fprintf(stdout, "<p>%s</p>",hold);
} //end of else
return;
}
where decodeUrl is defined as
Code:
#include "functions.h"
void decodeUrl(char *str){
/*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. Converting ox to decimal should not be done on textarea and
other text type input in case end user meant for that to happen */
while (*str++ ){
puts(str);
if ( *str == '+')
*str = ' ';
if ( *str == '&')
*str = '\n';
}
}
Now when I run this code it works fine. However attempting to duplicate it. I coded the follwoing:
Code:
#include <stdio.h>
void decodeUrl (char *str);
int main(void){
char hold[]="this + *990))^%$#&^%$#21+++)(*&^%$4";
char *hold2="this + *990))^%$#&^%$#21+++)(*&^%$4";
decodeUrl(hold);
decodeUrl(hold2);
puts(hold);
puts(hold2);
getchar();
return 0;
}
void decodeUrl(char *str){
/*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. Converting ox to decimal should not be done on textarea and
other text type input in case end user meant for that to happen */
while (*str++ ){
// puts(str);
if ( *str == '+')
*str = ' ';
if ( *str == '&')
*str = '\n';
}
}
And lo and behold I'm crashing my program. What am I not seeing. Everytime I think I have a hold on this language I get another surprise ...seemngly...
I'm going back over my chapters on arrays, pointers and char arrays, but in the meantime, someone enlighten me please...