#include #include #include #include #include #define GUESTBOOK_FILE "/home/fria/gemini/guestbook.gmi" int ishex(int x) { return (x >= '0' && x <= '9') || (x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F'); } int urldecode(const char *s, char *dec) { char *o; const char *end = s + strlen(s); int c; for (o = dec; s <= end; o++) { c = *s++; if (c == '+') c = ' '; else if (c == '%' && ( !ishex(*s++) || !ishex(*s++) || !sscanf(s - 2, "%2x", &c))) return -1; if (dec) *o = c; } return o - dec; } int main() { char *input=getenv("QUERY_STRING"); FILE *fp=NULL; time_t t = time(NULL); struct tm tm = *localtime(&t); char *output=NULL; if(!(input && *input)) { printf("10 Enter message?\r\n"); exit(0); } output=malloc(sizeof(*output)*strlen(input)+1); if(output==NULL) exit(1); if(urldecode(input,output)==-1) exit(1); if((fp=fopen(GUESTBOOK_FILE,"a"))==NULL) exit(1); fprintf(fp,"### %d-%02d-%02d %02d:%02d:%02d\n%s\n",tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,output); fclose(fp); free(output); output=NULL; printf("30 /cgi-bin/guestbook/view.cgi\r\n"); return 0; }