To keep myself interested in C, I decided to start messing with CGI. I am just testing stuff out now, getting used to C for the web, but I have already hit a problem. I bet it is with my pointers, but I am not sure. The code I am testing is to get form data. What I have:
Code:
#include <stdio.h>
#include <stdlib.h>
void clear_string(char *string, int size);
int main(void) {
char *query;
query = (char *)malloc(sizeof getenv("QUERY_STRING") + 1);
clear_string(query, sizeof getenv("QUERY_STRING") + 1);
query = getenv("QUERY_STRING");
printf("Content-Type: text/html\n\n");
printf("<html>\n");
printf("<head>\n");
printf("<title>CGI form test</title>\n");
printf("</head>\n");
printf("<body>\n");
if ( strcmp(*query, NULL) ) {
printf("<form action=\"cgiform.cgi\" method=\"post\">\n");
printf("\tName: <input type=\"text\" name=\"name\" />\n");
printf("\t<input type=\"submit\" value=\"Submit!\"/>\n");
printf("</form>\n");
}
else {
printf("submitted");
printf("\n%s", *query);
}
printf("</body>\n");
printf("</html>\n");
return(0);
}
void clear_string(char *string, int size) {
/* clear_string padds a pointer string with 'size' number of \0's (Nulls), making it usefull for both clearing strings, and preparing them for non-
null terminated strings to be entered */
int i;
for (i = 0; i < size; ++i) string[i] = '\0';
}
The clear string function is to clear the memoryspace with nulls for the query to be saved in, and I have used it to also clear one more space with a null character, so the string becomes null terminated when it is inserted.
Anyway, is this my pointer or what is it? I get a Segmentation fault when the script tries to compare the string to the null.