Question:
I wrote a cgi script in C language to read the string entered from the web page and append the string to a local text file data. txt;
The cgi script is as follows, the source file of collect.c:
#include#include #define MAXLEN 80/* 4 for field name "data", 1 for "=" */#define EXTRA 5/* 1 for added line break , 1 for trailing NUL */#define MAXINPUT MAXLEN+EXTRA+2#define DATAFILE "./data.txt" void unencode(char *src, char *last, char *dest) {for(; src != last; src++ , dest++) if (*src =='+') {*dest = '';} else if(*src =='%') {int code; if (sscanf(src+1, "%2x", &code ) != 1) {code ='?';} *dest = code; src +=2;} else {*dest = *src;} *dest =' '; *++dest =' ' ;} int main(void) {char *lenstr; char input[MAXINPUT]; char data[MAXINPUT]; long len; printf("%s%c%c ", "Content-Ty pe:text/html;charset=iso-8859-1",13,10); printf(" Response "); lenstr = getenv("CONTENT_LENGTH"); if (lenstr == NULL || sscanf(lenstr,"%ld",&len) != 1 || len> MAXLEN) {printf("Error in invocation-wrong FORM probably. ");} else {printf("< p>CONTENT_LENGTH is %ld.
", len); // fgets(input, len+1, stdin); fgets(input, len+1, stdin); unencode(input+EXTRA, input+ len, data); FILE *fp; fp = fopen(DATAFILE, "a+"); if(fp == NULL) {printf("Sorry, cannot store your data.
");} else {printf("Your data is: %s.
", data); fputs(data, fp);} fclose(fp); printf("Thank you! The following contribution of yours has been stored:
%s ",data);} return 0;}
The web page file is as follows, collect.html: