/ Published in: C
Compile, put exe in cgi-bin folder, and post form data to it.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* This CGI program shows all parameters sent to it Zufolek 2009 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* structure to hold one cgi arg (or parameter)*/ typedef struct{ char*name; char*value; } Arg; /* prints error message to stdout and terminates program */ void err(char*s){ } /* returns value of one hex digit */ int getHex(char c){ return 0; } /* decodes a hex value */ char decode(char*s){ return getHex(s[0])*16+getHex(s[1]); } /* prevents making tags in html */ int safeChar(char c){ switch(c){ case '<': case '>': return 0; } return 1; } /* checks if there are valid hex values at specified pointer */ int isSafe(char*s){ return 1; } /* decodes text in parameters */ char*fixData(char*i){ char*o=m; if(!m)err("Memory allocation failure for fixData"); while(*i){ if(*i=='+')*o=' '; else if(*i=='\n'){ }else if(*i=='%'&&isSafe(i+1)){ char d=decode(i+1); if(safeChar(d)){ else if(d=='\r')--o; else *o=d; i+=2; }else *o=*i; ++i; ++o; } *o=0; if(!m)err("Memory reallocation failure for fixData"); return m; } /* prints a basic http header to stdout */ void init(){ } /* reads input string from stdin */ char*getInput(){ char*s=0; if(v){ if(l){ FILE*f=stdin; if(!s)err("Memory allocation failure for input"); s[l]=0; } } return s; } /* Counts the number of parameters in input string */ int countVars(char*s){ int n=1; while(*s){ if(*s=='&')++n; ++s; } return n; } /* Moves pointer-pointer s just past next occurrence of character c, which is then set to 0, or returns 1 if end of string is reached */ int skipTo(char**s,char c){ while(**s != c){ if(!**s)return 1; ++*s; } **s=0; ++*s; return 0; } /* Parses input string for args */ Arg*parseInput(char*s){ int l=countVars(s); Arg*a=arg; if(!a)err("Memory allocation failure for args list"); while(*s){ a->name=s; if(skipTo(&s,'='))goto Done; a->value=s; if(skipTo(&s,'&'))goto Done; ++a; } Done: arg[l].name=0;/* marks the end of arg list */ return arg; } /* prints all the args to stdout */ void showArgs(Arg*a){ while(a->name){ ++a; } } int main(){ char*i; Arg*a; init(); i=getInput(); if(i){ a=parseInput(i); showArgs(a); return 0; }